2016-07-20 181 views
0

我有點卡在這裏。 運行包含幾個步驟的批處理文件。 我需要運行一個文件夾中的批處理文件,通過每個文件夾和3個文件夾向下分配一個變量。所以複製第三層文件夾的每個文件夾 所以我的文件夾結構將變量傳遞給子文件夾

e.g1在下降:2135698563325 \ Folder1中\文件夾2 \ Folder3 \ C007

EG2:21356486543248 \ Folder1中\文件夾2 \ Folder3 \ C111

REM get c007 as a variable to be able to set a folder name 
set variable = Folder0\Folder1\Folder2\Folder3\%Variable% 

REM Step 2:Check that the c007 folder doens't already exist 
if %Variable%==\\hippo\Folder4\ ((echo "Error: Duplicate Folder"):eof) Else mkdir \\hippo\Folder4\%Variable% 

REM Step 3:Copy a default File Structure from Template Dir 
xCopy /s \\hippo\production\Folder4\Temaplate \\hippo\production\Folder4\%Variable% 
Rem Step 4: Copy the contents of c007 in to Folder6 
xCopy /s %Variable% \\hippo\production\Folder4\Variable\Folder5\Folder6\ 

這是否更有意義?

+3

應該是'%變量%'的',而不是在所有的代碼variable'。 –

+0

好吧,所以我可以改變,但仍然不知道如何去通過每個文件夾,並貶低一個變量的第三個文件夾? –

+1

你能用一個你得到什麼和你需要什麼的例子來編輯你的文章嗎?我在這裏有點失落。例如腳本的第二行不能真正按照原樣運行。 –

回答

0

以下是關於如何獲得c007c111的註釋批處理腳本。把你的代碼放入子程序ProcessFolder。文件夾名稱c007c111通過引用%~1的第一個參數傳遞給子例程。

@echo off 
setlocal EnableExtensions 

rem Get current directory path always without backslash at end. Environment 
rem variable CD holds the current directory path usually without backslash 
rem at end. But current directory string is with backslash at end if the 
rem current directory is the root directory of a drive. 
if "%CD:~-1%" == "\" (
    set "CurrentFolder=%CD:~0,-1%" 
) else (
    set "CurrentFolder=%CD%" 
) 

rem Process the folder names 4 levels below current directory. 
for /D %%A in ("%CurrentFolder%\*") do (
    for /D %%B in ("%%A\*") do (
     for /D %%C in ("%%B\*") do (
      for /D %%D in ("%%C\*") do (
       for /D %%E in ("%%D\*") do (
        call :ProcessFolder "%%~nxE" 
       ) 
      ) 
     ) 
    ) 
) 

endlocal 
rem Avoid a fall through to the subroutine by exiting batch processing here. 
goto :EOF 

rem Subroutine to process the folder with name found above. 

:ProcessFolder 
echo Folder variable is: %~1 

rem Exit subroutine and continue in the most inner FOR loop above. 
goto :EOF 

對於理解使用的命令以及它們如何工作,打開命令提示符窗口中,執行有下面的命令,並完全讀取顯示每個命令的所有幫助頁面非常謹慎。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?