使用批处理从列表中删除重复的顶级文件夹名称(Delete duplicate top

编程入门 行业动态 更新时间:2024-10-24 01:48:08
使用批处理从列表中删除重复的顶级文件夹名称(Delete duplicate top-level folder names from list with batch)

我需要从文件夹列表中删除重复的文件夹名称。 当有超过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 /b

FWIW:我正在使用此命令生成列表,然后删除folder1,folder2等之前的路径

For /d %%G in (*) do dir /ad /on /s /b "%%G" >> folderlist.txt

I 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 /b

FWIW: 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\%%c

you 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

更多推荐

本文发布于:2023-07-05 09:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理   文件夹   名称   列表中   top

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!