0

I would like to know that how can a BATCH file be configured to first run a python script: example.py (to do a find and replace) and then run the script on multiple files in sub folders with in a root folder.

Also the files edited/changed in the sub folders should be deleted and saved with the new changed file.

Your help will be much appreciated.

Thanks in advance.

1 Answer 1

0

You could start with something like this:

for /r [[drive:]path] %a in (*.txt) do example.py "%a"

this executes example.py for every .txt file, starting from [[drive:]path], and recursing subdirectories /r. If you omit drive:path it will start the search from the current directoty, e.g.

for /r %a in (*.html) do example.py "%a"
for /r C:\ %a in (*.html) do example.py "%a"

The first example executes example.py for every *.html file under current dir, the second one executes example.py for every .html file under C:\.

It is also possible to delete changed files, but we need to have some more information. Maybe you just need this?

for /r %a in (*.txt) do example.py "%a" temp.tmp & copy temp.tmp "%a"

Remeber that this works if you launch this command at the command prompt. If you want to use it inside a batch file (.cmd, .bat) you have to use %%a instead of %a.

9
  • for /r . %a in (*.txt) do example.py "%a" So the . in the script after /r represents the root folder directory ?
    – KingMak
    Commented Nov 19, 2012 at 22:23
  • It represents the folder where to start the search...you could use C:\ or any other path if you wish...
    – fthiella
    Commented Nov 19, 2012 at 22:32
  • So I changed the .txt to .html and the inserted the file path and the script name but i got an error: and was unexpected at this time. Please help me.
    – KingMak
    Commented Nov 19, 2012 at 22:44
  • How do you run this command? In a batch file or from the prompt? Does your root path contain any space?
    – fthiella
    Commented Nov 19, 2012 at 22:50
  • 1
    @fthiella: The dot is never necessary in for /r command and certainly not used. Surely you will be proud when this uncommon form of FOR command start to spread in Batch file beginners community, as I said you before... :-(
    – Aacini
    Commented Nov 20, 2012 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.