Blog

Find a CSV row by ID in a Windows batch file

Match an ID in the first column of a CSV file and retrieve the remaining values. Use FOR /F for simple data and Import-Csv for quoted fields or blanks.

A diagram highlights a matching row and moves its two values out of the table.

If a batch file needs different values for different IDs, keep those associations in a CSV file. Supporting another ID then takes only one extra row. For a simple three-column lookup table, use FOR /F to match the first column against the entered ID and put the two remaining values in variables.

Find a matching row in a fixed three-column file

Try this in Windows 11 Command Prompt. Save data.csv without a header and lookup.bat in the same folder. The example assumes unique IDs and alphanumeric fields. All characters in the sample are ASCII, so save the files as text without a BOM.

a001,pen,stock
a002,tea,office
a003,tape,stock
@echo off
setlocal EnableDelayedExpansion
set "targetId="
set /p "targetId=Enter ID: "
if not defined targetId exit /b 1
set "Data_01="
set "Data_02="
for /f "usebackq tokens=1-3 delims=," %%A in ("%~dp0data.csv") do (
    if "%%A"=="!targetId!" (
        set "Data_01=%%B"
        set "Data_02=%%C"
    )
)
if not defined Data_01 (
    echo ID not found.
    exit /b 1
)
echo Data_01=!Data_01!
echo Data_02=!Data_02!

Run lookup.bat and enter a002. It displays Data_01=tea and Data_02=office. Later commands in the same batch file can use both variables. If the script cannot find the ID, it exits with code 1.

With tokens=1-3, the first three fields go into %%A, %%B, and %%C. The comparison uses %%A and the entered !targetId!. Because %~dp0 resolves to the batch file’s drive and folder, the same CSV opens regardless of the current folder. The usebackq option lets you enclose a path with spaces in double quotes. Microsoft’s for command reference

Read CSV with blank or quoted fields using Import-Csv

With a002,,office as input, FOR /F groups consecutive delimiters and treats office as the second field. It also splits at the comma inside quotes in a002,"tea, large",office. To handle these cases in Windows PowerShell 5.1 or PowerShell 7, run the following from the folder containing the file.

Import-Csv -LiteralPath .\data.csv -Encoding UTF8 -Header Id, Name, Location |
    Where-Object { $_.Id -ceq 'a002' } |
    Select-Object Name, Location

Use -Header to name the columns in the headerless example. If your file already has headers, omit it and filter using the actual names. Blank values stay blank, and "tea, large" is read as a single value. For files that use an encoding other than UTF-8, identify the encoding first and adjust -Encoding. Microsoft’s Import-Csv reference

When the ID matches but the product name is garbled

Try reading the following line, saved as Shift_JIS, with the command above. The alphanumeric ID still matches, but the Japanese characters in the product name are read incorrectly.

a002,"紅茶, 茶葉",office

Keep the original CSV and open the file in Character Encoding Converter. Choose UTF-8 as the target encoding and click “Convert & download ZIP”. Extract the ZIP into a separate folder and run the same Import-Csv example there. If Name contains 紅茶, 茶葉 with the comma intact and Location contains office, the text and field boundaries were read correctly.