1

If I have a certain folder with a number of .txt files, each contain a single curl cmd in a single line, I want to be just able to point to the folder then all of these commands are launched once, preferably in the ascending order of their file modification date and preferably the output of each command is saved in a file (just one file for all, with separation format in between).

I tried a .bat solution as follows but a 8kb file (or command) doesn't run correctly, despite that I just opened the .txt file in which the command is and copied it directly into cmd, it works:

@echo off setlocal enabledelayedexpansion :: Loop through all .txt files in the current folder (excluding this batch script itself) for %%f in (*.txt) do ( set "cmd=" :: Read the content of each file (e.g., 1.txt, 2.txt, etc.) for /f "delims=" %%a in (%%f) do ( set "cmd=!cmd! %%a" ) :: Execute the command echo Executing: %%f cmd /c "!cmd!" ) ) pause endlocal 

Other 3-4 kb commands run well.

3
  • 1
    So what error(s) do you get? Comment out the @echo off and inspect the output carefully. Commented Nov 23, 2024 at 14:29
  • 1
    There is a limit on how many characters each line in a batch script can have superuser.com/questions/1070272/…. Commented Nov 23, 2024 at 14:32
  • 1
    apparently max command line len is 8191 bytes. much larger for powershell Commented Nov 23, 2024 at 16:17

1 Answer 1

1
@echo off && cd /d "%~dp0" for /f ^usebackq^ ^delims^= %%G in =;(' dir /o-d /t:w *.txt ');= do for /f usebackq^ ^delims^= %%i in =;(` type "%%~dpnxG" `);= do echo/Executing: "%%~G" "%%~i" && <con: "%ComSpec%" /s /q /e:on /c "%%~G "%%~i" endlocal 

Notice that we lack examples of the file contents and/or the syntax/commands used. If you're using cURL in your commands, it's implied that links are present in your .txt files. These links may contain special characters like %, &, ?, or others (due to URL encoding in the addresses), which could lead to unexpected behaviors if necessary escapes are not applied.


Note: The suggested code is merely a demonstration of how the process can be streamlined without the need to define a variable or use SetLocal EnableDelayedExpansion. However, this approach may still result in unexpected behavior when processing links or addresses containing the "!" character.


..."I want to be just able to point to the folder then all of these commands are launched once"...

  • Just replace cd /d "%~dp0" with:
    cd /d "D:\Full\Path\To\Folder\Where\Your\Txt\Files\Are"

..."preferably in the ascending order of their file modification date"...

  • Just replace For ... (*.txt) do.. with:
    For /F ... ( dir /o-d /t:w *.txt ) do ...

..."just one file for all, with separation format in between"...

  • In this case, it might be useful to add a counter and include it in the output during each command execution:
@echo off && cd /d "%~dp0" set /a "_cnt=1-1" for /f ^usebackq^ ^delims^= %%G in =;(` dir /o-d /t:w *.txt `);= do for /f usebackq^ ^delims^= %%i in =;(` type "%%~dpnxG" `);= do =;( call set /a "_cnt+=1" echo/Executing: "%%~G" "%%~i" call echo/Runnning cmd #%%_cnt%%: "%ComSpec%" /s /q /e:on /c "%%~G %%~i" );= endlocal 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.