2012-06-12 17 views
2

我正在寫我的第一批文件,因爲我以前從未使用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,所以我可以看到它的工作。

+0

我會說,只要你有任何不平凡的if或ANY循環停止使用批處理文件。我開始學習Python只是這種事情。或者選擇其他任何(非PowerShell腳本:-)腳本(它可能與msbuild一起使用),你會更快樂,更有效率 –

+0

我很想(我崇拜Python),但我堅持批量時刻。 – Torr

+0

我不認爲這會在'C:'或'D:'驅動器上找到'cmd'文件,因爲'installer_location.txt'在E文件找不到文件後會一直存在(但是爲空) :'驅動器。 – Neil

回答

3

在您的SET /P命令中,變量名稱和等號之間有空格。這正是你所要求的,而不是你想要的:它設置了一個名字以空格結尾的變量。擺脫多餘的空間,我認爲它會起作用。

附加條件: - (!好對象)

爲了避免這個問題尼爾指出,試試這個:

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 
+4

你是一個巫師,哈利。 – Torr

相關問題