batch-file - Delete files based on partial names in a text file
问题描述
I would like to delete all .mp3
files in C:\temp
which start with that defined witin a txt file,Mytxtfile.txt
.
For example, C:\temp
contains:
AliciaEffort.mp3
AliciaKlay.mp3
AliciaLow.mp3
Emanlw.mp3
EmanDown.mp3
JonKLa.mp3
JohnHd.mp3
Mytxtfile.txt
contains:
AliciaEffor
Emoanlw
GrezyK
JohnHd
etc.
My code:
set WORKDIR="C:\temp"
set LISTFILES="C:\Users\StevenRg\Desktop\mytxtfile.txt"
pushd %WORKDIR%
for %%G in (*) do (
for /f "tokens=* usebackq" %%H in (%LISTFILES%) do (
if %%G==%%H del /p "%%G"
)
)
popd
But it doesnt work ... Any ideas ?
解决方案
You could probably do it like this:
@Echo Off
Set "WorkDir=C:\temp"
Set "ListFile=%UserProfile%\Desktop\mytxtfile.txt"
CD /D "%WorkDir%" 2>Nul || Exit /B
If Not Exist "%ListFile%" Exit /B
For /F "UseBackQ Delims=" %%A In ("%ListFile%") Do Del /A /F /Q "%%A*.mp3" 2>Nul
Optionally, it may be safer to replace the last line with this:
For /F Delims^=^ EOL^= %%A In ('Type "%ListFile%"') Do Del /A /F /Q "%%~A*.mp3" 2>Nul
推荐阅读
- angular - Asp.net core 3.1 + Angular 10 使用多个 razor (.cshtml) 页面
- javascript - 通过 github api 获取私有 repo 分支
- kdb - (KDB/Q+) 更新简单表的前 N 行
- javascript - 正则表达式允许长度限制为 5 的所有字符?
- css - 当每个 line-height 不同时,如何在 flexbox 中垂直居中文本?
- c - 这个 if 语句对这些嵌套循环的时间复杂度有什么影响?
- html - 如何在输入中放置锚标记
- javascript - for循环检查所有计数在一个循环中反应原生
- perl - Perl ) 进入和退出每个子目录
- apache-spark - 在pyspark中加载超过54位小数的double类型时如何避免精度损失