Batch file get variables from CSV and execute one line at a time

I'm stumped on this one.  I'm able to get the variables from the CSV but rather than running the execution part of the script, it continues to dump all the CSV as variables first (only the last line of the CSV is passed to the actual work part of the script).   The CSV has 3 columns that are being with each being used as a variable.

C:\>SET SN=servername1   & SET SRC=OldDS-1   & SET DEST=NewDS1

C:\>SET SN=servername2   & SET SRC=OldDS-1   & SET DEST=NewDS1

C:\>SET SN=servername3   & SET SRC=OldDS-1   & SET DEST=NewDS1

C:\>server=nowhere.com --username=domain\username --password=password --datacent
er="Development" --vm=[OldDS-1 ]servername3 /servername3 .vmx:NewDS1

 I'm probably forgetting something obvious.

SET VCENTER=nowhere.com
SET USER=domain\username
SET PASS=password
SET DC="Development"
SET INPUTFILE=c:\svmotion.csv
FOR /f "tokens=1,2,3 delims=," %%i IN ('type "%INPUTFILE%"') DO SET SN=%%i & SET SRC=%%j & SET DEST=%%k 
someperl.pl --server=%VCENTER% --username=%USER% --password=%PASS% --datacenter=%DC% --vm=[%SRC%]%SN%/%SN%.vmx:%DEST%

Open in new window

slayer199Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You need to use brackets to build a command block for FOR. Further you need to enable delayed expansion (!var!):

@echo off
setlocal EnableDelayedExpansion
SET VCENTER=nowhere.com
SET USER=domain\username
SET PASS=password
SET DC="Development"
SET INPUTFILE=c:\svmotion.csv
FOR /f "tokens=1,2,3 delims=," %%i IN ('type "%INPUTFILE%"') DO (
   SET SN=%%i
   SET SRC=%%j
   SET DEST=%%k 
   someperl.pl --server=%VCENTER% --username=%USER% --password=%PASS% --datacenter=%DC% --vm=[%SRC%]%SN%/%SN%.vmx:%DEST%
)

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
On the other hand, if you do not use the vars again, just combine the FOR command into one:

echo off
setlocal EnableDelayedExpansion
SET VCENTER=nowhere.com
SET USER=domain\username
SET PASS=password
SET DC="Development"
SET INPUTFILE=c:\svmotion.csv
FOR /f "tokens=1,2,3 delims=," %%i IN ('type "%INPUTFILE%"') DO ^
someperl.pl --server=%VCENTER% --username=%USER% --password=%PASS% --datacenter=%DC% --vm=[%%j]%%i/%%i.vmx:%%k

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Correction for first version (I forgot to use ! instead of %):

@echo off
setlocal EnableDelayedExpansion
SET VCENTER=nowhere.com
SET USER=domain\username
SET PASS=password
SET DC="Development"
SET INPUTFILE=c:\svmotion.csv
FOR /f "tokens=1,2,3 delims=," %%i IN ('type "%INPUTFILE%"') DO (
   SET SN=%%i
   SET SRC=%%j
   SET DEST=%%k 
   someperl.pl --server=%VCENTER% --username=%USER% --password=%PASS% --datacenter=%DC% --vm=[!SRC!]!SN!/!SN!.vmx:!DEST!
)

Open in new window

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
BTW, if using several SETs in one line as in your code, be sure to not leave a space between the value and the next ampersand. The space will be part of the value else.

set ex=1234 & set ex2=another one
echo "%ex%"
will output  "1234 "
slayer199Author Commented:
I attempted the send solution and it didn't pass the variables.  Output looks like this:

C:\>(
SET SN=aa-estprod-dw5
 SET SRC=Vmax-Repl-04
 SET DEST=DMX800-2
 someperl.pl --server=nowhere.com --username=domain\username --password=password
 --datacenter="Development" --vm=[!SRC!]!SN!/!SN!.vmx:!DEST!
)
'someperl.pl' is not recognized as an internal or external command,
operable program or batch file.
slayer199Author Commented:
Also, it did run one line at a time from the CSV, but it didn't pass the variables.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Try the solution in http:#a30735520.
The output in http:#a30735861 is not fitting my script. It should not echo anything (because of the @echo off at the beginning). If you removed or omitted that line, the output is ok, but your perl script is not found, resp. it has no registered application. You might have to either prepend a start command, or the perl interpreter.

To test what would be executed, use the following code. The perl commandline will be echoed (and only that) for each line of your CSV file, and the variables should be replaced.

If you omit the @echo off, vars with ! will not be expanded on the output - so you can't use that for debugging.

@echo off
setlocal EnableDelayedExpansion
SET VCENTER=nowhere.com
SET USER=domain\username
SET PASS=password
SET DC="Development"
SET INPUTFILE=c:\svmotion.csv
FOR /f "tokens=1,2,3 delims=," %%i IN ('type "%INPUTFILE%"') DO (
   SET SN=%%i
   SET SRC=%%j
   SET DEST=%%k 
   echo someperl.pl --server=%VCENTER% --username=%USER% --password=%PASS% --datacenter=%DC% --vm=[!SRC!]!SN!/!SN!.vmx:!DEST!
)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slayer199Author Commented:
Your solution didn't quite work, but it gave me an idea that did.  I was making this overly complicated...your initial response of adding the perl script section into the DO statement simplified the script.  Here's what I used that worked:
SET INPUTFILE=c:\scripts\svmotion.csv
FOR /f "tokens=1,2,3 delims=," %%i IN ('type "%INPUTFILE%"') DO (
svmotion.pl --server=%VCENTER% --username=%USER% --password=%PASS% --datacenter=%DC% --vm=[%%j]%%i/%%i.vmx:%%k

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You can either use the caret (=> "continuation on next line"), as I showed, or brackets.
slayer199Author Commented:
Qlemo's script put me on the right path to a complete solution.  It was very helpful!
slayer199Author Commented:
Thanks...you definitely helped!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.