简体   繁体   中英

How to execute a windows batch command recursively?

例如,批处理文件中有重命名命令,并且您希望在当前目录和所有子目录上执行该文件。

Suppose your batch is named something like myrename.cmd , then you can easily do the following:

call myrename.cmd
for /r /d %%x in (*) do (
    pushd "%%x"
    call myrename.cmd
    popd
)

The first line will run it for the current directory, the for loop will iterate recursively ( /r ) over all directories ( /d ) and execute the part in the parentheses. What we do inside them is change the directory to the one we're currently iterating over with pushd —which has the nice property that you can undo that directory change with popd —and then run the command, which then will be run in the directory we just switched to.

This assumes that the batch lies somewhere in the path. If it doesn't and just happens to lie where the batch file above lies, then you can use

"%~dp0myrename.cmd"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM