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 user@server.url.de:/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 user@server.url.de:/path/to/iterated/directory/subfolder0%%i/files/
serverpassword
) ELSE (
pscp H:\path\to\iterated\directory\subfolder%%i\and\files\*.as user@server.url.de:/path/to/iterated/directory/subfolder%%i/files/
serverpassword)
)
Yet I get an error message saying: ""(" cant be processed syntactically at this point"".