Solved delete files which start with numbers batch

March 16, 2016 at 09:32:28
Specs: Windows 7
I am trying to delete image files which are start with numbers and dash and for that I implemented this:
@echo off
robocopy c:\images c:\images2 /MIR /XA:H /W:0 /R:1 /REG /FFT /XN /XC /XO
@echo off
cd c:\images2
del \ -*.JPG /q

cd c:\images2
del ^#*.JPG /q

I can copy data from one folder to another and also delete files which are start with dash but couldn't delete files which are start with numbers.

Does anyone has idea about this?
Please let me know asap.


message edited by RV16

See More: delete files which start with numbers batch

Report •

March 16, 2016 at 20:04:10
this: ^#*.JPG /q
is sort of a reg.exp which CMD/DEL does not handle. You need a filter to submit the filenames to the DEL. Findstr works most of the time:
::========= begin batchscript
@echo off>delset.bat & setlocal enabledelayedexpansion
For /f "tokens=*" %%a in ('dir /b *.jpg ^| findstr /i /r "^[0-9]*-.*"') do (
>>delset.bat echo del "%%a"
::enable the next line for the sub-bat execution
rem call delset.bat

message edited by nbrane

Report •

March 16, 2016 at 21:01:59
✔ Best Answer
:: To turn off safety edit out the echo before "del"
::======  script starts here  ===============
:: delete jpg starting with #-
:: RV16.bat  2016-03-17 10:44:28.95
@echo off & setLocal enableDELAYedeXpansioN

:: pushd to the dir where your files are

for /f "tokens=* delims= " %%i in ('dir/b *.jpg') do (
set J=%%i
set firsttwo=!J:~0,2!
  for /L %%x in (0 1 9) do (
    if %%x- equ !firsttwo! echo del "!J!"
::======  script ends here  =================


M2 Get custom script or take private lessons

Report •

March 17, 2016 at 06:50:21
Are we all posting our versions of the batch? 'Cause I also thought up a different way.
robocopy c:\images c:\images2 /MIR /XA:H /W:0 /R:1 /REG /FFT /XN /XC /XO
for /f "delims=" %%a in ('dir /b c:\images2\*.jpg ^| findstr "^[0-9] ^-"') do @del "c:\images2\%%~a"

How To Ask Questions The Smart Way

message edited by Razor2.3

Report •

Related Solutions

Ask Question