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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.