There is a long history here on Stack Overflow in questions about iterating variable assignment within for-loops. The most prominent one is the solution in bash:
#!/bin/bash
for i in {1..28}
do
if [ $i -le 9 ]
then
cd /change/to/target/folder/and/numerated/sub/folder0${i}
sh RunScript.sh data_0${i}.file
else
cd /change/to/target/folder/and/numerated/sub/folder${i}
sh RunScript.sh data_${i}.file
fi
done
(The difference before and after the else is the 0 before the ${i})
This in-line shell expansion of iterated variable names in loops has also been discussed here on Stack Overflow for python and R.
Here I want to ask the question how to do such an operation in .bat files, one time executed with cmd.exe and also executed with Microsoft PowerShell, if there is a difference there.
So far, my code looks like this:
FOR /L %i IN (1,28) DO (
pscp [email protected]:/path/to/target/folder/and/numerated/sub/folder%i/data_%i.file H:\path\to\target\folder\data%i.file
server_password
Apparently, this code does not work, but (a) I would like to know what code would work and (b) whether there is a difference between the code for cmd.exe and for PowerShell.
EDIT:
I now do have this:
@ECHO OFF
FOR /L %%i IN (1,1,28) DO (
IF (%%i LEQ 9) (
pscp H:\path\to\iterated\directory\subfolder%%i\and\files\*.as [email protected]:/path/to/iterated/directory/subfolder0%%i/files/
serverpassword
) ELSE (
pscp H:\path\to\iterated\directory\subfolder%%i\and\files\*.as [email protected]:/path/to/iterated/directory/subfolder%%i/files/
serverpassword)
)
Yet I get an error message saying: ""(" cant be processed syntactically at this point"".
). Do you execute this code in a batch-file or directly on the command line (you tagged both)? Powershell is very different.for /?, press the[ENTER]key, and read the usage information for the command, paying particular attention to the syntax for aFor /Lcommand,FOR /L %variable IN (start,step,end) DO command [command-parameters], yours does not match that.For /Lpart, because in doing so you'll miss, some other vital information, regarding an imporatant difference between the code in a batch file or directly in cmd.To use the FOR command in a batch program, specify %%variable instead of %variable.1..28 | % { your command here using $_ as the incremented number }, but as with many things there may be more than one method and another may be more suitable,for ($i=1; $i -le 28; $i++) { your command here using $i as the incremented number }.echo onand see what's really executed, and where it exactly stops. Btw: yourifline: you compare the string(9to the string9), which is definitively not what you want. Useif %%i LEQ 9 (