Suppress “File Not Found” in Batch File

2019-08-16 23:59发布

问题:

I was trying to run below bat file which gives me number of files present in a folder, i was getting the count as per the files but if there is no files i am getting "File Not Found"

Count is 0 and which is okay, but i want to suppress "File Not Found"

Script:

@echo off

setlocal enabledelayedexpansion

@echo off

set c2=0

echo Folder,NoOfFiles

for  /f "tokens=*" %%x in ('dir /s /a /b "D:\Flow\Flow1\Flow2\Flow3\NGR*.txt"') do set /a c2+=1

echo.NGR,!c2!

Result:

Folder,NoOfFiles

File Not Found

NGR,0

Press any key to continue . . .

I was trying to use 2>nul but that was also not working

Can someone please help me to get the solution, what can be done here?

回答1:

try with :

for  /f "tokens=*" %%x in ('dir /s /a /b "D:\Flow\Flow1\Flow2\Flow3\NGR*.txt" 2^>nul') do set /a c2+=1