batch-fileplinkpscp

Interate Using Plink and PSCP


I am trying to iterate to various different Linux servers and scp the .cer files to a windows computer. I need to create a folder based on the var that is returned and then scp the file to that location. There seems to be something wrong with the For /F loop where it is creating each folder but it is only doing the scp copy to the first assignment of the variable. i.e all files go to the first folder created. Do I need to clear the variable each time - I am new to batch scripting. Thanks for your help.

REM @echo off
FOR /F %%A IN (addresses.txt) DO (
@echo %%A
FOR /F "tokens=* USEBACKQ" %%F IN (`plink -pw "password" "root@%%A" -m cmds.txt`) DO (
@echo %%F
SET var=%%F
mkdir c:\temp\certfiles\%%F
pscp -pw password -P 22 root@%%A:/Certificates/*.cer c:\temp\certfiles\%var%\
)
)

Solution

  • You need to understand Delayed Expansion Hence, invoke Delayedexpansion and then use !var! in place of %var%; although on second glance, since %%F is assigned to var, you can replace %var% with %%F in this case.