2011-08-30 187 views

回答

5

这是一个黑客位,但你可以使用:

Set NasPath=C:\Things\Folder 
Set NasFolder=%NasPath% 
:GetFolder 
Set GetFolderTemp=%NasFolder:*\=% 
If Not %GetFolderTemp%==%NasFolder% (
    Set NasFolder=%GetFolderTemp% 
    Goto :GetFolder 
) 
Echo NasPath =%NasPath% 
Echo NasFolder=%NasFolder% 
Exit /B 

不管你做什么,不要把周围的Set NasPath=...声明的任何部分的报价。使用引号是这样的:

Set FromPath=C:\Program Files\Blah 
Set NasPath=C:\Things\Folder 
RoboCopy "%FromPath%" "%NasPath%" 

不要使用引号是这样的:

Set FromPath="C:\Program Files\Blah" 
Set NasPath="C:\Things\Folder" 
RoboCopy %FromPath% %NasPath% 
+1

+1,但是你应该总是用这种方式引用''Set“FromPath = C:\ Program Files \ Blah”'否则你失败并且'Set FromPath = C:\ Documents&Settings' – jeb

+0

@jeb'SET FromPath = C :\ Docs and Settings'是有效的。就像@ Hand-E所说的那样,要使用该变量,请用引号括起来,''%FromPath%“'。 – aphoria

+0

很明显,“文档和设置”有效,但有一个&符号失败,并且文件/路径名称允许使用&符号 – jeb

0

假设C:\Program Files\Mickey\Mouse样的路径(不带引号),你也可以使用下面的代码:

setlocal EnableDelayedExpansion 

set path=C:\Program Files\Microsoft\Mickey\Mouse 
:shift 
for /f "tokens=1* delims=\/" %%i in ("!path!") do (
    set folder=%%i 
    set path=%%j 
) 
if not [!path!] == [] goto :shift 

echo folder: !folder! 

endlocal 
+0

请注意,这将覆盖'PATH'环境变量。 –

1

为了不与空间有任何问题,我建议这个代码:

Set NasPath=C:\Things\My Space\Folder 
Set GetFolderTemp=%NasPath% 
:GetFolder 
FOR /F "tokens=1,* delims=\" %%1 IN ("%GetFolderTemp%") do (
set NasFolder=%%1 
set GetFolderTemp=%%2 
) 
if not "a%GetFolderTemp%"=="a" goto :GetFolder 

echo %NasFolder% 
相关问题