我有一个脚本列出了文件夹中的所有子目录并将它们放在一个文件中:
dir "\\test\e$\1" /a:d /s /b | sort>"C:\folders.txt
效果看起来像这样:
\\test\e$\1
\\test\e$\1\target1
\\test\e$\1\target1\in
\\test\e$\1\target1\out
\\test\e$\1\target2
\\test\e$\1\target2\in
\\test\e$\1\target2\out
\\test\e$\1\target3
\\test\e$\1\target3\in
\\test\e$\1\target3\out
\\test\e$\2
\\test\e$\2\target1
\\test\e$\2\target1\in
\\test\e$\2\target1\out
\\test\e$\2\target2
\\test\e$\2\target2\in
\\test\e$\2\target2\out
\\test\e$\2\random_folder_without_in_subfolder
我真正需要的是:
\\test\e$\1\target1
\\test\e$\1\target2
\\test\e$\1\target3
\\test\e$\2\target1
\\test\e$\2\target2
这种形式更好(如果可能)(分隔符:" |:"):
\\test\e$\1\target1|:\\test\e$\1\target2|:\\test\e$\1\target3|:\\test\e$\2\target1|:\\test\e$\2\target2
答案 0 :(得分:0)
@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
具有特殊含义的字符。