Link to home
Start Free TrialLog in
Avatar of snakahara7
snakahara7

asked on

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

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
Avatar of ozo
ozo
Flag of United States of America image

perl -ic:/destination/* -ple "BEGIN{chdir 'c:/source';@ARGV=<*>;$/=qq(\n);$\=qq(\r\n)}"
Avatar of snakahara7
snakahara7

ASKER

i tried it and it does go through all the files in the directory, but it doesn't do the conversion.  thanks.
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?
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

#!/usr/bin/perl
chdir 'c:/source';
$^I='destination/*';
@ARGV=<*>:
$^I=.bak;
while( <>  ){
  s/\n/\r\n/;
  print;
}
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...
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!!