Reputation: 465
I've inherited a batch file, and in it, I've got the following command -
forfiles /s /c "cmd /c IF @ISDIR==TRUE rmdir @FILE /q" >> C:\Apps\%dt%\%dt%.log 2>&1
In the log, I get the following error:
ERROR: The system cannot find the file specified.
How can I put an else condition so that it outputs the name of the file not found, instead the Error Message?
Upvotes: 1
Views: 3273
Reputation: 34909
Ther error comes from forfiles
when it is executed in a directory with no content (no files and no directories). Remember that forfiles
searches the current working directory if no path is specified explicitly by the /P
switch, and the search mask defaults to *
if switch /M
is not given, hence matching all items. So the error you encounter can only occur with an empty working directory.
The error has got nothing to do with the if @isdir==TRUE
query.
If you want a specific message to appear in your log, you could check the ErrorLevel
state after forfiles
has been executed, which is 1
in case no items match the search criteria, and 0
if at least one item matches:
forfiles /S /C "cmd /C if @isdir==TRUE rmdir @file" || echo "%CD%" is empty. >> "C:\Apps\%dt%\%dt%.log" 2> nul
This writes the message "%CD%" is empty
into the log with %CD%
replaced by the current directory.
The 2> nul
suppresses the error message ERROR: The system cannot find the file specified.
.
The ||
operator is conditionally concatenates commands and lets the right one execute only if the left one has been completed successfully, meaning that ErrorLevel
is 0
.
Upvotes: 4