I'm trying to execute a command in a bat file which is working perfect when I specify the targeted file in the command ( for example: "c:\my work\month.stats.txt").

The command line structure is exactly like this

 xxxxxxx.exe [command_parameter] c:\my work\month.stats.txt

I want to make it work automatically on every ".txt" file exist in certain folder (for example: "c:\my work" folder).

Is there a way to do that?


  • it's a command linked to an exe file related to my work. it's working like this ( xxxxxxx.exe [command_parameter] c:\my work\month.stats.txt ). The thing is that I have many ".txt" files and I want the bat file to do this command on any .txt file exist on "c:\my work" folder – Medo Reo Aug 26 '18 at 3:23
  • would you please give me an example for this command line? ( xxxxxxx.exe [command_parameter] c:\my work\month.stats.txt) – Medo Reo Aug 26 '18 at 3:33
  • 1
    Open command prompt in that folder and type: For /R %Y in (*.txt) do (xxx.exe %Y) – Biswapriyo Aug 26 '18 at 4:18
  • 1
    forfiles /s /m *.txt /p c:\"my work" /C "cmd /c xxxx.exe xxx_params @path" no need any bat files, that one line can do what you want – Alex Aug 26 '18 at 5:03
  • Thanks guys for your help. I really appreciate it. I tried both commands and they worked. The thing is that "xxxx.exe" file execute commands by itself and the final parameter of this command is the path of the text file. When I use "For" or "forfiles" commands they work only if the "exe" and it's associated files exist at the same path of the targeted ".txt" files. I'm looking for a way to make it work even if the targeted ".txt" in another path. THANKS AGAIN. – Medo Reo Aug 26 '18 at 14:20

To apply the executable command with all text file use For /R loop. Open command prompt in that folder and type:

For /R %Y in (*.txt) do (abc.exe %Y)

Or if you want to apply the command in another folder add the folder path in that command:

For /R "C:\path\folder" %Y in (*.txt) do (abc.exe %Y)

Here are the details of this command:

  • Syntax:

FOR /R [[drive:]path] %%parameter IN (set) DO command
  • Key drive:

    • path : The folder tree where the files are located.
    • set : A set of one or more files. Wildcards must be used. If (set) is a period character (.) then FOR will loop through every folder.
    • command : The command(s) to carry out, including any command-line parameters.
    • %%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G)

Source: ss64: For /R

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.