0

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"".

11
  • 1
    missing ). Do you execute this code in a batch-file or directly on the command line (you tagged both)? Powershell is very different. Commented Jul 12, 2021 at 10:05
  • 2
    …although, questions should be limited to one only, so asking for a PowerShell example, and a Batch File example in the same question would be wrong. I have therefore removed your [powershell] tag, to concentrate only on the issue for which you've provided code. For that, the first thing I'd advise is that you open a Command Prompt window, type for /?, press the [ENTER] key, and read the usage information for the command, paying particular attention to the syntax for a For /L command, FOR /L %variable IN (start,step,end) DO command [command-parameters], yours does not match that. Commented Jul 12, 2021 at 10:50
  • 3
    @PolII, just to expand upon Stephans query a little, whilst a [batch-file] is run via [cmd], there are some things which need to be taken into account. So, regarding my previous advice, when you read the usage information for your command, do not just skip to the For /L part, 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. Commented Jul 12, 2021 at 11:47
  • 1
    I could say 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 }. Commented Jul 12, 2021 at 12:04
  • 2
    no. You can run it with echo on and see what's really executed, and where it exactly stops. Btw: your if line: you compare the string (9 to the string 9), which is definitively not what you want. Use if %%i LEQ 9 ( Commented Jul 12, 2021 at 12:48

1 Answer 1

0

To those who happen to have the same question as I did, as an answer here is the code that works for the for-loop/if structure with an incremented variable as a batch script:

@ECHO OFF
FOR /L %%i IN (1,1,28) DO (    ::The numbers refer to start, step and end
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)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.