0

I need to find out if the given csv doesn't have any values other than header using batch script.

I'm trying with below line but my batch script is just flashing. Guide me to get the expected result.

(for /f "skip=1" %%a in ('type response.csv') do if "%%a"=="" (
set isDataPresent= No
echo %isDataPresent%
)
if %isDataPresent% EQU No goto noRecords
)
:noRecords
echo file is blank
[do something]  
User123
  • 1,498
  • 2
  • 12
  • 26
  • 1
    There is no chance for `"%%a"` to be equal to `""`, because `for` skips empty lines (if you have just the header, with `skip=1`, there is no data to be processed, so the `if "%%a"...` isn't even executed. But you can use this - see my answer. – Stephan Jun 15 '21 at 09:29
  • This is more compact though more complex way: `(for /F "usebackq skip=1" %%a in ("response.csv") do rem/) && (echo records found) || (echo file is blank)` – aschipfl Jun 15 '21 at 09:57

2 Answers2

1

You have a delayed expansion problem.
May I suggest another (simpler) approach?

for /f "skip=1" %%a in (response.csv) do goto :data
echo no data.
goto :eof
:data 
echo data available

If there is just one line (or no line at all), the do clause in for /f isn't even executed and the script continues with echo no data.. If there is a second line, the goto is executed (thus skipping the rest of the file)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Alternatively, you could use this approach:

@Set "}="&(Set /P "="&Set /P "}=")<"response.csv"&If Not Defined } Echo No Data

The idea is that the variable named } will be defined with the content of line 2 of response.csv. If that variable is not defined, it means that there was no content on line 2.

This is a little different to the answer already provided because if the second line was empty, and there was a line 3 with content, it would show as having no data. Therefore this example only reports no data on line 2, not no data beyond line 1.


If you want to report no data beyond line 1, here's another alternative:

findstr /N "^" "response.csv" 2>NUL|findstr /BL "2:">NUL||Echo No Data

Although I'd prefer to use full paths and names:

%SystemRoot%\System32\findstr.exe /N "^" "Response.csv" 2>NUL | %SystemRoot%\System32\findstr.exe /B /L "2:" 1>NUL || Echo No Data
Compo
  • 36,585
  • 5
  • 27
  • 39