SlothLovesChunk
SlothLovesChunk

Reputation: 75

Batch File - FOR LOOP

Let me set the stage of my issue. I have a folder (FOLDER_ABC). Within that folder, there is a folder for my application, including a unique and always changing version number (application-v1.3.4). Within that folder, there is the application (application-v1.3.4.exe) - which will also change periodically.

C:\FOLDER_ABC\application-v1.3.4\application-v1.3.4.exe

In this section below I create a directory listing of the FOLDER_ABC for any folders starting with application* and store that folder name into a file called directory.txt. I then create a perameter and store that directory into it. I'm doing it this way, versus applying the direct full path to the directory or file, since the versions will change and I don't want to hard code the batch script.

cd C:\FOLDER_ABC\
dir  application* /b /ad>"C:\FOLDER_ABC\directory.txt"
set /p verdir= <C:\FOLDER_ABC\directory.txt

Here is my issue. In the section below, I'm trying to get my batch script to run the application*.exe file, and continue on with my batch file. It currently runs my application, but it hangs and doesn't continue the rest of my batch script. I'm really new to all this coding so I appreciate the help. I assume it could be something related to me not closing the FOR loop properly? How can I get it to continue on to :FINISH?

cd "C:\FOLDER_ABC\%verdir%\"
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO %%G;

:FINISH
ECHO THE END
exit

Figured it out, but didn't have enough StackOverflow credits to answer my own question. My solution is listed below. Thanks everyone. You pointed me in the right direction.

cd "C:\FOLDER_ABC\%verdir%\"
FOR %%G in (*.exe) DO START %%G

Upvotes: 1

Views: 652

Answers (3)

to StackOverflow
to StackOverflow

Reputation: 124794

Try using the START command:

FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO START %%G;

There may be other better ways if achieving what you want, for example, if you know that the exe always has the same name as its directory, and that there will be only one such directory, you could do the following:

FOR /D %%i in (application-v*) DO START %i\%i.exe

UPDATE

From comments:

My only issue, which I just realized, is that the application folder and application name are not always identical.

In that case, you could try something like:

for /d %%i in (application-v*) do for %%j in (%%i\*.exe) do start %%j

Upvotes: 1

dbenham
dbenham

Reputation: 130919

I'm not sure if this is your problem, but it is possible that the ; is causing problems. You do not terminate commands with ; in batch files.

There is no need for a temporary file. Your code can be greatly simplified with FOR:

pushd c:\folder_abc
for /d %%F in (application*) do cd "%%F"
for %%F in (*.exe) do "%%F"

:FINISH
ECHO THE END
exit /b

Upvotes: 1

Endoro
Endoro

Reputation: 37589

you can try:

FOR /f "delims=" %%G IN ('dir /b /a-d *.exe') DO start "" "%%~G"

Upvotes: 1

Related Questions