Reputation: 334
I need to create a batch script that will run all .exe files in a folder. This must include subfolders.
I am running windows 7, and the batch file is stored in the root folder
I have tried several variations with no success. The two main variations are as follows
REM dir *.exe /S /B > tmpFile
REM set /p listVar= < tmpFile
REM del tmpFile
FOR %%F IN (%listVar%) DO %%F
=======================================
FOR /F "usebackq" %%F IN ('dir *.exe /S /B') DO %%F
I have looked through several variations on the above methods but none work. The top version (which I would like to avoid) only reads in one line from the file. The Bottom version outputs an unmodified "dir" command in the prompt window.
Ultimately I would like help identifying how to solve this issue without the need for a temp file.
Upvotes: 5
Views: 23921
Reputation: 15
There is an important caveat to MC NDs solution. First if you run that script as an administrator, the home directory defaults to C:\Windows\System32 even if the script is located elsewhere. Attempting to run every script from that location will have a detrimental effect on your system and potentially cause you to need to reinstall windows (ask me how I know... and thank god for test VMs and checkpoints) You should prefix that code with
cd /D "%~dp0"
This changes the working directory to wherever the file is located.
So the script would be:
cd /D "%~dp0"
for /r "." %%a in (*.exe) do start "" "%%~fa"
I'd even go so far as to recommend taking the directory as an argument.
Upvotes: 0
Reputation: 149
for %1 in (%USERPROFILE%\*.exe) do "%1"
This will start all executables in your user folder. Of course, that includes installers and stuff like that, and you probably don't want to reinstall all your apps, so replace %USERPROFILE%
with the directory you want.
Upvotes: 0
Reputation: 70923
for /r "." %%a in (*.exe) do start "" "%%~fa"
This will start all the executable files under the current folder and subfolders. Change the reference to the current folder ("."
) to your needs.
Upvotes: 8