使用 if 條件在多個位置建立捷徑的批次文件

使用 if 條件在多個位置建立捷徑的批次文件

我們有一個批次文件,可以在 Windows 2003 電腦上建立捷徑。我們使用的命令類似於:

echo copy the shortcut to the server All Users folder for windows 2003...
xcopy "%OutputDir%%LocalOutputDir%\Shortcuts\*.*" "\\%SERVERNAME%\c$\Documents and Settings\All Users\Start Menu\Programs\Startup\*.*"

現在,我們也有一些新的 Windows 2008 機器,它們的捷徑資料夾(所有使用者)的路徑是不同的(C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup)。

所以我的問題是我們如何放置一個 if 條件,以便如果批次檔找不到 2003 的位置,它將快捷方式保存在 2008 機器的位置...

謝謝!

答案1

IF <statement>  (
..
..
) ELSE (
...
...
)

……或單獨的 IF 語句而不帶 ELSE,以防存在第三條路徑

IF exist "\\%SERVERNAME%\c$\Documents and Settings\" (
...
...
)

IF exist "\\%SERVERNAME%\C:\ProgramData\Microsoft\Windows\Start Menu\" (
...
...
) 

您可能想要使用/簽出環境變數“所有使用者資料" 它可以讓您自動存取下列資料夾:

在 XP 和 2000 上> C:\Documents and Settings\All Users
在 Vista/Win7/+ 上 > C:\ProgramData

如果您使用變數而不是硬編碼路徑,則不需要 IF 語句...

xcopy "%OutputDir%%LocalOutputDir%\Shortcuts\*.*" "\\%SERVERNAME%\%ALLUSERSPROFILE%\Start Menu\Programs\Startup\*.*"

相關內容