-1

Trying to read username and password from text file, each username and password separated on new line in txt file. Not able to iterate over the array and get first element and second element from array and assign to varUser and varPass, then third and fourth and so on. These two temporary variables will be used as parameter for winScp console for login sftp server. The script.txt file has command to connect that takes varUser and varPass as parameter from batch file.

rem 1. reading text file and filling in array

for /F "usebackq delims=" %%a in (file.txt) do (
    set /A i+=1
    call echo i=%%i%%
    call set array[%%i%%]=%%a
    call set lastIndex=%%i%%
)

rem 2. reading array values from filled array and store in temp varUser and varPass

set /a i=0
set /a j=1
for /L %%i in (1,2,%lastIndex%) do (
    call echo %%array[%%i]%%
    set varUser=%%array[%%i]%%
    set varPass=%%array[%%j]%%
    winscp.com /script=script.txt /parameter put %varUser%,%varPass%
    set /a j+=1
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • If you set **and use** an environment variable in a (code block) you need [DelayedExpansion](https://ss64.com/nt/delayedexpansion.html) –  May 10 '19 at 19:11
  • 1
    Also your question is unclear, where do you fill the array? There is no for variable `%%j` and `%j%` is set and incremented but never used. –  May 10 '19 at 19:18
  • i am filling the array in fist loop by reading the txt file and second loop trying to iterate the previously stored value and put it in temp variable varUser and varPass, i am trying to do something like varUser=a[i] and varPass=a[i+1] in the second loop. – Nikhil_buzz_light_year May 11 '19 at 02:27

4 Answers4

2

just to give an alternative without an array and without for:

@echo off
<file.txt call :read
goto :eof

:read
  set "user="
  set "pass="
  set /p user=
  set /p pass=
  if "%user%%pass%"=="" goto :eof
  echo %user%, %pass%
goto :read

Assuming following (suboptimal) format for file.txt:

user1
password1
user2
password2
user3
password3
Stephan
  • 53,940
  • 10
  • 58
  • 91
1

This assumes your file.txt is constructed as:

user1 password1
user2 password2
user3 password3
...

This does exactly what you attempted in 2 loops (besides attempting to set an array)

@echo off
setlocal enabledelayedexpansion    
for /F "usebackq tokens=1,*" %%a in ("file.txt") do (
    set varUser=%%a
    set varPass=%%b
    echo user=!varUser! password=!varPass!
    winscp.com /script=script.txt /parameter put !varUser!,!varPass!
)

That is pretty unneeded though as you can simply use the metavariables as is to run the commands.

@echo off
for /F "usebackq tokens=1,*" %%a in ("file.txt") do (
    winscp.com /script=script.txt /parameter put %%a,%%b
)

If you really want to set an array and use it like you did, for an unknown reason:

@echo off
setlocal enabledelayedexpansion
set cnt=0
for /f "usebackq tokens=1,*" %%a in ("file.txt") do (
    set /a cnt+=1
    set varUser[!cnt!]=%%a
    set varPass[!cnt!]=%%b
)

for /l %%i in (1,1,%cnt%) do (
    echo user=!varUser[%%i]! password=!varPass[%%i]!
)

If on the other hand your file.txt is in complete list form, like:

user1
password1
user2
password2
user3
password3
...

Then this is probably what you wanted to get to :)

@echo off
setlocal enabledelayedexpansion
set cnt=0
for /f "usebackq delims=" %%a in ("file.txt") do (
    set /a cnt+=1
    set array[!cnt!]=%%a
)

set handler=0
for /l %%i in (1,1,%cnt%) do (
    set /a handler+=1
    call set varUser=%%array[!handler!]%%
    set /a handler+=1
    call set varPass=%%array[!handler!]%%
    echo user=!varUser! password=!varPass!
    winscp.com /script=script.txt /parameter put !varUser!,!varPass!
    if "!handler!"=="!cnt!" goto :eof
)

This last example is what you seem to want, based on your last comment.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
1
@echo off
setlocal
set "varUser="

for /f "delims=" %%A in (file.txt) do (
    if defined varUser (
        call winscp.com /script=script.txt /parameter put %%varUser%%,%%A
        set "varUser="
    ) else set "varUser=%%A"
)

1st loop cycle to set the value of varUser and the 2nd loop cycle to execute winscp. It will keeping looping until reaches end of file. Perhaps easier than what you tried with a variable name sequence.

This will work with reading a file such as:

jack
boy
jill
girl!

If you put the user and the password on the same line for each user, then you could use:

@echo off
setlocal

for /f "tokens=1,2 delims= " %%A in (file2.txt) do (
    winscp.com /script=script.txt /parameter put %%A,%%B
)

This will work with reading a file such as:

jack boy
jill girl!
michael_heath
  • 5,262
  • 2
  • 12
  • 22
1

Just another variant, using

  • findstr /n to read the file and number the lines
  • a for /l with step 2 to process the Array

With a file UserPass.txt:

user1
password1
user2
password2
user3
password3

This batch:

:: Q:\Test\2019\05\10\SO_56083497.cmd
@Echo off&SetLocal EnableDelayedExpansion

for /f "tokens=1*delims=:" %%i in ('findstr /N "." ^<".\UserPass.txt"'
    ) do Set "Array[%%i]=%%j"&Set n=%%i

for /l %%i in (1,2,%n%) do (
    Set /a j=%%i+1
    Call Set "varPass=%%Array[!j!]%%"
    echo winscp.com /script=script.txt /parameter put !Array[%%i]!,!varPass!
)

yields this output (remove the echo in front of winscp)

> Q:\Test\2019\05\10\SO_56083497.cmd
winscp.com /script=script.txt /parameter put user1,password1
winscp.com /script=script.txt /parameter put user2,password2
winscp.com /script=script.txt /parameter put user3,password3