Works in Command Prompt but not in BAT-file or CMD

2019-10-20 04:04发布

问题:

This question already has an answer here:

  • Why does FOR loop command not work in a batch file which works on Windows command prompt? 3 answers

Due to problem with WINDOWS DEFENDER I need do the following:

for /r "%appdata%\..\Local\MyProg\2.0" %D IN ("*MyProgram.exe") DO "%~fD

It works perfect in COMMAND PROMPT but not in batch-file or cmd-file - why?

How do I make in a excutable file as a Batch-file or ".cmd"?

回答1:

When running for loops in a batch file, you need to use an additional % in predefined variables. So it should be:

for /r "%appdata%\..\Local\MyProg\2.0" %%D IN ("*MyProgram.exe") DO "%%~fD

I suggest you read up using the well documented help by running for /? from cmdline. You will benefit from it, guaranteed!



回答2:

A recursive For loop already returns the full path, additionally there's already a system variable for the path %AppData%\..\Local.

For /R "%LocalAppData%\MyProg\2.0" %%A In ("*MyProgram.exe") Do "%%A"

Depending upon your needs, it may be worth checking out the Start command usage too, Start /?. You may find that Do Start "" "%%A" is what you needed.