Solved

Help trying to pull variables from a tab delimited text file into a script.

Posted on 2013-11-08
6
490 Views
Last Modified: 2013-11-09
Hello, I am trying to pull 2 variables from a tab delimited text file to run a batch script.

I have one batch script which calls the other and the data from the text file.

For /F %%x %%y n (feed.txt) do import.bat %%x %%y

Then I input %1 for variable1 and %2 for variable2
When I run the import.bat file as follows "import.bat username card#" it works.

My text file is tab delimited.

variable1  variable2

Ideally I would like to have a text file user variable1 ignore variable2 and use variable3 but I can run another export with jsut the 2 variables. Thanks in advance.
0
Comment
Question by:njtreker
  • 3
  • 2
6 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 75 total points
ID: 39634978
If I understand you correctly, this should work:
For /F "tokens=1,3" %%x in (feed.txt) do import.bat %%x %%z

Open in new window

That should bring in variable 1 & 3, skipping variable 2...if not, then we need to know what the delimeter is...by default it's a space character.  But if your text file is broken up like:

data1,data2,data3

Then you could use:
For /F "tokens=1,3 delims=," %%x in (feed.txt) do import.bat %%x %%z

Open in new window


If that doesn't sort it - can you post an example of the file?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39635007
Agreed with sirbounty there, the default delims should work with tab OK, though I think if you are choosing the tokens you want:

For /F "tokens=1,3" %%x in (feed.txt) do call import.bat %%x %%y

Open in new window


or:

For /F %%x in (feed.txt) do call import.bat %%x %%z

Open in new window


The CALL to run the other batch file for each line in the text file and x/y if specifiying token 1&3, or x/z if not.

Also your other import.bat file, is it worth us incorporating the loop into that instead?

Steve
0
 

Author Comment

by:njtreker
ID: 39635645
I am a lot closer. Thanks! I can't get the batch file to read variable 3. When I run it it interprets it as %%z. If I change may launch file, change the variable number to 2 and the %%z to %%y, and remove variable 2 and just have 2 variables it works. Below are my files. I changed some of the data of course for privacy. Variable 1 is a username, Variable2 is a ID # and Variable3 is a card #. For this process I need username and card #

Files:

Procimportlaunch.bat

@echo off
rem Iterate through each line of the input file
rem For each line, execute the command with the current line content as a parameter

For /F "tokens=1,3" %%x in (feed.txt) do call import.bat %%x %%z

@echo on

Import.bat

c:
cd \Program Files\PaperCut MF\server\bin\win
server-command set-user-property %1 secondary-card-number %2

Feed.txt

fraxxxx      010712345      13123
maxxx      010912345      13123
jonxxxx      010912345      13123
turxxxxx      014123456      12345

Thanks again for your help. Worse case I have a way to do it,  but I would like to know how to skip variables for the future.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 425 total points
ID: 39635647
See my comment.  It is not %%z but %%y.

If you use "tokens=1,3" %%x in (...) then  %%x is the first token and %%y is the 3rd.
If you use "tokens=1,2*" %%x then %%x is first, %%y is secondm, %%z is the rest of the line
if you use no "tokens=" bit then %%x if first, %%y second, %%z third.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39635648
Seeing your import.bat actually just use:

@echo off
cd /d c:\Program Files\PaperCut MF\server\bin\win
for /f "tokens=1,3" %%x in (feed.txt) do (
  echo Working on %%x and %%y
  ECHO server-command set-user-property %%x secondary-card-number %%y
)

Remove the uppercase ECHO command when it looks right, it will just show to the screen for now.

Steve
0
 

Author Comment

by:njtreker
ID: 39635669
Steve, That's got it, I got confused with Sirbounty's post. Thanks so much both of you!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now