我需要一个蝙蝠才能删除文本文件中未包含的所有具有相对名称的文件
在文本文件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"
任何人都可以帮助我。
谢谢
答案 0 :(得分:0)
试一下。它适用于我的测试。
我放置了 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
答案 1 :(得分:0)
尝试一下。它遍历$thedir
的内容,并删除不在list.txt
中并且不是脚本的文件。将这两个(2)文件放在同一目录中。最好不要将它们与要删除的文件放在同一目录中。从cmd.exe Shell运行.bat文件。
当您满意要删除正确的文件后,请从-WhatIf
cmdlet中删除Remove-Item
。
=== Remove-Except.ps1
$thedir = 'C:\src\t\delexcept'
$precious = (Get-Content -Path (Join-Path -Path $thedir -ChildPath 'list.txt')) +
@(Join-Path -Path $thedir -ChildPath 'list.txt')
Get-ChildItem -Path $thedir -Exclude 'list.txt','*.ps1','*.bat' |
Where-Object { (-not $_.PSIsContainer) -and
($precious -notcontains $_.FullName) -and
$_.FullName.StartsWith($thedir) } |
ForEach-Object { Remove-Item -Path $_.FullName -WhatIf }
=== Remove-Except.bat
powershell -NoLogo -NoProfile -File "%~dpn0.ps1"
答案 2 :(得分:0)
在cmd行上应该这样做
for /f "delims=" %A in ('Dir /B /A-D ^|find /v /i "list.txt"') Do @findstr /IL "%~nxA" list.txt >Nul ||(echo del %A)
如果list.txt的最后一行末尾有cr / lf,我将在findstr中加入/E
开关,
否则最后一个条目将与reference不匹配。
如果输出看起来正常,请删除del前面的回声。
作为批处理文件:
@Echo off
set "folder=C:\S-ATLANTICO-1\MEDIA"
PushD "%folder%"
set "excludeFile=list.txt"
for /f "delims=" %%A in ('
Dir /B /A-D ^|find /v /i "%excludefile%"'
) Do findstr /IL "%~nxA" "%excludefile%" >Nul ||(
echo del %%A
)
Popd