- 1 year ago
Windows Batch For-Loop 'input line too long'
If you're encountering an "input line too long" error while using a for
loop in a Windows batch script, it means that the command line you're trying to execute is too long for the shell to handle. This can happen if you're iterating over a large number of files, for example.
To work around this issue, you can use a forfiles
command instead of a for
loop. The forfiles
command can handle long command lines by executing each command in a separate instance of cmd.exe
.
Here's an example of how you can use the forfiles
command to iterate over a directory and print the name of each file:
bashforfiles /p "C:\path\to\directory" /m "*" /c "cmd /c echo @file"
In this example, the /p
option specifies the path to the directory you want to iterate over, the /m
option specifies the file mask (in this case, *
to match all files), and the /c
option specifies the command to execute for each file. The @file
token is replaced with the name of the current file being processed.
You can replace the echo @file
command with your own command, as long as it doesn't exceed the maximum command line length limit.