I was able to fix this issue in the end thank you everyone for your help though. I fixed it by changing the format to .txt of the accesselevations.csv file.
Find a string in csv and only work on that line
-
01-09-2022 - |
Domanda
I have a batch file that needs to look through a csv and find one line based on a unique string, that is a variable mixture of letters and numbers, that should be input by the user (most likely scanned by a barcode).
The current csv set up is as per below:
School,computerid,Model,Serial,User,Access
These are headings and aren't the actual data. The variable that will be searched for is the WorkstationID
. Any suggestion is much appreciated.
My entire code is below:
@echo off
setlocal enabledelayedexpansion
Echo What is the students computer name?
set /p computerid=
findstr /i "%computerid%" "AccessElevations.csv" > %temp%\computer.txt
pause
FOR /F "tokens=1-6 delims=," %%a in (%temp%\computer.txt) DO (
set school=%%a
set Model=%%c
set Serial=%%d
set user=%%e
set access=%%f
Thanks,
Toby
Soluzione 3
Altri suggerimenti
If this deosn't help then please explain what problem you are having and what the error message is.
@echo off
setlocal enabledelayedexpansion
Echo What is the students computer name?
set /p computerid=
findstr /i "%computerid%" "AccessElevations.csv" > "%temp%\computer.txt"
pause
FOR /F "usebackq tokens=1-6 delims=," %%a in ("%temp%\computer.txt") DO (
set school=%%a
set Model=%%c
set Serial=%%d
set user=%%e
set access=%%f
)
EDITED - To adapt to comments
@echo off
setlocal enableextensions disabledelayedexpansion
set "inputFile=AccessElevations.csv"
set "computerID="
set /p "computerID=What is the students computer name? "
if not "%computerID%"=="" for /f "tokens=1-6 delims=," %%a in (
'findstr.exe /i /r /c:"^[^,]*,%computerID%," "%inputFile%"'
) do (
set "school=%%a"
set "model=%%c"
set "serial=%%d"
set "user=%%e"
set "access=%%f"
)
echo school : %school%
echo model : %model%
echo serial : %serial%
echo user : %user%
echo access : %access%
rem clean
endlocal
If a computerID is provided, findstr
is used to find this computerID in the second field (skip the line until the first comma, then the computerID, then a comma) of the indicated file. If a line is found, for
command will split it using a comma as a delimiter.