Batch File System cannot find file after Loop

529 Views Asked by At

I am creating a batch file to open remote Computer Management console by taking User ID as input and computer name from 2nd column from file data.csv. it works fine on first attempt. When it goes back to :start label. and ask for other input. it gives error. System cannot find file ./data.csv My code is

 :start
    set /p Input="Enter User-ID:" 

    for /f "usebackq tokens=1-4 delims=," %%a in (".\data.csv") do (
       if %input% ==%%a ("cmd /c Start /B /wait compmgmt.msc –a /computer=%%b")

    )
    cls
    GOTO start
2

There are 2 best solutions below

3
On BEST ANSWER

Good practice to use %~dp0 for paths in batch files (instead of relative paths like .) that way if the current working folder changes the file will always be located.

So change to %~dp0data.csv

0
On
:start
set /p Input="Enter User-ID:" 
PUSHD    
for /f "usebackq tokens=1-4 delims=," %%a in (".\data.csv") do (
 if %input% ==%%a ("cmd /c Start /B /wait compmgmt.msc –a /computer=%%b")

)
POPD
cls
GOTO start

should restore sanity, pushing the directory then restoring it before the next cycle.