?
Solved

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

Posted on 2008-06-20
8
Medium Priority
?
1,607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This is about my first experience with programming Arduino.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Progress
Starting up a Project
Suggested Courses

743 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