简体   繁体   English

批处理文件 - FOR LOOP

[英]Batch File - FOR LOOP

Let me set the stage of my issue. 让我谈谈我的问题。 I have a folder ( FOLDER_ABC ). 我有一个文件夹( FOLDER_ABC )。 Within that folder, there is a folder for my application, including a unique and always changing version number ( application-v1.3.4 ). 在该文件夹中,我的应用程序有一个文件夹,包括一个唯一且始终在变化的版本号( application-v1.3.4 )。 Within that folder, there is the application ( application-v1.3.4.exe ) - which will also change periodically. 在该文件夹中,有应用程序( application-v1.3.4.exe ) - 它也会定期更改。

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 . 在下面的这一节中,我为以application * FOLDER_ABC的任何文件夹创建FOLDER_ABC的目录列表,并将该文件夹名称存储到名为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. 在下面的部分中,我试图让我的批处理脚本运行application*.exe文件,并继续我的批处理文件。 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? 我认为这可能与我没有正确关闭FOR循环有关吗? 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. 弄清楚了,但没有足够的StackOverflow信用来回答我自己的问题。 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

Try using the START command: 尝试使用START命令:

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: 如果您知道exe的名称与其目录的名称相同,并且只有一个这样的目录,则可以执行以下操作:

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

UPDATE 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

你可以试试:

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

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: 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM