1

I've a perl file changes.pl already. I can run changes.pl via cmd command. I'm looking for disabling saving backup files option. I've tried cmd command 1 and 1a. But, I get result from running any of both.

changes.pl

BEGIN {
    @ARGV = map glob("\"$_\""), @ARGV;
}

s/a/b/g;
s/c/d/g;
s/e/f/g;

cmd command

perl -i.bak -p changes.pl My/Files/Directory/*.txt

cmd command 1

perl -i -p changes.pl My/Files/Directory/*.txt

cmd command 1a (back-slash version of cmd command 1)

perl -i -p changes.pl My\Files\Directory\*.txt

Result

I get nothing at all. The cmd command doesn't terminate even.

Note:

  • I'm using Perl on Windows.
  • I've gotten changes.pl and cmd command from Borodin's answer on a previous question of mine.
Community
  • 1
  • 1
Omar
  • 6,681
  • 5
  • 21
  • 36
  • Does it do anything at all with command 1? Might the forward slashes in your path on Windows be a problem? Try using backslashes instead. – simbabque Sep 09 '15 at 13:36
  • @simbabque I've tried the forward slashes. I get nothing at all for both back and forward slashes versions. The cmd command doesn't terminate even. – Omar Sep 09 '15 at 13:45
  • Maybe there are a lot of files and it takes really long? – simbabque Sep 09 '15 at 13:47
  • What is that backslash-doublequote thing supposed to do? What's wrong with plain `@ARGV = map glob, @ARGV;` – choroba Sep 09 '15 at 13:47
  • @simbabque No, I test on three files only. Also **cmd command** works fine and quickly. But it creates backup files. – Omar Sep 09 '15 at 14:02
  • @choroba Glob patterns are split on whitespace: `perl -E'say for map glob, @ARGV' "foo bar"` This code handles paths containing whitespace. – ThisSuitIsBlackNot Sep 09 '15 at 14:11
  • Also see [Perl on windows wont let me replace contents inside a file](http://stackoverflow.com/q/25248853/176646) – ThisSuitIsBlackNot Sep 09 '15 at 14:25

1 Answers1

3

In-place editing works slightly different on Windows. On a *nix system you can leave off the ext from the -i switch and the backup file will be unlinked after the edit. But, on Windows the ext is required and therefore the backup file is retained. On Windows, you'll need to manually delete the backup(s) after the edit.

Ron Bergin
  • 1,070
  • 1
  • 6
  • 7
  • 5
    Might be worth adding - this is because of a limitation in Windows which doesn't exist in Unix - in Unix you can open a file, then delete/rename - and because it's open, the data still exists and is readable (until you close it). On Windows this doesn't apply - the file once opened cannot be overwritten/deleted until it's closed. – Sobrique Sep 09 '15 at 14:41