Good morning Monks,

I have a large folder of text files, and I want to substitute all instances of a certain string within those files with another string (essentially every time the word “end” appears, I want to start a new line).

To do this, I thought it would be safer to use a script that copies the files to a new folder before it modified them. After a bit of Googling, I found a Perl script (created by durden_tyler and uploaded to https://www.unix.com/302318333-post5.html) which, with a couple of modifications, allows me to do just this.

#!perl -w # Script to process all *.txt files in current directory and put the o +utput # in the corresponding *.txt.new files in the current directory again. my $indir = 'C:\Users\li\test'; # the input directory name my $outdir = 'C:\Users\li\test2'; # the output directory name my $i = 0; # loop index my $infile; # the input file name my $outfile; # the output file name while (defined($infile = glob($indir."\\*.txt"))) { # loop + through the txt files in directory $indir printf("(%d)\tNow processing file => %s\t",++$i,$infile); $outfile = $outdir."\\".substr($infile,rindex($infile,"\\")+1); # n +ame the output file open (IFILE, "<$infile") or die "Can't open $infile: $!"; # open + input file for reading open (OFILE, ">$outfile") or die "Can't create file: $!"; # crea +te output file for writing while (<IFILE>) { # loop through contents of the input file my @lines = <IFILE>; my @newlines; foreach(@lines) { $_ =~ s/<\/end>/<\/end>\n/g; push(@newlines,$_); print OFILE $_; } close(IFILE) or die "Can't close $infile: $!"; # clos +e input file close(OFILE) or die "Can't close $outfile: $!"; # clos +e output file printf("Output file => %s\n",$outfile); } }

The only problem is that this process (obviously) alters the original “date modified” Windows timestamps (that are displayed in File Explorer) for the files. This is unfortunate because the timestamps, at the moment, are one of the key ways that I’m keeping track of the large amount of data I’ve got.

Is there any way that I can still substitute the string within each file without losing the original date modified timestamp?

Thanks a lot!


In reply to Maintaining date modified timestamp when copying files by Maire

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.