gpt4 book ai didi

windows - 如何使用 Windows 命令行搜索文件,然后创建带有位置的文本文档?

转载 作者:可可西里 更新时间:2023-11-01 10:07:56 25 4
gpt4 key购买 nike

我正在编写我的第一个批处理文件,因为我以前从未使用过 Windows 命令行,所以我遇到了一些问题。

这是我的场景。我在几个图像上使用这个批处理文件来安装一些东西。我希望批处理文件检查安装程序应该位于的文件夹,如果不存在,我希望批处理文件在计算机中搜索安装程序。之后,我希望它运行所述安装程序。

这是我所拥有的:

ECHO.
ECHO Starting Foo installation

IF EXIST Install\Installer.cmd (CALL Install\Installer.cmd & GOTO NextPart) ELSE (GOTO SearchInstaller)

:SearchInstaller
SET the_path =
E: & CD\
DIR /S/B Installer.cmd > installer_location.txt
IF EXIST installer_location.txt (SET /P the_path =< installer_location.txt & GOTO FoundIt)
C: & CD\
DIR /S/B Installer.cmd > installer_location.txt
IF EXIST installer_location.txt (SET /P the_path =< installer_location.txt & GOTO FoundIt)
D: & CD\
DIR /S/B Installer.cmd > installer_location.txt
IF EXIST installer_location.txt (SET /P the_path =< installer_location.txt & GOTO FoundIt)
ECHO Installation file not found.

:FoundIt
ECHO Batch file found at%the_path%
CALL %the_path%
ECHO Finished installation & ECHO.
GOTO NextPart

:NextPart
(more stuff)

我认为问题是它没有保存路径,一旦我使用 DIR 找到它。我已经研究了好几天,我的 Google 搜索全是紫色链接。我发现的所有内容都表明我的语法是正确的,但我知道我做错了什么。

我试过在几个地方放置 ECHO Program execution reached this point.,所以我至少知道它到达了哪里。我看到的问题是将文本文件的内容分配给 the_path 的行以及我尝试 ECHO 路径的行,所以我可以看到它奏效了。

最佳答案

在您的 SET/P 命令中,变量名和等号之间有一个空格。这完全符合您的要求,而不是您想要的:它设置一个名称以空格结尾的变量。去掉多余的空格,我认为它会起作用。

附加:-

为了避免 Neil 指出的问题(很好!)试试这个:

DIR /S/B Installer.cmd > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :foundit

事实证明,如果未找到 installer.cmd,则 installer_location.txt 为空且 the_path 未定义。如果您想格外小心,请尝试

if defined the_path if exist "%the_path%" goto :foundit

关于windows - 如何使用 Windows 命令行搜索文件,然后创建带有位置的文本文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11006014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com