How to read values in a particular column of a CSV file in Batch?
38,678
Solution 1
Suppose you have a space-delimited file named yourfile.csv, and you want to read the fourth (D1) column, you should execute this:
for /F "tokens=4 delims= " %i in (yourfile.csv) do @echo %i
Solution 2
On windows 7 with powershell you can easly parse the csv with Import-Csv EX:
Import-Csv -Delimiter " " -Header a,b,c,d,e c:\the.csv | foreach{ Write-Host $_.d }
Related videos on Youtube
Author by
Mandar Shinde
Updated on September 18, 2022Comments
-
Mandar Shinde almost 2 years
I am planning to write a batch script wherein I need to scan the values from a particular column of a CSV file one by one and store them in a variable for further processing.
Say, following is the CSV file:
A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A3 B3 C3 D3 E3 .. .. .. .. ..
I have to read D1, execute a command using it's value, read D2, execute a command, and so on.
How can this be achieved?
-
DanteTheEgregore over 10 yearsWelcome to Super User! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
-
and31415 over 10 yearsFYI, the command won't work properly if the file name contains spaces. You need to put straight quotes around the file name and enable the
usebackq
option, like this:for /f "usebackq tokens=4 delims= " %i in ("your file.csv") do @echo %i
Also, the delimiter already incluses spaces and tabs by default, so it might be omitted in this case.