0

I've seen multiple posts about issues like this, but I can't find the answer that would fit my example.

What I need

Within my script I need to start all .cmd-files that are in a spesific folder (not where my script is located). It is 53 .cmd files that does different type of jobs within this folder.

The issue

I want a short command that makes it possible to open all the files within one or a few lines insted of having 53 of these lines start "c:\allcmdfiles\example-xxx-xx shortcut.cmd"

What I'm trying

So with other posts on StackOverFlow I bumped into the call command. I've tried a variety of these commands, but can't make it work

for %%f in ("c:\allcmdfiles*.cmd") do CALL %%f

Perhaps some practical info

Its gonna run on Windows Server 2008 R2 std, but I'm testing on W7

The files will variate with spaces in the name and dashes

My question

So does anyone know how I can nail this? Thanks!

6
  • Have you tried the loop with single % (%f) and .bat extension? Commented May 20, 2015 at 12:21
  • @i486 Yeah, I've tried that but it didn't make any differences. Thanks for your suggestion tho! Commented May 20, 2015 at 12:25
  • I hope the above loop is also in .bat file? Also, cd in target dir. Commented May 20, 2015 at 12:27
  • do must be on same line as for or cmd.exe exits batch processing because of invalid syntax. Test batch files by executing them from within a command prompt window and not by double clicking on it from GUI to see error messages. Commented May 20, 2015 at 12:32
  • @Mofi Aha, so thats why it has been exiting. Good to know! I made a testfolder now with multiple .cmd files. I startet command promt and ran the following line: for %%f in ("C:\ProgramData\test*.exe")do CALL %%f (The error message i get now sais %%f was not expected now. (Keep in mind this is directly translated by me since my os is in norwegian)) Commented May 20, 2015 at 12:43

2 Answers 2

2
for /f "tokens=*" %%i in ('dir /b c:\allcmdfiles\*.cmd') do call c:\allcmdfiles\%%i

you missed the dir command to list the files with wildcars , we used /b (brief) because we only need the name of the file

Sign up to request clarification or add additional context in comments.

6 Comments

That makes sence and seems like something that would work. Tho I must admin that when I add it to the script and run it, it opens just one of the .cmd files and then exits itself. I tried open command promt and run the command from there, but it gives me this error "%%i was not expected now (directly translated from the Norwegian error). Any ideas? You're definitely on to something here. Thanks for your reply!
remove the double % if you run it directly from the prompt. If you want the console to stay open you will have to tweak it a bit. Maybe like this for /f "tokens=*" %%i in ('dir /b c:\allcmdfiles\*.cmd') do start "" "cmd /K c:\allcmdfiles\%%i"
Shelling out a new process to list the files is inefficient and unnecessary. It should work perfectly well with a conventional for command. (And does for me.)
@Kayasax If there were spaces in the foldernames, how can I do this? I've tried to add "" here and there, but doesn't seem to work properly. I'm probably placing it wrong. With this command: for /f "tokens=*" %%i in ('dir /b c:\allcmdfiles\*.cmd') do start "" "cmd /K c:\allcmdfiles\%%i" and lets say the folders new name is "c:\all cmd files\.
for /f %%i in ('dir /b "c:\all cmd files\*.bat"') do start "title" cmd /k "c:\all cmd files\%i"
|
2

This works for me in a batch file:

for %%f in ("c:\test\test*.cmd") do call "%%f"

or the equivalent on the command line:

for %f in ("c:\test\test*.cmd") do call "%f"

I note that when using Kayasax's variant you said the first child script runs and then the parent exits. That suggests that the child script is using the exit command. The best solution is to fix the child script, but if you can't do that you'll have to run it in a subshell:

for %%f in ("c:\test\test*.cmd") do cmd /c "%%f"

3 Comments

Thanks for the great input, Harry. I'll test and see how this works with the script. Tho Kayasax's last suggestion did actually work perfectly which I just testet. This one: for /f "tokens=*" %%i in ('dir /b c:\allcmdfiles*.cmd') do start "" "cmd /K c:\allcmdfiles\%%i"
cmd /c will close the console after the script execution, you have to use /k to keep it open
@Kayasax: in this context, cmd /k would do completely the wrong thing; it would stop and wait for user commands after each child script and wouldn't run the next one until the user issued an "exit" command. I'm not launching new console windows here, each script runs in the parent console window. (This is usually the preferred behaviour; I doubt the OP really wants a dozen extra console windows left behind each time he runs the script!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.