Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there an enhanced Type command which can read a specific line from a file?
I need to process a csv file by each line as I have 3 different commands which need to run for each line. I will be using a dos batch file to call the csv but using the type command shows the whole file in one go.

That doesn't appear to function as I need it too.
I have a .csv file that looks like this...
IP Address,Location,Device
10.0.0.1,Office1,1234
10.0.0.2,Office2,4321
10.0.0.3,Office3,1122
10.0.0.4,Office4,2233
10.0.0.5,Office5,3344
10.0...... and so onI then want to seperate each line into variables but by each line only...
Starting with the 2nd line:
SET Collumn1=IPADD
SET Collumn2=OFFICE
SET Collumn3=DEVICEI then want to process that line of data before proceeding to the next line:
http://%IPADD%/command1.cmd
http://%IPADD%/command2.cmd
http://%IPADD%/command3.cmd
echo successfully processed %OFFICE%,%DEVICE >> output.txtAnd then move onto the next line in the csv file and follow the same set of commands for each and every line.

Totally untested. Note that I had coded the http lines exactly as you stated but on this forum they were expanded to what you see below. I have no html experience, maybe you can make some sense of it.
:: Code begins... @echo off cls setlocal enabledelayedexpansion for /f "skip=1 tokens=1-3 delims=," %%1 in (filename.csv) do ( set IPADD=%%1 set OFFICE=%%2 set DEVICE=%%3 & call :process ) exit /b :process <a href="http://!IPADD!/command1.cmd" target="_blank">http://!IPADD!/command1.cmd</a> <a href="http://!IPADD!/command2.cmd" target="_blank">http://!IPADD!/command2.cmd</a> <a href="http://!IPADD!/command3.cmd" target="_blank">http://!IPADD!/command3.cmd</a> echo successfully processed !OFFICE!,!DEVICE! >> output.txt goto :eof :: Code ends...

This is perfect. I've just imported it into my batch script and it processes the various commands required for each line separately.
This means I can just keep a simple .csv file up to date without having to re-format it every time into a .bat format to perform my commands.
This is a very simple method of processing a .csv file... perfect!
Thank you very much!
p.s. I'm using cURL to process the web addresses and commands so they wouldn't actually be html web addresses but cURL and then the %IP%.

Well, that's very good news. Thank you for coming back to report your success.
My knowledge level doesn't allow me to understand your p.s. but if you're happy I can live with that for the time being.
Glad to have been able to assist. Good luck.

Seems to me the setting of vars and the sub are unnecessaary.
===========================
@echo off > newfile & setLocal EnableDelayedExpansionfor /f "skip=1 tokens=1-3 delims=," %%a in (my.csv) do (
echo http://%%a/command2.cmd
echo http://%%a/command3.cmd
echo successfully processed %%b, %%c >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

I would prefer to use the vars so I can use them elsewhere throughout my batch script and easily reference to them as nd when required. I just gave the example above to simplify my question and vars are perfect.
I do have another query though... Can I ignore commas in enclosed quotations?
There is one collumn I have which includes an address which uses commas to seperate each section of the address. This works fine in the csv file as it's enclosed in quotations but the batch file obviously reads each comma and doesn't work unless I remove the collumn out of the csv file.
Any ideas?

M2G - Thank you for your input and guidance. I have always been a bit verbose, perhaps it's in the genes.
JonMc - Sorry, I don't know how to achieve what you want, perhaps M2G can come up with a solution. You are correct tho', the script as written will delimit .csv file entries on every comma it encounters. Can you post the format of the line in question so that we can see what we're trying to deal with and indicate if this line is in a fixed location within the file? It may be possible to change the delims just for that line perhaps by splitting the source .csv file into three (or more) files then reading the multiple files to perform the commands and writing to one contiguous file again. Hopefully M2G can come up with a much simpler solution.
V.

Well, if we take my above example and expand on it a bit, the csv file could look like this:
IP Address,Location,Address,Device 10.0.0.1,Office1,"99 Acme Road, Acme Town, Acmeville, AC9 9ME",1234 10.0.0.2,Office2,"10 Vale Road, Vale Town, Valeville, VA9 9LE",4321 10.0.0.3,Office3,"22 Some Road, Some City, SO9 9ME",1122 10.0.0.4,Office4,"77 That Close, That Town, That City, Thatsville, TH9 9AT",2233 10.0...... and so on
Appologies for the un-imaginitive names I've used. Anyhow, the point is that there are some lines with 4 seperators in the address, others with 5 and others with just 3 so the differences could range throughout the data I have.I was going to reference that data based on errorlevel checking of the commands from the IP Address. For example, I would run the 3 commands required for each IP Address, as noted above and then setup an auto email to say the device %IP% in %OFFICE%, %LOCATION%, %ADDRESS% cannot be accessed, please restart.... or something to that effect. This is why the variables are quite important to me.
Now that I know how to access a csv file from a batch file and process commands on IP addresses, I'm comming up with loads of applications I could use this for. But I can see this quotation/comma issue cropping up every now and then so would be nice to somehow state anything between "quotations" to ignore.

M2G obviously hasn't had a chance to respond yet so here I go with an idea... I haven't included the http lines... just an echo to let you see what the vars contain. This appears to work whether or not there is an address in the .csv line. It's dependent on there being 4 digits in the Device. I have no doubt that M2G will be able to improve on my efforts.
@echo off cls setlocal enabledelayedexpansion for /f "skip=1 tokens=1-2* delims=," %%1 in (filename.csv) do ( set IPADD=%%1 set OFFICE=%%2 set DEVICE=%%3 set DEVICE=!device:~-4! & call :process ) exit /b :process echo ipadd=%ipadd% location=%office% device=%device%

Hi Garibaldi,
I see what your doing there with the 4 digits at the end but I don't think that can be a workable solution as not all the various different data I may be processing will definitely have a final column of 4 digits.
I think I will just change the separator for another character that will not be used such as # or | or ~.
I don't see any issues with that, do you?
Effectivly, my text based file would look like this:
IP Address#Location#Address#Device 10.0.0.1#Office1#"99 Acme Road, Acme Town, Acmeville, AC9 9ME"#1234 10.0.0.2#Office2#"10 Vale Road, Vale Town, Valeville, VA9 9LE"#4321 10.0.0.3#Office3#"22 Some Road, Some City, SO9 9ME"#1122 10.0.0.4#Office4#"77 That Close, That Town, That City, Thatsville, TH9 9AT"#2233 10.0...... and so on
and the batch would look like...@echo off cls setlocal enabledelayedexpansion for /f "skip=1 tokens=1-3 delims=#" %%1 in (filename.csv) do ( set IPADD=%%1 set OFFICE=%%2 set ADDRESS=%%3 set DEVICE=%%4 & call :process ) exit /b :process curl <a href="http://%IPADD%/command1.cmd" target="_blank">http://%IPADD%/command1.cmd</a> curl <a href="http://%IPADD%/command2.cmd" target="_blank">http://%IPADD%/command2.cmd</a> curl <a href="http://%IPADD%/command3.cmd" target="_blank">http://%IPADD%/command3.cmd</a> echo Successfully processed %OFFICE%, %DEVICE% at %ADDRESS%>>output.txt goto :eofDo you see any issues with that setup?
Thanks for all your help so far, couldn't have got this far without you!!

I think only one very small amendment to the script is needed, amend "tokens=1-3" to "tokens=1-4", can't test the coding in :process
Using your script, after amending, and the input data you provided, below is the screen output produced by just echoing the vars.
IPADD=10.0.0.1 OFFICE=Office1 ADDRESS="99 Acme Road, Acme Town, Acmeville, AC9 9ME" DEVICE=1234 IPADD=10.0.0.2 OFFICE=Office2 ADDRESS="10 Vale Road, Vale Town, Valeville, VA9 9LE" DEVICE=4321 IPADD=10.0.0.3 OFFICE=Office3 ADDRESS="22 Some Road, Some City, SO9 9ME" DEVICE=1122 IPADD=10.0.0.4 OFFICE=Office4 ADDRESS="77 That Close, That Town, That City, Thatsville, TH9 9AT" DEVICE=2233Whew! We must be close to putting this thread to bed.
Valerie.

Yeah, didn't increase the tokens for the extra collumn. I'll keep an eye out for that in future.
That's great though. Thanks for all your help, it's very much appreciated.
Jon.

What is cURL?
=====================================
If at first you don't succeed, you're about average.M2

I use it to query some web servers and download reports. There are several commands which I need to run on each webserver and then perform additional tasks depending on the results. That's why it was important to process my data file on a per line basis.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |