Reputation: 275
I want to write a windows shell command that goes through all files in a folder with 20 files like:
InputFile1.csv
InputFile2.csv
InputFile3.csv
InputFile4.csv
...
InputFile20.csv
and I want to delete all files with a file number bigger than 5. That means only InputFile1
to InputFile4
should be left in the folder C:\tmp\out
I wrote this command:
FOR /R C:\tmp\out\ %G IN (*) DO
FOR /F %H IN ("%~nG") DO
(SET str="%H" && SET lastdigits=%str:~10,2% && IF %lastdigits% GTR 5 del %G)
as a result, on the command prompt I get, for each file in the folder:
C:\tmp\out>(SET str="InputFile1" && SET lastdigits= 9 && IF 9 GTR 4 del C:\tmp\out\VisInputFile1.csv )
.......
......
C:\tmp\out>(SET str="InputFile9" && SET lastdigits= 9 && IF 9 GTR 4 del C:\tmp\out\InputFile9.csv )
and all the files are deleted.
Why is lastdigits
always 9?
This needs to be one shell command, because I want to use this command in a BPEL-Activity.
Thanks in advance.
Upvotes: 1
Views: 1443
Reputation: 70923
cmd /q /e:on /c "(for /l %a in (1 1 4) do set "inputFile%a=1")&for %a in (inputfile*.csv) do if defined %~na (echo keep "%a") else (echo del "%a")"
It just defines a variable for each file name to keep and then iterates over the file list checking if a variable with the same name is defined. If it is defined keep the file else remove the file. It is equivalent to
for /l %a in (1 1 4) do set "inputFile%a=1"
for %a in (inputfile*.csv) do if defined %~na (
echo keep "%a"
) else (
echo del "%a"
)
but in one line
For a substring operation and numeric test on the file numbers
cmd /q /e:on /v:on /c "for %a in (inputfile*.csv) do (set "n=%~na" & set /a "n=!n:~9!">nul & (if !n! gtr 4 (set "n=") else if !n! lss 1 (set "n=")) & if defined n (echo KEEP "%a") else (echo del "%a"))"
For this to work, as the value of the file number is extracted inside a block and processed inside the same block, delayed expansion is needed.
It is equivalent to
setlocal enableextensions enabledelayedexpansion
for %a in (inputfile*.csv) do (
set "n=%~na"
set /a "n=!n:~9!">nul
if !n! gtr 4 (
set "n="
) else if !n! lss 1 (
set "n="
)
if defined n (echo KEEP "%a") else (echo del "%a")
In both cases, delete operations are only echoed to console. If the output is correct, remove the echo
command that prefixes del
edited (again, I understood it the wrong way) to adapt to comments : to handle the two ranges and remove the files 1-5, 15-20
First code
cmd /q /e:on /c "(for %a in (1 2 3 4 5 15 16 17 18 19 20) do set "inputFile%a=1")&for %a in (inputfile*.csv) do if not defined %~na (echo keep "%a") else (echo del "%a")"
Second code
cmd /q /e:on /v:on /c "for %a in (inputfile*.csv) do (set "n=%~na" & set /a "n=!n:~9!">nul & set "delete=" & (if !n! leq 5 (set "delete=1") else if !n! leq 20 if !n! geq 15 set "delete=1") & if defined delete (echo del "%a") else (echo KEEP "%a"))"
Upvotes: 1
Reputation: 502
Because you posted with the batch file tag I am giving you a batch command you can run from command line, and figure you will know how to modify it if needed for powershell.
The code below will delete the files as you want to do. It simply sets var G=5 and then at each iteration increments the number by 1 until G=20 is processed, then it stops. The G var increments the number in the file name with each iteration.
This just echoes the files so you can see it works. Change ECHO to DEL to delete the files.
FOR /L %G IN (5,1,20) DO ECHO "C:\tmp\out\InputFile%G.csv"
If you are running this from a batch file, use @echo off and double the %marks like this
@echo off
FOR /L %%G IN (5,1,20) DO ECHO "C:\tmp\out\InputFile%%G.csv"
Upvotes: 0