我有一个文件夹,其中包含以下文件。
如何删除所有扩展名为.rst的文件(File1.rst, File2.rst, File3.rst, File4.rst, File5.rst),除了批处理文件中的 "index.rst "文件。
我已经试过了,但是没有用。
for /f "skip=1 delims=" %%i in ('D:\hfTools\Projects\Validation-Source\Docs\source /b "*.rst"') do @(if "%i" neq "index.rst" echo %i)
欢迎任何帮助 谢谢你的帮助。
下面是一个例子,说明如何使用该软件执行任务 ForFiles
命令。forfiles.exe
:
@%__AppDir__%forfiles.exe /P "D:\hfTools\Projects\Validation-Source\Docs\source" /M "*.rst" /C "%__AppDir__%cmd.exe /D /C If @IsDir==FALSE If /I Not @FName==0x22index0x22 Del /A /F @File"
Dir
命令。@Echo Off
SetLocal EnableExtensions
If Exist "D:\hfTools\Projects\Validation-Source\Docs\source\*.rst" (
PushD "D:\hfTools\Projects\Validation-Source\Docs\source" && (
For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D "*.rst" ^
^| %__AppDir__%findstr.exe /E /I /L ".rst" ^
^| %__AppDir__%findstr.exe /I /L /V /X "index.rst"'
) Do Del /A /F "%%G"
PopD
)
)
Where
命令。where.exe
:例子 批处理文件
@Echo Off
SetLocal EnableExtensions
For /F "EOL=? Delims=" %%G In ('%__AppDir__%where.exe ^
"D:\hfTools\Projects\Validation-Source\Docs\source":"*.rst" ^
^| %__AppDir__%findstr.exe /E /I /L /V "\index.rst"'
) Do Del /A /F "%%G"
你甚至可以将其作为单行 批处理文件:
@For /F "EOL=?Delims=" %%G In ('%__AppDir__%where.exe "D:\hfTools\Projects\Validation-Source\Docs\source":"*.rst"^|%__AppDir__%findstr.exe /EILV "\index.rst"')Do @Del /A/F "%%G"
或直接从a cmd 窗口。
For /F "EOL=?Delims=" %G In ('%__AppDir__%where.exe "D:\hfTools\Projects\Validation-Source\Docs\source":"*.rst"^|%__AppDir__%findstr.exe /EILV "\index.rst"')Do @Del /A/F "%G"
Remove-Item -Path "D:\hfTools\Projects\Validation-Source\Docs\source\*.rst" -Exclude "index.rst" -Force
你甚至可以用一行批处理文件来运行,如果你真的需要。
@%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoP "RI 'D:\hfTools\Projects\Validation-Source\Docs\source\*.rst' -E 'index.rst' -Fo"
没有测试过
forfiles /M *.rst /C "cmd /c if @file!=index.rst del @file"
解释:
/M *.rst # only consider *.rst
if ... # @file is the filename as found by forfiles
# != is this the correct way say "does not equal"?
其实你已经很接近了,只是你忘了把一些东西翻倍而已 %
-迹象,你错过了 dir
命令。
rem // Change to target directory, because `dir /B` only returns pure file names:
pushd "D:\hfTools\Projects\Validation-Source\Docs\source" && (
rem // Capture the output of `dir /B` and loop through the lines/files:
for /f "delims= eol=|" %%i in ('
dir /B /A:-D-H-S "*.rst"
') do @(
rem // Check file name against predefined exception:
if /I not "%%i"=="index.rst" (
rem // Actually delete the currently iterated file:
ECHO del "%%i"
)
)
popd
)
一旦你对输出感到满意, 去掉大写 ECHO
指挥 来实际删除文件。
请注意,该模式 *.rst
在 dir /B
命令行实际上是对这两个 长短文件名 (如果启用了后者),所以它实际上匹配的文件的扩展名是 始于 rst
(如果适用)。
如果要删除的文件总是符合以下模式 File*.rst
你甚至不需要 if
条件。
rem // Change to target directory, because `dir /B` only returns pure file names:
pushd "D:\hfTools\Projects\Validation-Source\Docs\source" && (
rem // Capture the output of `dir /B` and loop through the lines/files:
for /f "delims= eol=|" %%i in ('
dir /B /A:-D-H-S "File*.rst"
') do @(
rem // Actually delete the currently iterated file:
ECHO del "%%i"
)
popd
)
使用PowerShell就能轻松完成。如果你使用的是支持的Windows版本,PowerShell就可以使用。当你确信正确的文件将被删除时,请移除 -WhatIf
从命令。
Remove-Item -Path './*' -Include '*.rst' -Exclude 'index.rst' -WhatIf
你必须通过cmd.exe(.bat文件)脚本来完成。
powershell -NoLogo -NoProfile -Command ^
"Remove-Item -Path './*' -Include '*.rst' -Exclude 'index.rst' -WhatIf"