0

I have a python script called script.py which I would like to run on all of the directories inside of a specific directory. What is the easiest way to do this using batch scripting?

My assumption is some usage of the FOR command, but I can't quite make any leads to solve the problem using the command...

2 Answers 2

3

Use the /d operator on for:

for /D %%d in (*) do script.py %%d

This assumes you only need to execute one command per directory (script.py %%d) if you need to execute more use braces (). Also I'm guessing there's an execution engine needed first, but not sure what it is for you.

A multi-line example:

for /D %%d in (%1) do (
   echo processing %%d
   script.py %%d
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Works perfectly. I assume the * represents current directory. Not sure what you mean by use more () to execute more than one command. But not necessary for what I'm working on.
Yup * represents current (well it's a file-filter match), you could also pass in the directory as an arg (change * into %1 and call as <whatever> \windows\* - Added a multi line and arg example.
0

Building upon Rudu's answer, and assuming script.py is located in the same directory (say C:\Test) as the following batch script, you could also run script.py recursively on all the directories present in C:\Test:

@echo off
for /d /r %%d in (*) do (
  echo Processing: %%d
  script.py "%%d"
)

The FOR help available from cmd.exe does not seem to mention it supports multiple modifiers, but it works on Windows 7.

3 Comments

Could you alter this script slightly to clean a directory (remove all files, but leave directory structure)
To delete all files recursively from a directory leaving the directory structure intact: del /s /q /f "C:\Test Area\Target" (/s walks the directory tree,/q won't prompt you for confirmation, /f will delete read-only files).
For some reason @BoReal answer is giving me some weird behaviour with TortoiseSVN the file that I ran the clean (outputfolder) says it is "obstructed" afterwards (the output folder and subdirectories are under source control). if i perform a tortoise clean directory, it says that the folder is not a working copy directory. My guess: Deleting files inside the hidden .svn folder??? Sounds correct to me...

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.