-1

I have a report that deposits files from a network (NTFS) folder into another specific network (NTFS) folder. Some of the files contain a random string of numbers in the file name (example filename: xxsample_export 2131.xml). I need to find and delete all files in the folder that contain numbers in the filename. I've tried using Findstr but no luck so far. :(

Edit: I've tried this, I'm incredibly new at findstr. Thanks for your assistance.

for /F %%F in (dir /A -D "%Path%\*.xml" | findstr  /R "[0-9]*.xml" do del %%F"    
Stephan
  • 53,940
  • 10
  • 58
  • 91
Wings Fan
  • 1
  • 1
  • 2

3 Answers3

2

Lame but quick (I thought del yourdir\*[0-9].xml would work but nooooo), and only works if you need flat deletion, otherwise look at aschipfl's solution using dir /B and findstr.

In a .bat file (yourdir is the directory you want to delete files from):

for %%n in (1 2 3 4 5 6 7 8 9 0) do for %%a in (yourdir\*%%n.xml) do del "%%a" 2>NUL
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Good idea, +1! However, this might cause error messages since you are modifying the directory content while iterating through it by `for`, as `for` does not enumerate all matching items in advance; reference also [this post](http://stackoverflow.com/q/31975093)... – aschipfl Jul 27 '16 at 13:17
  • When I say it's lame I try to keep up my promises :) Edited to mask errors. Not optimal, I admit – Jean-François Fabre Jul 27 '16 at 13:21
  • Unfortunately, this is deleting every file in the folder and not just the files with numbers. – Wings Fan Jul 27 '16 at 14:28
2

Here is a solution using findstr:

set "LOCATION=\path\to\files"
for /F "eol=| delims=" %%F in ('
    dir /B /A:-D "%LOCATION%\*.xml" ^
        ^| findstr /I /R "[0-9][0-9]*\.xml$"
') do (
    del "%LOCATION%\%%F"
)

This deletes all .xml files that contain numbers as the last part of their file names.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

I know this question is old, but just in case someone googles this, here is what i ended up doing:

For every file in in the current directory:

for %%n in (1 2 3 4 5 6 7 8 9 0) do (
for %%f in (*) do (echo %%f|findstr /i /L "%%n">nul
if errorlevel 1 (echo skip) else ( del "%%f") ) )

For every folder:

for %%n in (1 2 3 4 5 6 7 8 9 0) do (
for /d %%f in (*) do (echo %%f|findstr /i /L "%%n">nul
if errorlevel 1 (echo skip) else ( rmdir "%%f" /s /q) ) )

For every file inside every folder in the current directory:

for %%n in (1 2 3 4 5 6 7 8 9 0) do (
for /d %%f in ("%cd%\"*) do ( cd "%%f" &  for %%f in (*) do (echo %%f|findstr /L "%%n">nul
if errorlevel 1 (echo skip) else ( del "%%f") ) ) )