循环遍历每个子目录而不会更深入 - 批处理脚本

时间:2016-08-02 23:12:08

标签: loops batch-file directory subdirectory

我一直在寻找一段时间来查看是否可以创建一个进入子目录的脚本但停止,执行命令,然后离开目录而不进入任何子子目录。这是我到目前为止所得到的:

FOR /R "%cd%" %%G in (.) DO (
    cd %%G
    echo %%G
    cd ..
)

但问题是它只是不停地浏览每个子目录,所以它看起来像这样:

now in C:\Users\JacobGunther12\Desktop\Batch GMA Converter\(gdcw)_usp_.45_silenced_300572504\models\.
now in C:\Users\JacobGunther12\Desktop\Batch GMA Converter\(gdcw)_usp_.45_silenced_300572504\models\weapons\.
now in C:\Users\JacobGunther12\Desktop\Batch GMA Converter\(gdcw)_usp_.45_silenced_300572504\sound\.
now in C:\Users\JacobGunther12\Desktop\Batch GMA Converter\(gdcw)_usp_.45_silenced_300572504\sound\impacts\.
now in C:\Users\JacobGunther12\Desktop\Batch GMA Converter\(gdcw)_usp_.45_silenced_300572504\sound\weapons\.
now in C:\Users\JacobGunther12\Desktop\Batch GMA Converter\(gdcw)_usp_.45_silenced_300572504\sound\weapons\usp45\.

而不只是从文件夹"批量GMA转换器"进入第一个子目录。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

遍历从dir /B /AD获取的子目录的静态列表,如下所示:

@ECHO OFF
SETLOCAL EnableExtensions
set "curdir=%CD%"
FOR /F "delims=" %%G in ('dir /B /AD') DO (
    pushd "%curdir%\%%G"
    echo %curdir%\%%G
    popd
)