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.
this is my file.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
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 :)
CodePudding user response:
You can give a try for this batch script and if it's OK you can remove the ECHO(
before Del
command :
@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
::----------------------------------------------------------------------------------