How to find subdirectories of only two levels deep?

房东的猫 提交于 2019-12-13 04:47:52

问题


I got a script that lists all subdirectories in a folder and put them in a file:

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

effect looks like this:

\\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

what I really need:

\\test\e$\1\target1
\\test\e$\1\target2
\\test\e$\1\target3
\\test\e$\2\target1
\\test\e$\2\target2

even better (if possible) in this form (separator: "|:"):

\\test\e$\1\target1|:\\test\e$\1\target2|:\\test\e$\1\target3|:\\test\e$\2\target1|:\\test\e$\2\target2

回答1:


@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

You would need to change the setting of sourcedir to suit your circumstances.

May have problems with directorynames containing characters that have a special meaning to cmd.



来源:https://stackoverflow.com/questions/33936340/how-to-find-subdirectories-of-only-two-levels-deep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!