tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to automate my edit-many-files work here with perl, maybe with the dos command line. I don't need backup files because I have everything under version control already.

First question is, can I get this working a slick one liner from dos (mainly for perl missionizing around the office :) ). Second is, if there's a way to do this without a backup file?

The usual tactic for inplace many files is the "perl dash pie" trick,  perl -p -i -e "s/change this/to this/g" *, but this appears to be only for nix. I haven't been able to get * to work as "all files" from the dos command line. Is there a way to get this to work as a one-liner from dos?

Abandoning the one liner idea for the time being, I found two very helpful merlyn posts at My pattern for "in-place edit" for many files and Shell trick to edit many files with perl (in reply to another post). Like the perl pie trick though, these leave backup files.

Any way to adopt the above scripts without leaving those .bak breadcrumbs?

Replies are listed 'Best First'.
Re: Inplace editing, one liner, without a backup file?
by Fletch (Bishop) on Aug 09, 2005 at 12:34 UTC

    This came up the other week. It's basically a limitation of Windows (SURPRISE!) that Perl has to make a named backup file. See One-liner: -i without .bak?.

    --
    We're looking for people in ATL

      Thanks fletch. From reading the post you linked to, I also conclude that in place edit on multiple files with the "perl pie trick" works better from the cygwin command line than from the dos command line, where I get
      C:\thomasdata\thomasprojects\fixHtml\t\structural\beginning_includes>p +erl -p -i -e "s/this/that/" *.* Can't open *.*: Invalid argument.
      Well, one more reason to install cygwin I suppose, something I've been meaning to toy with for awhile. But even with cygwin, you automagically get .bak no matter what, if I understood that thread correctly.
        workaround:
        for %f in (*.*) do perl -pi.bak -e "s/this/that/" %f & del %f.bak


        holli, /regexed monk/
Re: Inplace editing, one liner, without a backup file?
by salva (Canon) on Aug 09, 2005 at 12:37 UTC
    maybe a silly question but, is there anything disallowing you to remove the backup files after editing them with the one-liner? del *.bak?

    And btw, to select all files under MS-DOS you have to use the *.* pattern.

      Actually I'm removing them in subversion, on commit, with the pretty tortoisesvn gui interface. (I select all the .bak files and delete).

      I couldn't get the pie trick to work from dos with or without the *.*. Am I doing something wrong? (See my reply to fletch.)

        Unlike the shells found on most Unix-like operating systems - the Windows command prompt ( and the MS-DOS command.com before it) does not expand the "wildcard" parameters before passing them as arguments to a program - it is left to the program itself to do that.

        /J\

Re: Inplace editing, one liner, without a backup file?
by davidrw (Prior) on Aug 09, 2005 at 12:52 UTC
    if for some reason it's now * vs *.* that's the problem, or you can't just del *.bak at the end due to size constraints, you can loop in a batch file, working with one file at a time so there's never more than 1 .bak file present (the self-calling is because the FOR/DO only allows for running a single command):
    REM foo.bat @echo OFF IF "%1" == "process" GOTO processFile GOTO convertAll :convertALL FOR %%f IN (c:\temp\*.txt) DO CALL %0 process "%%f" GOTO end :processFile SET in=%2 perl -p -i -e "s/change this/to this/g" %in% del %in%.bak GOTO end :end
    Update: Oops. fixed the error in variable name in ":processFile" .. also took advantage of "%0" .. thanks ikegami!
      Seems extremely useful (more for the multi file replace than for the deletion of the bak files), but when I run it it just hangs.

        He made a small error. He was using %f% where he needed to use %in%. ...or simply %2. Also, I changed foo.bat to %0.

        @echo off IF "%1" == "process" GOTO processFile GOTO convertAll :convertALL FOR %%f IN (c:\temp\*.txt) DO CALL %0 process "%%f" GOTO end :processFile perl -p -i.bak -e "s/change this/to this/g" %2 del %2.bak GOTO end :end

        If you really wanted to name the parameter, try the following. It cleans up the environment as a bonus (by spawning another shell where CALL was formerly used).

        @echo off IF "%1" == "process" GOTO processFile GOTO convertAll :convertALL FOR %%f IN (c:\temp\*.txt) DO cmd /c %0 process "%%f" GOTO end :processFile SET IN=%2 perl -p -i.bak -e "s/change this/to this/g" %IN% del %IN%.bak GOTO end :end
        </c>