SlothLovesChunk
SlothLovesChunk

Reputation: 75

If output contains "File Not Found", then log statement A, otherwise statement B (batch file)

I have a batch file that is checking the parition to see if there is a hiberation file. If the directory listing finishes and the file is not found, it will output: File Not Found, but for some reason that text is not going into my log as I wish. If there is way to have that go to my log, let me know.

Otherwise, I am looking to have some code that will basically say, if the output contains "Files Not Found" then log the text "File Not Found" in my output log. If it does, it automatically logs the info I need, so nothing needed.

set output=mylog.txt

echo.=================================>> "%output%"
echo.Performing Hibernation file Check...
echo.Hibernation File Check: >> "%output%"
C:
cd /
dir /a /s hiberfil.sys >> "%output%"
echo.
echo.
echo.=================================>> "%output%"

Upvotes: 0

Views: 143

Answers (2)

Tom McAnnally
Tom McAnnally

Reputation: 133

Change your dir command in the batch file to the following to also output stderr to the same file you are outputting stdout to.

dir /a /s hiberfil.sys >> "%output%" 2>>&1

Upvotes: 1

Stephan
Stephan

Reputation: 56180

to exactly answer the question in your title then log statement A, otherwise statement B

(dir /a /s hiberfil.sys >nul 2>&1 && echo yes || echo no) >>"%Output%"

>nul 2>& Redirects all output (StdOut and StdErr) to NUL:

&& serves as "if previous anser was successfull then..."

|| serves as "if previous anser was not successfull then..."

But as your batchfile is built,

dir /a /s /b hiberfil.sys >> "%output%" 2>&1

would be the best answer. It gives only one line of feedback - either "File not found" or the filename including the complete path. (except it finds several files that match - then one line per file)

Upvotes: 2

Related Questions