I want to run it repeatedly with the variables specified in the Windows batch file.

Asked 2 years ago, Updated 2 years ago, 61 views

I would like to perform a series of iterations on a particular string in a Windows batch file.
I wrote the batch file as below, but the error message echo is off appears.
I searched, but it said that if there is a description of @echooff~~, it can be processed without any problems, so I corrected it, but it doesn't work.

@echo off
setlocal enabled delayed expansion

~~~~~~
~~~~~~

set target_1 = "aaaaaa"
set target_2 = "bbbbbbb"
set target_3 = "cccccc"
set target_4 = "dddddd"
set target_5 = "eeeeeeee"

for /l%%i in (1,1,5)do(
  echo target_%%1
  Specific treatment target_%%1
)

→I have revised the contents as follows.
"The output of echo is ""target_1""."
If you know how to deal with it, please let me know

@echo off
setlocal enabled delayed expansion

~~~~~~
~~~~~~

set target_1 = "aaaaaa"
set target_2 = "bbbbbbb"
set target_3 = "cccccc"
set target_4 = "dddddd"
set target_5 = "eeeeeeee"

for /l%%i in (1,1,5)do(
  set n = %%1
  echo target_!n!

  if exist target_!n!(
     ~~~~
  )
)

batch-file

2022-09-30 19:27

2 Answers

As h.toki said, set must not have spaces before or after =.
Also, if there is no space between for and /l, you will get an error.
Use !var! for the delay deployment of the variable var.
I tried using a subroutine call to further expand the name as a variable name.

@echo off
setlocal enabled delayed expansion

set target_1 = "aaaaaa"
set target_2 = "bbbbbbb"
set target_3 = "cccccc"
set target_4 = "dddddd"
set target_5 = "eeeeeeee"

for /l%%i in (1,1,5)do(
  set n = %%i
  echo target=target_!n!
  call —subtarget_!n!
)
exit /b

—sub
US>echo! %1!


2022-09-30 19:27

In general, use SHIFT to shift the arguments one by one.CALLis

call [drive:][path]<filename>[<batchparameters>][:<label>[<arguments>]]

The syntax allows you to add arguments after the label.Arguments are assigned to variables %1, %2, %3...Even if you don't use CALL

 hoge.bat aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

When the and process start, the arguments are assigned to variables %1, %2, %3….

Then, shift the arguments in SHIFT and loop them until they are empty.

@echo off
call —loop aaaaaaaaaaabbbbcccccccddddddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
goto —eof

—loop
if"%1"=="goto:eof

echo%1

shift
goto —loop


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.