我需要从文件夹列表中删除重复的文件夹名称。 当有超过1个子文件夹时,会出现重复项。 我最终得到如下列表。 我想摆脱任何有sub2文件夹的行。
文件夹1 \ SUB1 文件夹2 \ SUB1 文件夹2 \ SUB1 \ SUB2 folder3 \ SUB1
下面的代码可以工作,如果只有一个sub2 foldername,但它很尴尬 - 如果不止一个sub2,则无望。 必须有一个更好的方法。 任何帮助非常感谢。
@Echo off SETLOCAL EnableDelayedExpansion :: Write the sub2 foldernames to a tmp file For /f "tokens=3 delims=\" %%I IN (folderlist.txt) DO Echo %%I >>temp.tmp :: Set var for each sub2 name in tmp file and :: call routine to write lines that don't contain that name For /f %%G in (temp.tmp) do ( Set findstring=%%G CALL :FindDup ) EXIT :findDup For /f %%H in ('Type folderlist.txt ^|Find "!findstring!" /v') Do ( Echo %%H >> NoDup.txt ) exit /bFWIW:我正在使用此命令生成列表,然后删除folder1,folder2等之前的路径
For /d %%G in (*) do dir /ad /on /s /b "%%G" >> folderlist.txtI need to delete duplicate folder names from a folder list. The duplicates occur when there's more than 1 subfolder. I end up with a list like below. I want to get rid of any line that has a sub2 folder.
folder1\sub1 folder2\sub1 folder2\sub1\sub2 folder3\sub1
Following code works if there is only one sub2 foldername, but it's awkward--hopeless if more than one sub2. There's gotta be a better way. Any help much appreciated.
@Echo off SETLOCAL EnableDelayedExpansion :: Write the sub2 foldernames to a tmp file For /f "tokens=3 delims=\" %%I IN (folderlist.txt) DO Echo %%I >>temp.tmp :: Set var for each sub2 name in tmp file and :: call routine to write lines that don't contain that name For /f %%G in (temp.tmp) do ( Set findstring=%%G CALL :FindDup ) EXIT :findDup For /f %%H in ('Type folderlist.txt ^|Find "!findstring!" /v') Do ( Echo %%H >> NoDup.txt ) exit /bFWIW: I'm using this command to generate the list, then deleting the path preceding folder1, folder2, etc
For /d %%G in (*) do dir /ad /on /s /b "%%G" >> folderlist.txt最满意答案
你几乎就在那里,如果你只想在删除子文件夹后得到结果列表,只需尝试将适当的行回显到列表文件,先将它复制到临时文件中。
move folderlist.txt %temp%\folders.txt for /f "tokens=1,2,* delims=\" %%a in (%temp%\folders.txt) do ( if .%%c==. echo %%a\%%b >>folderlist.txt )如果要从磁盘中删除该文件夹,请将该行更改为
if not .%%c==. rd /s %%a\%%b\%%cyou are almost there, if you just want the resulting list after eliminating the subfolders, just try to echo the appropiate lines to the list file, having copied it first into a temporary.
move folderlist.txt %temp%\folders.txt for /f "tokens=1,2,* delims=\" %%a in (%temp%\folders.txt) do ( if .%%c==. echo %%a\%%b >>folderlist.txt )if you want to remove the folder from the disk, then change the line to
if not .%%c==. rd /s %%a\%%b\%%c更多推荐
发布评论