Solved

Using Perl to replace LF with CR LF in all files in a directory

Posted on 2008-06-20
8
1,602 Views
Last Modified: 2008-06-24
I'm trying to run the following command over multiple files in a directory (the syntax below works for individual files).  The command replaces all the LF in the files with CRLF.  I want to place this command in a batch file that i can schedule to run automatically.  I also tried using wild cards *.* to execute over all files, but it doesn't work.  any suggestions?  thanks

perl -pe "s/\x{0d}/\x{0a}/g" c:\source\input1 > c:\destination\output1
0
Comment
Question by:snakahara7
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21834092
perl -ic:/destination/* -ple "BEGIN{chdir 'c:/source';@ARGV=<*>;$/=qq(\n);$\=qq(\r\n)}"
0
 

Author Comment

by:snakahara7
ID: 21834385
i tried it and it does go through all the files in the directory, but it doesn't do the conversion.  thanks.
0
 

Author Comment

by:snakahara7
ID: 21834978
it goes through all the files in the directory and makes a back up of them, but does it then convert the file and put the output somewhere else? or is it supposed to overwrite the original file?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:ghostdog74
ID: 21836398
since you are using Perl, then do everything in Perl
#!/usr/bin/perl
 
 
while ( <*.*> ) {
  
  open(FH, "<", $_);
  
   while( <FH> ) {
    $line=$_;
    $line =~ s/\n/\r\n/g;
    print $line;
   
   }
  close(FH);
 
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 21836454
#!/usr/bin/perl
chdir 'c:/source';
$^I='destination/*';
@ARGV=<*>:
$^I=.bak;
while( <>  ){
  s/\n/\r\n/;
  print;
}
0
 

Author Comment

by:snakahara7
ID: 21861679
thanks for the code snippets... i used the code and changed a few things and it's working properly (saved the program as processfiles.pl).  If i call it from the command line, it works fine, but if i place it in a .bat file and run the batch file, it opens a command window and shows "c:\test>perl processfiles.pl".  It just stays like that and never returns from the batch program.  when i eventually close the command window, it's as if the perl program was not run at all from the batch routine.  i have the following in the batch program:

move c:\source\* c:\destination            moves files to directory for processing
perl processfiles.pl                               perl program


processfiles.pl code:

#!/usr/bin/perl
chdir 'c:/source';
$^I='c:/destination/*';
@ARGV=<*>;
while( <>  ){
  s/\x{0d}/\x{0a}/g;
  print;
}

Any idea why my batch routine never returns/ends?  it just seems like it's stuck or in an infinite loop.
Thanks in advance for all your help...
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 21861726
after
move c:\source\* c:\destination
are there any files in c:\source left for the perl script to process?
If you want replace LF with CR LF in all files  all files in c:\destination
#!/usr/bin/perl
$^I='.bak';
@ARGV=<c:/destination/*>;
while( <>  ){
  s/\x{0d}/\x{0a}/g;
  print;
}
0
 

Author Comment

by:snakahara7
ID: 21861803
you are right... i had my directories mixed up and had moved all my files OUT of the processing folder.  Now that there's files to process, it is working GREAT!!!

Thanks ozo for all your help!!!
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question