3

I have two text files, one stores the name of files I need to copy and one the addresses to where i want to copy them. My idea was to create two arrays to store that information and then loop through them both so that I always get the filename and the corresponding address. This is the code I use to store the information in the arrays:

@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%%
)

And then I loop through the two arrays and try to things with them:

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%
)

My problem is, that I can't seem to read the array through the indexes. set SUBPROJECT_DIR=%ADRESS_ARRAY[%%j]%and set PRESENTATION_NAME=%NAME_ARRAY[%%j]% don't seem to set the variables, they remain empty. Does anybody know why?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
UsefulUserName
  • 555
  • 1
  • 4
  • 19
  • 4
    Have a look at [delayed expansion](http://ss64.com/nt/delayedexpansion.html) – geisterfurz007 Sep 08 '16 at 12:36
  • 4
    You could either EnableDelayedExpansion as already suggested or use the call set method already implemented earlier in your code. – Compo Sep 08 '16 at 12:44
  • 2
    Array management details are described at [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Sep 08 '16 at 14:07

1 Answers1

1

Your suggestions brought me on the right track. This is the now working version, just in case somebody else has the same problem:

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%%
)
UsefulUserName
  • 555
  • 1
  • 4
  • 19