gpt4 book ai didi

batch-file - 批处理文件删除文件夹

转载 作者:行者123 更新时间:2023-12-03 06:24:25 24 4
gpt4 key购买 nike

我找到了一些删除文件夹的代码,在本例中删除了除“n”个文件夹之外的所有文件夹。我创建了 10 个测试文件夹,加上 1 个已经存在的文件夹。我想删除除 4 之外的所有内容。该代码有效,它留下了 4 个测试文件夹,除了它还留下了另一个文件夹。在批处理文件中检查的其他文件夹是否有某些属性阻止其被删除?它是几周前通过一项工作创建的。

这是我窃取的代码(但不太了解细节):

rem DOS - Delete Folders if # folders > n
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=4
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\MyFolder_Temp\FolderTest
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tf}1{" (
Echo %%I:%%~fI >>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
Echo Date Name>>"%temp%\tf}1{"
Echo %%I:%%~fI >>"%temp%\tf}1{"
))
PopD
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
Goto _Removeold
Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"

最佳答案

For/F 行是关键。它获取单引号中的该行部分的结果,并对其进行迭代。

因此,它获取目标目录中文件的目录列表,根据 _NumtoKeep 变量排除前 n 行,并显示结果。

然后,它会使用 Set _rdflag=/q 行提出问题,确认操作,然后再进行工作。

关于batch-file - 批处理文件删除文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1773454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com