【发布时间】:2018-09-26 18:51:03
【问题描述】:
for /F %%i in ("%dist_exe_source%") do set dist_exe=%%~nxi
我对在字符串中检查的内容以及在 dist_exe 中设置的内容感到困惑?
我已经深入研究了命令提示符帮助,但我仍然对这一行中实际发生的情况感到困惑。
提前致谢
编辑
背景:在发布了一些 cmets 之后,我想提供更多信息。我正在处理我继承的应用程序。此应用程序是使用 Qt IDE 编写的。该应用程序是客户测试和与我们的产品交互的一种方式。批处理文件是“自定义构建步骤”过程的一部分,该过程调用该文件以及两个参数,这些参数是如下路径。
scripts\windows_collect_files_to_package.bat build\release\unpacked release\application.exe
完整代码在这里:
:: the directory that this script was started in
set cur_dir=%cd%
:: the directory where all the files that need to be packaged will be placed
set dist_dir=%1
:: the exe to find needed dlls for
set dist_exe_source=%2
for /F %%i in ("%dist_exe_source%") do set dist_exe=%%~nxi
:: clean up any leftovers...
rmdir /q /s %dist_dir%
:: copy all the dlls and other stuff to dist_dir...
mkdir %dist_dir%
copy /y %dist_exe_source% %dist_dir%
cd %dist_dir%
windeployqt --compiler-runtime %dist_exe%
cd %cur_dir%
批处理文件最终将应用程序所需的文件依赖项分组到特定目录中。然后它运行 windeployqt.exe(这是来自 Qt for windows 平台的部署命令)。
我的问题是了解 for 循环在做什么,以便我可以记录整个过程。
【问题讨论】:
-
我会假设源变量具有文件的完整路径,并且
FOR命令正在读取该变量,而 set 命令仅获取文件名而没有路径。 -
如果没有任何进一步的上下文,很难确定地解释该行在做什么。在到达此行之前,代码中的
%dist_exe_source%设置为什么? -
我认为这是不正确的。如果变量
%dist_exe_source%的值是一个可执行命令,它应该读取For /F "Delims=" %%A In ('"%dist_exe_source%"') Do Set "dist_exe=%%~nxA",其中新变量%dist_exe%将保存从命令返回的文件名和扩展名。如果变量%dist_exe_source%的值只是一个带有路径的可执行文件名,则它应为For %%A In ("%dist_exe_source%") Do Set "dist_exe=%%~nxA",其中新变量%dist_exe%将保存%dist_exe_source%值的文件名和扩展名部分。 -
@KenWhite
dist_exe_source设置为可执行文件:set dist_exe_source=\release\application.exe。它被设置为命令行的参数;所以批处理文件行实际上设置为dist_exe_source=%2。第二个论点其实是我上面写的。 -
@N.Dijkhoffz Compo 是绝对正确的。命令不正确。应该是
for %%i in ("%dist_exe_source%") do set "dist_exe=%%~nxi"来获取分配给环境变量dist_exe的文件名和文件扩展名,而不需要相对或绝对路径。此处不需要选项/F,如果可执行文件的路径包含空格字符,则可能会产生错误结果,正如 Compo 解释的那样。有关命令 FOR 的帮助,请打开命令提示符窗口并运行for /?,它会在多个页面上输出此命令的帮助。
标签: batch-file for-loop