2016-09-08 152 views
3

我有兩個文本文件,一個存儲我需要複製的文件的名稱和一個要複製到的位置的地址。我的想法是創建兩個數組來存儲這些信息,然後循環遍歷它們,這樣我總能得到文件名和相應的地址。這是我的代碼使用存儲陣列中的信息:數組索引不返回值

@echo off 
set /A i=0 
set x=0 

for /F "usebackq delims=" %%a in ("Name.txt") do (
    set /A i+=1 
    call echo %%i%% 
    call set NAME_ARRAY[%%i%%]=%%a 
    call set n=%%i%% 
) 

set /A i=0 

for /F "usebackq delims=" %%a in ("Adress.txt") do (
    set /A i+=1 
    call echo %%i%% 
    call set ADRESS_ARRAY[%%i%%]=%%a 
    call set n=%%i%% 
) 

然後我遍歷兩個數組,並嘗試的東西與他們:

for /L %%j in (1,1,%n%) do (
:: subproject dir, relative to the sandobox dir 
set SUBPROJECT_DIR=%ADRESS_ARRAY[%%j]% 
:: sandbox name 
set SANDBOX_NAME=C:\%SANDBOX_FOLDER_NAME%\%SUBPROJECT_DIR%\project.pj 
:: name of sandbox folder 
set SANDBOX_DIR=C:\%SANDBOX_FOLDER_NAME%\%SUBPROJECT_DIR% 
:: name of presentation to be copied 
set PRESENTATION_NAME=%NAME_ARRAY[%%j]% 

:: check out file with software version number 
si co --sandbox=%SANDBOX_NAME% --changePackageId=:none --hostname=%SERVERNAME% --port=%PORTNAME% %SANDBOX_DIR%\%PRESENTATION_NAME% 

:: Copying new files (Option /xo of robocopy) 
robocopy x: %SANDBOX_DIR% %PRESENTATION_NAME% 

:: check in modified file 
si ci --sandbox=%SANDBOX_NAME% --changePackageId=:none --hostname=%SERVERNAME% --port=%PORTNAME% %SANDBOX_DIR%\%PRESENTATION_NAME% 
) 

我的問題是,我可以似乎沒有通過索引讀取數組。 set SUBPROJECT_DIR=%ADRESS_ARRAY[%%j]%set PRESENTATION_NAME=%NAME_ARRAY[%%j]%似乎沒有設置變量,它們仍然是空的。有人知道爲什麼嗎?

+4

看一看[延遲擴展(http://ss64.com/nt/delayedexpansion.html) – geisterfurz007

+4

你既可以如已經建議的那樣使用EnableDelayedExpansion,或者使用之前在代碼中已經實現的調用集方法。 – Compo

+2

數組管理細節描述在[這個答案](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990# 10167990) – Aacini

回答

1

您的建議使我走上了正確的軌道。這是現在的工作版本,以防萬一別人有同樣的問題:

for /L %%j in (1,1,%n%) do (
:: subproject dir, relative to the sandobox dir 
call set SUBPROJECT_DIR=%%ADRESS_ARRAY[%%j]%% 
:: sandbox name 
call set SANDBOX_NAME=C:\%SANDBOX_FOLDER_NAME%\%%SUBPROJECT_DIR%%\project.pj 
:: name of sandbox folder 
call set SANDBOX_DIR=C:\%SANDBOX_FOLDER_NAME%\%%SUBPROJECT_DIR%% 
:: name of presentation to be copied 
call set PRESENTATION_NAME=%%NAME_ARRAY[%%j]%% 

:: check out file 
call si co --sandbox=%%SANDBOX_NAME%% --changePackageId=:none --hostname=%SERVERNAME% --port=%PORTNAME% %%SANDBOX_DIR%%\%%PRESENTATION_NAME%% 

:: Copying new files (Option /xo of robocopy) 
call robocopy x: %%SANDBOX_DIR%% %%PRESENTATION_NAME%% 

:: check in modified file 
call si ci --sandbox=%%SANDBOX_NAME%% --changePackageId=:none --hostname=%SERVERNAME% --port=%PORTNAME% --description="Automated weekly update." %%SANDBOX_DIR%%\%%PRESENTATION_NAME%% 
)