How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Steve Your Own Question
Steve, Consultant
Category: Programming
Satisfied Customers: 266
Experience:  Steve is a consultant in the areas of computer software and programming, information management and networking.
Type Your Programming Question Here...
Steve is online now
A new question is answered every 9 seconds

I want to use MS-DOS to read files with a single line like

Customer Question

Hello, I want to use MS-DOS to read files with a single line like the following:
"A", "B" , "C,D " , "" , "", "", "", "E", "F", "", "", "G" , "H". I wanted to assign variable values using data from each field within quotation marks. Normally, this is easily done using a command like for /f "tokens=... delims=," %%a in (%filename%) do set Field1=%%a & set Field2=%%b...etc
But, when commas are part of the data, as in the example above, this throws everything off. I have tried using " as a delimiter and a command like:
FOR /F tokens^=2^-26^ delims^=^" %%a in (%tempfile%) do set...
But, then this doesn't seem to get it right for a file without commas in the data fields for some reason. I would like help on ideas on how to parse lines like the example above.
Submitted: 11 months ago.
Category: Programming
Expert:  Steve replied 11 months ago.

Hi, my name is***** you able to escape the commas in the string using the ^ character?

Expert:  Steve replied 11 months ago.

"A","B" , "C^,D",...

Customer: replied 11 months ago.
The file contains a line of variable data with a fixed number of variable fields. The values are all surrounded by ". Additionally, they are separated by commas. But, sometimes, the data within the quotes will contain a comma. This will then put the read command out of synch with the order of the file data. Too sleepy to keep writing now. Let me know if you still have questions.
Expert:  Steve replied 11 months ago.

Sorry, I understand that. I was asking if you had any control over the file itself. You can mitigate the problem by escaping the comma that's inside the quotes in the file with the ^ symbol, for example, "C^,D"

Expert:  Steve replied 11 months ago.

The other alternative is to use Windows PowerShell to parse the file and create the output. The following regular expression will successfully split and parse what you pasted above:


In Windows Powershell, you can something similar to this:

$input_path = 'file.txt'

$regex = '\"(\w?\,?\w?\s?)\"'

select-string -Path $input_path -Pattern $regex -AllMatches | % {$_.Matches}|%{$_.Value}

Please let me know if you want to discuss this further. If this answers your question, please accept the answer and rate me accordingly. I don't get paid anything for helping you unless you accept the answer and rate me at least 3 stars. If it doesn't answer your question, please chat me back and I'll find the answer for you. Thanks!

Customer: replied 11 months ago.
I would really like to keep things as bare-bones as possible and use a programming language that is pretty ubiquitous like dos. I do not know windows powershell, but I did recently learn about it. Having said that, I think I do have windows powershell. I started it the other day to verify that I have it. But, as I said, I have never used it. I would also need help launching it from dos. I have a bunch of other code written in dos, so I would need to either re-write all of my code in powershell or launch powershell temporarily in my dos program just for this line of code and then close it again. I did in the meantime find an alternative solution on my own. Since the pattern "," essentially marks the border between fields in a line of data such as "A","","B","","","C", I thought about just reading in the line as a single string and replacing all instances of "," with a new delimiter like ; i.e., a simicolon. Then, I could strip the stray " marks and have a clean line like A;;B;;;C. However, when I tried that, my results were unexpected. I think the command: for /f "tokens=... delims=;" %%a in (file) might ignore the fields that contain no data (???).
Customer: replied 11 months ago.
fyi, I am doing this for a project at work in a large network setting and may not have much flexibility in using different coding languages.
Expert:  Steve replied 11 months ago.

I don't know that you're going to be able to do what you want to do with DOS commands. Windows Powershell essentially replaced the DOS batch commands for Windows. It comes installed with Windows 7 and all Windows Server installations. While its target audience is typically a more advanced "command-line" user, it's a very accepted system-administrator type of language used to do the kinds of things you're trying to do.

If you click Start and search for Powershell, you'll see Windows Powershell ISE, and then 32-bit and 64-bit versions of Powershell. The ISE is an interactive Powershell environment that makes it easier to build and troubleshoot these kinds of batch files.

You can copy and paste the commands I posted (assuming your filename is file.txt) and see the output you get from running them. The real power of Powershell, in your instance, is the ability to use regular expressions to parse the data. If you're not familiar with regular expressions, they are a programmer's Swiss Army Knife for parsing data.

Let me know how I can help you further.

Related Programming Questions