1

I am trying to create a menu that allows the user to drill down into a folder in order to batch copy and sync files. However, I am having a tough time accessing array values at the indices the user inputs. In the for loop they display fine so it looks like they are captured correctly in the array.

But when I try to set them or echo them later I don't get the correct output.

Not sure what I am doing wrong here.

:DirectoryContents
setlocal EnableDelayedExpansion
if exist "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%" (
    echo The sub-folders in this folder on the share drive are: 
    echo.
    Pushd  "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%"
    REM dir /b /ad
    set /a ID=0

    for /d %%a in (*) do (
        set folderName[%ID%]=%%a
        echo (!ID!^) %%folderName[%ID%]%%
        set /a ID=ID+1
    )
        echo.
    echo Please, select an assembly
    SET /P "selection=Input>"
    SET assemblyName=!folderName[%selection%]!
    echo assemblyName !assemblyName!

    call echo Folder0 !folderName[%0%]!
    call echo Folder1 !folderName[%1%]!
    call echo Folder2 !folderName[%2%]!

    echo test !folderName[%selection%]!
    call echo test2 %%folderName[%selection%]%%

    pause


    echo This folder does not exist on the shared drive

)
endlocal
REM pause
popd
exit /b

folder structure

cmd


Ok so based on feedback i made some updates to the code. I'm confident now that the folder names are being properly stored in the array index. I can easily see this by hard coding the index.

So echo Folder0 !folderName[0]! returns the name of the first folder etc.

However, after changing

set folderName[%ID%]=%%a
echo (!ID!^) %%folderName[%ID%]%%

to

set folderName[!ID!]=%%a
echo (!ID!^) %folderName[!ID!]%

I get a blank following the index number. Ex. (1)

Also the assemblyName variable isn't being set.

I tried

    SET assemblyName=!folderName[%selection%]!
    call echo assemblyName !assemblyName!

and

    SET assemblyName=%folderName[!selection!]%
    call echo assemblyName !assemblyName!

both return blanks.

Ultimately what I am trying to do is use the assemblyName variable in the network path to access the folder.

Pushd  "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%\!assemblyName!\"

changes

  • echo (!ID!^) %%folderName[%ID%]%% should be echo (!ID!^) !folderName[%ID%]! i was just testing something there – user5504571 Mar 10 '20 at 21:21
  • Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – Squashman Mar 10 '20 at 21:34
  • I would also read: [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script) – Squashman Mar 10 '20 at 21:36
  • @Squashman those links are very informative and have helped me fix a couple issues. But I still can't seem to both set a variable to an array index using the user input or echo it. – user5504571 Mar 11 '20 at 14:35

2 Answers2

1

you should be utilising delayed expansion (!expansion!). Replace:

set folderName[%ID%]=%%a

with

set folderName[!ID!]=%%a

To access the variable outside the loop it's assigned, For single Elements, use the index number.

echo Folder0 %folderName[0]%

Alternately, to access the entire array, you could use a For /L loop like so:

For /L %%A in (0,1,%ID%) do (Echo(!folderName[%%A]!)

Lastly, when incrementing a variable using Set /A, it does need need to reference itself:

Set /A ID+=1 Will suffice.

T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • Thanks for the reply. I didn't realize there was a difference between ! and % for delayed variables. I thought just enabling delayed expansion took care of that. I am still having a few issues `echo Folder0 !folderName[0]` now correctly display the contents of the indices. However after I changed `set folderName[!ID!]=%%a` the echo after it does not display the array index contents `echo (!ID!^) %folderName[!ID!]%`. – user5504571 Mar 11 '20 at 14:19
  • The issue again is the result of an incorrect attempt to expand the variable. the correct usage is `!folderName[%ID%]!` – T3RR0R Mar 11 '20 at 15:29
  • Unless you're comparing or sorting elements within the array, you wouldn't normally use the variable within the loop, you would just use the For loop variable, %%A in your case. – T3RR0R Mar 11 '20 at 15:33
  • i did try `!folderName[%ID%]!` and that didn't work. I included a picture of the output in my post. However, using %%a inside the loop does exactly what I want. Thank you! The only remaining issue I have is when I am outside the loop and trying access a user selected index and store it in assemblyName. I tried !folderName[%selection%]! but just returns a blank. – user5504571 Mar 11 '20 at 15:42
0

Finally figured it out. Code is below.

:DirectoryContents
setlocal EnableDelayedExpansion
if exist "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%" (
    echo The sub-folders in this folder on the share drive are: 
    echo.
    Pushd  "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%"
    REM dir /b /ad
    set /a ID=0

    for /d %%a in (*) do (
        set folderName[!ID!]=%%a
        echo (!ID!^) %%a
        set /A ID=ID+1
    )
    echo.
    echo Please, select an assembly
    SET /P "selection=Input>"
    echo !selection!

    call SET assemblyName=%%folderName[!selection!]%%
    echo assemblyName !assemblyName!


    pause

    if exist "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%\!assemblyName!\" (
        Pushd  "\\test\proj\dpa3\EXTERIOR\VCSE DVA Sync\%programNameAndYear%\!assemblyName!\"
        echo exists
        dir /b /ad
    ) else (
        echo This folder does not exist on the shared drive
    )

)
endlocal
REM pause
popd
exit /b