删除具有特定扩展名的文件(特定文件除外)

时间:2020-06-15 16:45:54

标签: windows batch-file cmd delete-file

我有一个包含以下文件的文件夹:

enter image description here

如何从批处理文件中删除所有扩展名为.rst的文件(File1.rst,File2.rst,File3.rst,File4.rst,File5.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)

任何帮助都将受到欢迎。谢谢。

4 个答案:

答案 0 :(得分:1)

以下是如何使用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"

或直接在窗口中:

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"

答案 1 :(得分:0)

未经测试:

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"?

答案 2 :(得分:0)

您实际上已经很亲密,只是忘记加倍几个total :: (Integer -> Integer) -> (Integer -> Integer) total f n = sum map (f) [1..n] 符号,而错过了%命令:

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实际上是针对两个long and short file names(如果启用了后者)进行检查的,因此它实际上与扩展名为 begin的文件匹配 dir /B(如果适用)。


如果要删除的文件始终与模式rst匹配,则您甚至不需要File*.rst条件:

if

答案 3 :(得分:0)

使用PowerShell可以轻松完成此操作。如果您使用的是受支持的Windows版本,则可以使用PowerShell。如果您确信将删除正确的文件,请从命令中删除<input type="text" name="newDate" value={newDate} className="datepicker" placeholder="Meeting Date" onChange={handleInputChange} /> <input style={{ width: '40%', marginRight: '10%' }} type="text" required name="startTime" value={startTime} placeholder="00:00" onChange={handleInputChange} className="timepicker" placeholder="Start Time" />

-WhatIf

您必须通过cmd.exe(.bat文件)脚本执行此操作:

Remove-Item -Path './*' -Include '*.rst' -Exclude 'index.rst' -WhatIf