0

I have a directory with for example 5 text files.
The batch file should perform a task on a text file and delete the file afterwards.

Is there a method to do this in a loop until there are no text files anymore?

By searching in world wide web I have found some code to count the number of files in a directory which may be a good starting point.

@echo off
set count=0
for %%x in (*.txt) do set /a count+=1
echo %count%
endlocal
Pause

Is there a method to incorporate this code into a batch file type of loop?

Mofi
  • 46,139
  • 17
  • 80
  • 143
John
  • 321
  • 5
  • 16
  • To determine whether there are `.txt` files left, you could use `if not exist "*.txt" ...` instead of counting the files... – aschipfl Jan 13 '17 at 08:08

3 Answers3

1

That is indeed a good starting point! A few things to note additionally:

  • You can execute more than one command in a loop using the syntax

    do (
    REM do things here
    )
    
  • When using a variable within the loop you set it in, you have to use DelayedExpansion. Have a look at it here on StackOverflow there are about 5 Million questions about, why a variable in batch is not set in a loop.

  • The parameter you are using (%%x in your case) contains valuable information about files if you are looping over them. They are listed over here.

  • You may not use any goto within that loop or else it will be broken! This includes comments with :: (which you should in general replace with REM).

For additional help have a look around SO and for example SS64. Also note that you can get more information about a command using either help <commandname> or <commandname> /?

Community
  • 1
  • 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
0

What you probably need is a while loop kind of solution. Unfortunately no built in windows command exists for a while loop but you can easily mock it yourself. A while loop is just something that executes some code as long as a condition is true. To evaluate a condition you have the IF command. To jump back to some code (or jump over some code) you have the goto command and labels of the form :labelName (nothing else can come on the same line as a label declaration). To jump to a label you can either use goto labelName , goto :labelName or even goto:labelName.

Your while loop can take 2 forms:

  1. Each time first evaluate condition then execute code (the while-do loop): the loop will be of this form

    :loop
    Rem commands to prepare your condition if any come here
    <commands_prepare_condition>
    IF NOT <condition> goto :outsideLoop
    Rem commands to loop over come here
    <commands_loop>
    goto :loop
    
    :outsideLoop
    <command_after_loop>
    
  2. Each time first execute the code inside the loop then verify condition for the next round (the do-while loop):

    :loop
    Rem commands to loop over come here
    <commands_loop>
    Rem commands to prepare your condition if any, come here
    <commands_prepare_condition>
    IF <condition> goto :loop
    
    <commands_after_loop>
    

    Beware that in this case, the loop will always be executed at least one time even if the first time the condition is not met because the condition is verified after the execution of the code in the loop.

In each of the 2 cases:

  • replace <commands_prepare_condition> with commands that will prepare your condition. As the list of conditions is limited for the IF you may need to prepare some variables or others before. In your case: the count of files.

  • replace <condition> with the condition you want to be true from the possibilities on this page. In your case that would be: %count% GTR 0. But consider @aschipfl's comment and use EXIST "*.txt" which doesn't need any preparation and will turn to true only if any file with txt extension exists

  • put all commands you want to execute while looping instead of <commands_loop>

  • <commands_after_loop> is the place where the commands after the loop must go. That part will be executed when the loop is exited.

J.Baoby
  • 2,167
  • 2
  • 11
  • 17
0

Open a command prompt window and run for /?. The output help explains how to used command FOR to do something on files found in a directory or with numbers or on various strings read from somewhere.

You can use for your simple task:

@echo off
for %%I in (*.txt) do (
    echo File: "%%~fI"
    echo del "%%~fI"
)
pause

Replace the first line with command ECHO in command block of FOR by the command line(s) to use on found *.txt file which name is hold by case-sensitive loop variable I.

Remove echo on second line in command block if the batch file is working as expected by you.

Note: This batch file just outputs the name of each found *.txt file with full path and the command line which would be used for deleting the *.txt file without doing the deletion.

Mofi
  • 46,139
  • 17
  • 80
  • 143