如何查找只有两个级别的子目录



我得到了一个脚本,它列出了文件夹中的所有子目录,并将它们放在一个文件中:

dir "\teste$1" /a:d /s /b | sort>"C:folders.txt

效果如下:

\teste$1
\teste$1target1
\teste$1target1in
\teste$1target1out
\teste$1target2
\teste$1target2in
\teste$1target2out
\teste$1target3
\teste$1target3in
\teste$1target3out
\teste$2
\teste$2target1
\teste$2target1in
\teste$2target1out
\teste$2target2
\teste$2target2in
\teste$2target2out
\teste$2random_folder_without_in_subfolder

我真正需要的:

\teste$1target1
\teste$1target2
\teste$1target3
\teste$2target1
\teste$2target2

在这种形式下更好(如果可能的话)(分隔符:"|:"):

\teste$1target1|:\teste$1target2|:\teste$1target3|:\teste$2target1|:\teste$2target2
@ECHO OFF
SETLOCAL
SET "sourcedir=."
FOR /f "delims=" %%a IN ('dir /s /b /ad "%sourcedir%" '
  ) DO (
 FOR /f "tokens=3,4delims=" %%d IN ("%%a") DO IF "%%e"=="" (ECHO(%%a) ELSE GOTO secondway
)
:secondway
@ECHO off
SETLOCAL enabledelayedexpansion 
SET "sourcedir=."
SET "longline="
FOR /f "delims=" %%a IN ('dir /s /b /ad "%sourcedir%" '
  ) DO (
 FOR /f "tokens=3,4delims=" %%d IN ("%%a") DO IF "%%e"=="" (
  SET "longline=!longline!|:%%a"
 ) ELSE GOTO done2
)
:done2
SET "longline=!longline:~2!"
SET longline
FOR /f "tokens=1*delims==" %%a IN ('set longline') DO ECHO %%b
GOTO :EOF

您需要更改sourcedir的设置以适应您的情况。

包含对cmd具有特殊意义的字符的目录名称可能存在问题。

相关内容

  • 没有找到相关文章

最新更新