Home > OS >  How can I make this perl command check for subfolders as well?
How can I make this perl command check for subfolders as well?

Time:08-08

I'm running Windows 10 and I have a batch file that changes the directory to the required location and a perl command that edits all text files in it by changing Enabled = 1 to Enabled = 0, but I can't figure out how to make the perl command check for subfolders.

@echo off
timeout 1 >nul 2>&1
cd /d D:
timeout 1 >nul 2>&1
cd "D:\MySettings"
timeout 1 >nul 2>&1
perl -wE "@ARGV = glob qq($ARGV[0]); $^I = qq(); while (<>) { s/Enabled =\K.*/ \x220\x22/g; print }" *.txt
Pause

CodePudding user response:

For this, I think it could be only done by perl commands, not in batch commands.

You can check here for more specific solutions

CodePudding user response:

For this to be done to files in subfolders, and (I presume) in sub-sub-folders, etc (recursively) -- you'd need to separate that list (glob) of all "entries" into files and folders. Edit the files, and then repeat the process in folders.

There are libraries for that, of course. The core File::Find is a good recommendation; however, since you need to edit each file in a simple manner let's look at Path::Tiny, which has another convenient method

If you insist on a command-line program ("one-liner")

perl -MPath::Tiny -wE"
    path(qq(.))->visit( sub { 
        my ($entry, $state) = @_; 
        return if not -T;
        path($entry)->edit_lines( sub { s/.../.../ } )
    }, 
    { recurse => 1 }
)"
  • Related