2014-02-18 46 views
0

基本上我只想使用變量而不是permament值。在cmd中unlogic FOR循環

我有一個循環,它的工作原理。

set OS_ROOT=%systemdrive% 

for /f "delims= " %%A IN ('DIR E:\BACKUP /A:D /O:-D /TW /B') 
DO (xcopy/e E:\BACKUP\%%A %OS_ROOT%\TempFestplatte) 

,但只要我實現變量:

SET STICK_ROOT=%CD:~0,3% 

這happends爲 「E:\」,並在我的循環使用:

for /f "delims= " %%A IN ('DIR %STICK_ROOT%\BACKUP /A:D /O:-D /TW /B') 
DO (xcopy/e %STICK_ROOT%\BACKUP\%%A %OS_ROOT%\TempFestplatte) 

它不工作任何摩爾。只有在'DIR(...)

爲什麼?

+2

您正在執行'DIR E:\\ Backup',它會給出「文件名,目錄名稱或卷標語法不正確。」改變'SET STICK_ROOT =%CD:〜0.3%'爲'SET STICK_ROOT =%CD:〜0.2%' –

回答

2

避免參考驅動器根文件夾的雙反斜槓。

:: Set variable in terms of current directory drive 
set "STICK_ROOT=%cd:~0,2%" 

:: Set variable in terms of current batch file drive 
set "STICK_ROOT=%~d0" 

不確定哪兩個更適合您的情況。

+0

是的,我是延遲。 – user2786496