0

I'm using Windows 10 64 bit and I'm trying to write a BAT file. I have the following code that is not fully functional.

@Echo off
SETLOCAL enabledelayedexpansion
SET value1=2020_01_19_17_00_01
SET Arr1[0]=_f104.sql
SET Arr1[1]=_f105.sql
SET Arr1[2]=_f106.sql
SET Arr1[3]=_f107.sql
SET Arr1[4]=_f108.sql
SET Arr1[5]=_f109.sql
REM ::read it using a FOR /L statement
For /l %%n in (0,1,5) do (
Echo The value of the concatinated values is now: C:\SS_Library_Backups\%value1%!Arr1[%%n]!
SET filename=C:\SS_Library_Backups\%value1%!Arr1[%%n]!
Echo The value of the variable filename is now: %filename%
)

This produces the resutls

C:\SS_Library_Backups>test4
The value of the concatinated values is now: C:\SS_Library_Backups\2020_01_19_17_00_01_f104.sql
The value of the variable filename is now: ""
The value of the concatinated values is now: C:\SS_Library_Backups\2020_01_19_17_00_01_f105.sql
The value of the variable filename is now: ""
The value of the concatinated values is now: C:\SS_Library_Backups\2020_01_19_17_00_01_f106.sql
The value of the variable filename is now: ""
The value of the concatinated values is now: C:\SS_Library_Backups\2020_01_19_17_00_01_f107.sql
The value of the variable filename is now: ""
The value of the concatinated values is now: C:\SS_Library_Backups\2020_01_19_17_00_01_f108.sql
The value of the variable filename is now: ""
The value of the concatinated values is now: C:\SS_Library_Backups\2020_01_19_17_00_01_f109.sql
The value of the variable filename is now: ""

It seems that I can't use the SET statement within the parenthesis. I don't know why an "Echo" works but a "Set" statement does not. I need to process several statements for the loop, like "If", "Goto", etc. Is there something I have to do to enable statements other than "Echo" within a parenthesis? Thanks for looking at this.

user3138025
  • 795
  • 4
  • 17
  • 46

1 Answers1

0

I found an answer. It has to do with the statement:

SETLOCAL enabledelayedexpansion

See the link How to set a variable inside a loop for /F

After reading that post, I changed the way I referenced variables which were created or modified inside the FOR / DO loop. Specifically, when I used exclamation points (!) instead of percentage (%), the code worked fine. Changing the last statement to:

Echo The value of the variable filename is now: !filename!

produced expected results.

user3138025
  • 795
  • 4
  • 17
  • 46