2015-10-30 137 views
0

我寫了一篇批顯示.PLT文件的每個子文件夾下,但會混淆變量「子目錄」:批次的變量混淆

@echo off 
set curdir=%cd% 

for /d %%D in (*) do (
    echo %%~fD 

    set subdir=%%~fD 
    echo %subdir% 

    for /r "%subdir%" %%i in (*.plt) do (
     echo %%i 
    ) 

    echo ______DONE______ 
) 

cd %curdir% 

- 我嘗試設置「子目錄的價值「作爲當前子文件夾,但每次打印的信息顯示,它總是等於最後一個子文件夾:

F:\test>show.bat 
F:\test\55  //--> this is the name of first sub folder and contains 55.plt 
F:\test\Ubuntu // but it prints last folder's name with "subdir" 
F:\test\Ubuntu\Ubuntu.plt // and printed file Ubuntu.plt under sub folder Ubuntu 
______ 
F:\test\Nexus 5 
F:\test\Ubuntu 
F:\test\Ubuntu\Ubuntu.plt 
______ 
F:\test\Ubuntu 
F:\test\Ubuntu 
F:\test\Ubuntu\Ubuntu.plt 
______ 
F:\test> 

我怎樣才能讓‘子目錄’等於當前子文件夾...?

+2

需要[延遲膨脹(http://ss64.com/nt/delayedexpansion.html) – npocmaka

+1

線'回波subdir'應該是'回聲%subdir%',對吧? – aschipfl

+0

@aschipfl是的,你是對的,我的錯字。 –

回答

0

你的使用延遲擴展碼:

@echo off 
setlocal EnableDelayedExpansion 
set "curdir=%cd%" 

for /d %%D in (*) do (
    echo %%~fD 

    set "subdir=%%~fD" 
    echo !subdir! 

    for /r "!subdir!" %%i in (*.plt) do (
     echo %%i 
    ) 

    echo ______DONE______ 
) 

cd "%curdir%" 
endlocal 
+1

)正如@Aacini指出的那樣,如果僅僅使用命令修飾符,則不需要使用延遲擴展。將FOR變量賦值給環境變量沒有意義。 – Squashman