我需要一个 bat 来删除所有不包含在文本文件中的具有相对名称的文件
在文本文件 list.txt 我有这个:
C:\S-ATLANTICO-1\MEDIA\Innplay-Logo.mp4
C:\S-ATLANTICO-1\MEDIA\logo-FB_sep.png
C:\S-ATLANTICO-1\MEDIA\logo-news_sa.png
并且在同一个文件夹中有这个文件:
Innplay-Logo.mp4
logo-FB_sep.png
logo-news_sa.png
Carlos.jpg
Sapo.png
list.txt
所以我需要删除下一个文件,因为 list.txt 中不存在
Carlos.jpg
Sapo.png
但我也必须保留 LIST.TXT
我试过这个,但没有成功
@echo off
setlocal
set "folder=C:\S-ATLANTICO-1\MEDIA"
set "excludeFile=C:\S-ATLANTICO-1\MEDIA\list.txt"
for /f "eol=: delims=" %%F in ('dir /b /a-d "%folder%" ^| findstr /vig:"%excludeFile%" ^| findstr /v /i "\list.txt"') do del "%folder%\%%F"
任何人都可以帮助我解决这个问题。
谢谢
试一试。它适用于我的测试。
我已经放置了echo语句,而不是实际删除任何内容。
@echo off
setlocal
set "folder=C:\S-ATLANTICO-1\MEDIA"
set "excludeFile=%folder%\list.txt"
:: Check that both the target folder and filter file exist before starting up.
if not exist "%folder%" echo %~nx0: The target folder doesn't exist. Nothing to do.&& goto :EOF
if not exist "%excludeFile%" echo %~nx0: The list file doesn't exist at the location specified!&& goto :EOF
for /f "delims=" %%F in ('dir /b /a-d "%folder%"') do call :process_file "%%F" "%~0" "%excludeFile%"
goto :EOF
:: --------------------------------------------
:process_file
:: --------------------------------------------
set input_file=%~1
set this_batch=%~2
set list_file=%~nx3
:: Skip list file and this batch file too
if "%this_batch%"=="%input_file%" echo Skip this batch file&& goto :EOF
if "%list_file%"=="%input_file%" echo Skip list file&& goto :EOF
:: Grep for the include file in the list
findstr /C:"%input_file%" "%excludeFile%" 2>&1 1>NUL
:: Bail out if the input line was in the list file
if not errorlevel 1 echo Skip "%input_file%", it is in %list_file%&& goto :EOF
:: Delete anything left
echo delete file %input_file%&& goto :EOF
goto :EOF
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句