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?
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 ""
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With