Enhanced Type command

May 10, 2009 at 17:19:17
Specs: Windows XP
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.

See More: Enhanced Type command

Report •

May 10, 2009 at 21:09:19
FOR loop?


For /f "delims=" %%1 in (filename.csv) do (
     commands here etc...

Report •

May 11, 2009 at 03:02:07
That doesn't appear to function as I need it too.

I have a .csv file that looks like this...
IP Address,Location,Device,Office1,1234,Office2,4321,Office3,1122,Office4,2233,Office5,3344
10.0...... and so on

I then want to seperate each line into variables but by each line only...
Starting with the 2nd line:
SET Collumn1=IPADD

I then want to process that line of data before proceeding to the next line:
echo successfully processed %OFFICE%,%DEVICE >> output.txt

And then move onto the next line in the csv file and follow the same set of commands for each and every line.

Report •

May 11, 2009 at 04:16:09
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

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

<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...

Report •

Related Solutions

May 11, 2009 at 05:23:48
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%.

Report •

May 12, 2009 at 00:30:11
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.

Report •

May 12, 2009 at 02:46:47
Seems to me the setting of vars and the sub are unnecessaary.

@echo off > newfile & setLocal EnableDelayedExpansion

for /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.


Report •

May 12, 2009 at 11:22:14
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?

Report •

May 12, 2009 at 15:20:48
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.


Report •

May 12, 2009 at 16:51:44
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,Office1,"99 Acme Road, Acme Town, Acmeville, AC9 9ME",1234,Office2,"10 Vale Road, Vale Town, Valeville, VA9 9LE",4321,Office3,"22 Some Road, Some City, SO9 9ME",1122,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.

Report •

May 12, 2009 at 19:25:05
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

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


echo ipadd=%ipadd%  location=%office%  device=%device%

Report •

May 13, 2009 at 15:19:10
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"99 Acme Road, Acme Town, Acmeville, AC9 9ME"#1234"10 Vale Road, Vale Town, Valeville, VA9 9LE"#4321"22 Some Road, Some City, SO9 9ME"#1122"77 That Close, That Town, That City, Thatsville, TH9 9AT"#2233
10.0...... and so on

and the batch would look like...

@echo off

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

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 :eof

Do you see any issues with that setup?

Thanks for all your help so far, couldn't have got this far without you!!

Report •

May 13, 2009 at 15:54:57
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.

ADDRESS="99 Acme Road, Acme Town, Acmeville, AC9 9ME"

ADDRESS="10 Vale Road, Vale Town, Valeville, VA9 9LE"

ADDRESS="22 Some Road, Some City, SO9 9ME"

ADDRESS="77 That Close, That Town, That City, Thatsville, TH9 9AT"

Whew! We must be close to putting this thread to bed.


Report •

May 14, 2009 at 00:38:04
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.


Report •

May 14, 2009 at 10:37:10
What is cURL?

If at first you don't succeed, you're about average.


Report •

May 14, 2009 at 11:39:11

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.

Report •

Ask Question