Solved

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

Posted on 2013-11-08
6
491 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 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