Title: How to run commands on each text file of a directory and delete each text file?
Tags: batch-file
Question: 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
Is there a method to incorporate this code into a batch file type of loop?
Comment: To determine whether there are `.txt` files left, you could use `if not exist "*.txt" ...` instead of counting the files...

Here is the accepted answer: 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"
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.

Here is another answer: 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
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 75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0 (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&gt; or <commandname&gt; /?

Here is another answer: 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:
Each time first evaluate condition then execute code (the while-do loop): the loop will be of this form
Rem commands to prepare your condition if any come here
<commands_prepare_condition&gt;
IF NOT <condition&gt; goto :outsideLoop
Rem commands to loop over come here
<commands_loop&gt;
goto :loop
:outsideLoop
<command_after_loop&gt;
Each time first execute the code inside the loop then verify condition for the next round (the do-while loop):
Rem commands to loop over come here
<commands_loop&gt;
Rem commands to prepare your condition if any, come here
<commands_prepare_condition&gt;
IF <condition&gt; goto :loop
<commands_after_loop&gt;
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&gt; 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&gt; 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&gt;
<commands_after_loop&gt; is the place where the commands after the loop must go. That part will be executed when the loop is exited.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:01/13/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<batch-file>', 'Title': 'How to run commands on each text file of a directory and delete each text file?', 'ViewCount': 166, 'id': 41628336, 'user_AboutMe': None, 'user_AccountId': 8135014.0, 'user_DisplayName': 'John', 'user_Reputation': 309.0, 'user_Views': 37.0, 'user_id': 6126937.0}}