[英]Batch File to Delete all Matching Files from All Folders then Save it in a Log.txt
I want to create a Batch File that will delete a File that has any of the two names such as "new" or "1234" of any file format.我想创建一个批处理文件,该文件将删除具有任何文件格式的两个名称中的任何一个的文件,例如“新”或“1234”。
this is my file.txt:这是我的文件.txt:
new
1234
this is what I want to happen:这就是我想要发生的事情:
I already tried some of the codes I found online but It deleted some of my files.我已经尝试了一些我在网上找到的代码,但它删除了我的一些文件。 So I'm scared to re-run codes that has such WildCards hehe.因此,我害怕重新运行具有此类通配符的代码,呵呵。
So far I have this:到目前为止,我有这个:
SET FILENAME=DeleteLog.txt
SET USERLOG=%USERPROFILE%\%FILENAME%
rem I just declared the Downloads for safety-purposes but it should be C:\
SET TARGET1=%USERPROFILE%\Downloads
echo STARTING THE FILE DELETION >>%USERLOG%
cd %TARGET1%
rem file.txt contains the string (new,1234)
for /f "delims=" %%a in ("file.txt") do echo del %TARGET1%*%%a* >>%USERLOG%
echo FINISHING THE FILE DELETION >>%USERLOG%
My Logic is: For every file(new,1234).txt on my C:\ Drive -> Delete -> Log我的逻辑是:对于我的 C:\ Drive -> Delete -> Log 上的每个文件(new,1234).txt
I'm trying to follow this but it does not work for me: Batch file to delete files with relative name contained in text list?我正在尝试遵循这一点,但它对我不起作用: 批处理文件删除文本列表中包含的相对名称的文件? What seems to be the problem?似乎是什么问题?
Any tips and suggestion will be appreciated:)任何提示和建议将不胜感激:)
You can give a try for this batch script and if it's OK you can remove the ECHO(
before Del
command:您可以尝试使用此批处理脚本,如果可以,您可以删除ECHO(
在Del
命令之前:
@echo off
Title Delete some files with pattern *123* or *new*
Set "CurrentFolder=%~dp0"
Set "Pattern=*123* *new*"
cd /d "%CurrentFolder%"
::---------------------------------------------------------------------------------
:Main
cls
setlocal enableDelayedExpansion
REM Count total files and store into array
set /a "Count=0"
@for %%f in (%Pattern%) do (
set /a "Count+=1"
set "list[!Count!]=%%f"
)
REM disply files
@for /L %%a in (1,1,%Count%) do (
echo( [%%a] - "!list[%%a]!"
)
Echo( -----------------------------------------------------------------------------------------
Echo( Total pattern files found are equal to %Count% on this folder : "%CurrentFolder%"
Echo( -----------------------------------------------------------------------------------------
echo(
If [%Count%] EQU [0] color 0C & echo( There are No Files that matches this %Pattern% in this Folder "%CurrentFolder%" & TimeOut /T 10 /NoBreak>nul & Exit /B 1
echo( Type "DELAll" in order to delete all files found here "%CurrentFolder%" with this "%Pattern%" ...
echo( Or Type only the number of file that you want to delete ...
set /p "Input="
@For /L %%i in (1,1,%Count%) Do (
If "%INPUT%" EQU "%%i" (
Cls & Call :DeleteFile "!list[%%i]!"
)
If /I "%Input%" == "Delall" (
cls & Call :DeleteFile "!list[%%i]!"
)
)
EndLocal & Goto Main
::---------------------------------------------------------------------------------
:DeleteFile <FilePath>
echo( ----------------------------------
Echo( Deleting "%~nx1"
ECHO( Del "%~1"
echo( ----------------------------------
Timeout /T 1 /NoBreak>nul
Exit /b
::----------------------------------------------------------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.