1.

Solve : Delete files - help?

Answer»

I have a files :

e:\data\2013-10-28-11-43-56.csv - 19-characters NAME
e:\data\2013-10-28-11-43-56_data.csv - 24-characters name

When i run in e:\data directory command : Code: [Select]del ???????????????????.csv (19 question marks, because i want to delete only 19-characters name FILE) both files becomes delete. Why ? I don't understand. Could anybody explain it ?
I have never worked with the '?' but you could do this:

echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%A in ('dir /b') do (
set a=%%~nA
if "!a:~19,1!"=="" echo %%A
)
set a=


It will delete anything with 19 or less characters, so you have to name it something long.Quote from: Lemonilla on November 05, 2013, 09:53:32 AM

I have never worked with the '?' but you could do this:

echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%A in ('dir /b') do (
set a=%%~nA
if "!a:~19,1!"=="" echo %%A
)
set a=


It will delete anything with 19 or less characters, so you have to name it something long.

It's works : Code: [Select]del ????-??-??-??-??-??.csv
but i don't know why. Question mark DISLIKES minus marks .txt does not mean "txt files with exactly 3 characters in the name", it means "txt files with up to 3 characters in the filename". If you use 8 question marks, that means "everything" - ??.txt is the same as *.txt
You can use FOR and a FINDSTR REGULAR expression

Delete only csv files with exactly 19 characters in the filename.

Note: there are 20 dots - 19 for the characters, then one dot + csv for the extension.

for /f %%A in ('dir /b ^| findstr /R "^....................csv"') do del "%%A"

Before:

f:\data>dir /b
2013-10-28-11-43-56.csv
2013-10-28-11-43-56_data.csv

After:

f:\data>dir /b
2013-10-28-11-43-56_data.csvWithout testing - you'll find that dir and del also match the short filename, not only the long filename.

This can lead to some surprises.Thanks Salmon.More robust version - can process & delete files with or without spaces in the name

for /f "delims=" %%A in ('dir /b ^| findstr /R "^....................csv"') do del "%%A"

Configurable - CHANGE extension and change number of characters

@echo off
setlocal enabledelayedexpansion
set extension=.csv
set chars=19
set regstring=
for /l %%A in (1,1,%chars%) do set regstring=!regstring!.
for /f "delims=" %%A in ('dir /b ^| findstr /R "^%regstring%%extension%"') do del "%%A"





Discussion

No Comment Found