Want to delete file not found

I have a script that searches for a file name that is in the format "abc2014.txt" in a specific folder. Then I count the number of such files that have the name * 2014. But if this file is not found, then on the command line it displays the output as "File not found."

My script:

@echo off
SetLocal enabledelayedexpansion
for /F "tokens=1" %%a IN ('Dir "C:\Users\BOX\*2014*"  /-C/S/A:-D') Do Set q=!n2! & Set n2=%%a
echo %q%

I do not want this output "File not found". How to suppress this output "File not found"? If there is no file there, I want to get blank .

How to achieve this?

+4
source share
2 answers

Just redirect the standard error (2) of the 'dir' command to nul.

@echo off 
SetLocal enabledelayedexpansion 
for /F "tokens=1" %%a IN ('Dir "C:\Users\BOX\*2014*" /-C/S/A:-D 2^>nul') Do Set q=!n2! & Set n2=%%a echo %q%

:

@echo off 
echo Files number:
Dir "C:\Users\BOX\*2014*" /-C/S/A:-D 2^>nul | find /C /V ""
+7
setlocal enableDelayedExpansion
for /F "tokens=1" %%a IN ('Dir "C:\Users\BOX\*2014*" /-C/S/A:-D  2^>nul') Do (
 Set q=!n2! 
 Set n2=%%a echo %q%
)
endlocal
+2

All Articles