Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to suppress File not found output

I have a script which search for file name which is in the format "abc2014.txt" in a particular folder. I am then counting the number of such files which have *2014 in their name. But if that file is not found then on command prompt it gives output as "File not found".

My script is:

@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 don't want this "File not found" output. How do I suppress this "File not found" output? If there is no file there, then I want blank output.

How to achieve this?

like image 357
user3164140 Avatar asked Jan 17 '14 14:01

user3164140


2 Answers

Just redirect standard error (2) of '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%

But if you want to count files with certain characters in the name:

@echo off 
echo Files number:
Dir "C:\Users\BOX\*2014*" /-C/S/A:-D 2^>nul | find /C /V ""
like image 61
mihai_mandis Avatar answered Nov 11 '22 12:11

mihai_mandis


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
like image 20
npocmaka Avatar answered Nov 11 '22 10:11

npocmaka