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

PolII
  • 107
  • 8
  • 1
    missing `)`. Do you execute this code in a batch-file or directly on the command line (you tagged both)? Powershell is *very* different. – Stephan Jul 12 '21 at 10:05
  • wrt "whether there is a difference between the code for cmd.exe and for PowerShell." - you seem to have forgotten to include the PowerShell code you want a comparison with? – Mathias R. Jessen Jul 12 '21 at 10:12
  • 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 [[tag: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. – Compo Jul 12 '21 at 10:50
  • @Stephan I want to execute this in a batch-file. I tagged both because I thought of cmd as the basis of batch-files. – PolII Jul 12 '21 at 11:29
  • 3
    @PolII, just to expand upon Stephans query a little, whilst a [[tag:batch-file]] is run via [[tag: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.` – Compo Jul 12 '21 at 11:47
  • @Compo Yes, I noticed that difference description - but thank you for pointing it out! But out of curiosity: What would be the .bat pendant for PowerShell, i.e. what would be the scripting version for PowerShell? – PolII Jul 12 '21 at 11:54
  • 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 }`. – Compo Jul 12 '21 at 12:04
  • @Compo Thank you for your answer and adding the PowerShell bit! – PolII Jul 12 '21 at 12:13
  • @Stephan Thank you for your answer - I rearranged brackets in that logic and still get the same error. Is there a way to execute .bat files and get error messages that point to the specific line number at which the script failed? – PolII Jul 12 '21 at 12:44
  • 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 (` – Stephan Jul 12 '21 at 12:48
  • @Stephan Oh yeah, that makes sense and is something I also could have thought of. Thank you very much for your correction of my if line statement! Now the .bat script works - thank you all for your help and for enduring me, someone who typically uses Windows only rarely. – PolII Jul 12 '21 at 13:36

1 Answers1

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 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)
)
PolII
  • 107
  • 8