我正在寫我的第一批文件,因爲我以前從未使用Windows命令行,所以遇到了一些問題。如何使用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找到它。我一直在研究數天,而我的谷歌搜索都充滿了紫色鏈接。我發現的一切都表明我的語法是正確的,但我知道我做錯了什麼。
我試過在幾個地方放置一個ECHO Program execution reached this point.
,所以我知道它至少在哪裏。我看到的問題是將文本文件的內容分配給the_path
的行以及我想要的路徑ECHO
,所以我可以看到它的工作。
我會說,只要你有任何不平凡的if或ANY循環停止使用批處理文件。我開始學習Python只是這種事情。或者選擇其他任何(非PowerShell腳本:-)腳本(它可能與msbuild一起使用),你會更快樂,更有效率 –
我很想(我崇拜Python),但我堅持批量時刻。 – Torr
我不認爲這會在'C:'或'D:'驅動器上找到'cmd'文件,因爲'installer_location.txt'在E文件找不到文件後會一直存在(但是爲空) :'驅動器。 – Neil