Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi All, Am trying to write a perl script which reads the path and searches file recursive from a directory and sub directories and process the file print the xml output in a file.

Basically, 1. Read the path and search through directories/sub directories for a file, if file found then process the file and pass the file content to xml format and print them in a file. I have all the perl scripts as a bit's and pieces but don't know how to put them in a in single script and get the expected output.

In details : path give say : "C:\test" Dir1 -- inside Dir1 there is one more sub directories say Dir2 Dir2 -- inside Dir2 there 2 more sub directories say Dir2a/Dir2b (Each of the sub directories Dir2a/b has 2 files each) Dir3 -- inside Dir3 there is 2 more sub directories say Dir3a (Each of the sub directories Dir3a has 2 files each)

now perl script should read directories/sub directories and file names, as names will be passed to xml format before printing. when ever files found, content of file(space delimted) should be replaced by comma delimeter before passing the values of file content to xml format.

I really appreaciate if someone will show me how get to expected result shown below. i really want to know more about for loop/while loop in perl. am still in learing phase so i need this to be done but as i said i am really putting effort to learn perl. Many Thanks in advance

script which i have so far :
#! /usr/bin/perl use strict; use warnings; use File::Basename; use File::Find; use Data::Dumper; use Class::CSV; use Text::CSV; use XML::TreeBuilder; use Data::Dumper; my ($csv,$inputfilename,$resultfile,$fh,$line,$dir,$fp,$base_dir,$dh,$ +file); my (@dir,@dir_names); $base_dir = 'C:\test'; @dir = $base_dir; # logic to travel through path and gets the directory names(just name) while (@dir) { $dir = pop(@dir); opendir($dh, $dir); while($file = readdir($dh)) { next if $file eq '.'; next if $file eq '..'; $file = "$dir/$file"; if (-d $file) { push(@dir, $file); push(@dir_names, basename($file)); } else { } } } #print scalar @dir_names; my $dir_count = scalar @dir_names; print "The no of folder persent in the path $dir_count"; print "\n"; #logic to travel through given same path and gets the file names (just + name of the file) print "<!-- ********************* Printing files names *************** +***********-->\n"; print "\n"; my $dirfile = 'C:\Test'; find({ wanted => \&process_file, no_chdir => $dir_count }, $dirfile); sub process_file { if (-f $_) { #print "This is a file: $_\n"; $file = $_; $file =~ s/.*\///; #$file =basename($file); $file =~ s/\.[^.]+$//; #print "FileName without path [$file]\n"; my @filenames =$file; print @filenames; print "\n"; } } print "<!-- ********************************************************** +******-->\n"; #logic to replace all space to comma delimited from a file,but this lo +gic should be placed once if file is found from above directory searc +h... # below $dir path is just to for checking purpose.. $dir = $base_dir.'/'.$dir_names[0].'/'.$dir_names[1].'/'.$dir_names[2] +; #print $dir; open $resultfile, '>>', 'C:\Test' or die "Can't open file: $!"; #foreach (@dir_names){ foreach $fp (glob("$dir/*.bat")) { open ($fh, "<", $fp) or die "can't read open '$fp':"; while ($line = <$fh>) { $line=~ s/ /,/g; print $resultfile $line; print $resultfile "\n"; } close $fh or die "can't read close '$fp':"; }

All the input files will have the same format and same number of column's

EX: input file :
start putty -ssh -W 13633 1.0.7.1 -l maxi -qaq sooter

Expected output will be:

# finally the xml structure to print/output in a file <configuration version="0.7.1.136" savepassword="True"> <root type="database" name="DIR1 NAME" expanded="True"> <container type="folder" name="DIR2 NAME" expanded="True"> <container type="folder" name="DIR2A NAME" expanded="True"> <connection type="PuTTY" name="DIR2A's-FILE1 NAME"> <connection_info> <name>DIR2A's-FILE NAME</name> <protocol>SSH</protocol> <host>FILE1's COLUMN[6]</host> <port>FILE1's COLUMN[5]</port> <session>Default Settings</session> <commandline>FILE1's COLUMN[10]</commandline> <description /> </connection_info> </connection> <connection type="PuTTY" name="DIR2A's-FILE2 NAME"> <connection_info> <name>DIR2A's-FILE NAME</name> <protocol>SSH</protocol> <host>FILE1's COLUMN[6]</host> <port>FILE1's COLUMN[5]</port> <session>Default Settings</session> <commandline>FILE1's COLUMN[10]</commandline> <description /> </connection_info> </connection> <container type="folder" name="DIR2B's NAME" expanded="False"> <connection type="PuTTY" name="DIR2B's-FILE1 NAME"> <connection_info> <name>DIR2B's-FILE1 NAME</name> <protocol>SSH</protocol> <host>FILE1's COLUMN[6]</host> <port>FILE1's COLUMN[5]</port> <session>Default Settings</session> <commandline>FILE1's COLUMN[10]</commandline> <description /> </connection_info> </connection> <connection type="PuTTY" name="DIR2B's-FILE2 NAME"> <connection_info> <name>DIR2B's-FILE NAME</name> <protocol>SSH</protocol> <host>FILE2's COLUMN[6]</host> <port>FILE2's COLUMN[5]</port> <session>Default Settings</session> <commandline>FILE2's COLUMN[10]</commandline> <description /> </connection_info> </connection> <container type="folder" name="DIR3's NAME" expanded="False"> <connection type="PuTTY" name="DIR3B's-FILE1 NAME"> <connection_info> <name>DIR3B's-FILE1 NAME</name> <protocol>SSH</protocol> <host>FILE1's COLUMN[6]</host> <port>FILE1's COLUMN[5]</port> <session>Default Settings</session> <commandline>FILE1's COLUMN[10]</commandline> <description /> </connection_info> </connection> <connection type="PuTTY" name="DIR3B's-FILE2 NAME"> <connection_info> <name>DIR3B's-FILE NAME</name> <protocol>SSH</protocol> <host>FILE2's COLUMN[6]</host> <port>FILE2's COLUMN[5]</port> <session>Default Settings</session> <commandline>FILE2's COLUMN[10]</commandline> <description /> </connection_info> </connection> </container> </container> </root> </configuration>

In reply to Recursive file processing from a path and printing output in a file by Vijay81

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found