0

I"m trying to delete these files with numbers appended to them using a for loop in a Windows batch file.

My problem is that I can't seem to build the filename strings using the for loop, despite several attempts at doing this.

Below is the snippet of my code that I"m trying to run with. As you can see, I've got 4 files named file0.txt, file1.txt, file2.txt and file3.txt in some nested folder, and I'm trying to delete them using a for loop

Ideally I want to be able to be able to set the limit of the for loop depending on how many files there are. And then I'd want to change the file extension from .txt to whatever to delete other files except the ones I want to keep.

Any help would be appreciated!! Here's the code:

setlocal enabledelayedexpansion

set num=3
set /a forLoopLimit=%num%-1

cd folder

FOR /L %%x IN (0,1,%forLoopLimit%) DO (
    setlocal enabledelayedexpansion
    echo %x
    set y=%x
    set filename=file%y%.txt
    del %filename% /f /q
    echo %filename%
)

cd ..
MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
bsquared
  • 13
  • 3

2 Answers2

1
setlocal enabledelayedexpansion

set num=3
set /a forLoopLimit=%num%-1

cd folder

FOR /L %%x IN (0,1,%forLoopLimit%) DO (
 echo %%x
 set filename=file%y%.txt
 ECHO del file%%x /f /q
 echo file%%x
)

cd ..

Problems:

To refer to the metavariable (Loop-control variable) x, you need %%x, not %x within a batch file.

setlocal is not a switch. Each time it is used, it establishes a new frame which is terminated by endlocal or reaching end-of-file.

If you are using delayededexpansion then you need to refer to the variable you are changing using !var!, not %var%. !var! means the changed value, %var% means the value of the variable as it was when the for keyword was encountered.

You don't need to use y in your application. %%x is actually a string, but it will be a numeric string, so it can be used in calculations.

The del command is simply echoed above to allow the command to be displayed - in case there's a code error which might delete unexpectedly.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hey Magoo. Thanks for your post! I ended up using the solution of Stephan, which was also based off of yours ;) Just wanted to know I ended up implementing both of your guys' code basically :) – bsquared Jul 21 '19 at 15:06
0

Basically, you fell into the delayed expansion trap (you enabled delayed expansion, but you didn't use it).

But your task can be done without those variables that need delayed expansion. You can use %%x directly and define the rest outside the loop:

@echo off
set num=3
set /a forLoopLimit=%num%-1
set "filebase=file"
pushd folder

FOR /L %%x IN (0,1,%forLoopLimit%) DO (
    ECHO del "%filebase%%%x.txt" /f /q
)
popd

Like Magoo, I disarmed the del command by just echoing it. If the output is what you expect, just remove the ECHO.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Hey, I'm about to try out this solution. Just wondering why you used popd and pushd instead of cd and cd.. ? – bsquared Jul 21 '19 at 14:40
  • `cd` changes the working directory and forgets where it comes from. `pushd` saves the current working directory on a stack before changing it, so `popd` can restore the previous working directory. Basically, it's just a personal habit. – Stephan Jul 21 '19 at 18:13