- 1 year ago
'RD' command not returning '0' or '1' as errorlevel on success or failure
The RD
command is used to remove a directory and all its contents. The command returns an errorlevel to indicate whether the operation was successful or not. In general, RD
returns a 0
errorlevel on success and a 1
errorlevel on failure.
However, there are some cases where the RD
command may not return a 0
or 1
errorlevel. This can happen if there are files or subdirectories in the directory being removed, and the user does not have permission to delete them. In this case, the RD
command will display an error message, but it will not set the errorlevel.
To work around this issue, you can use the IF EXIST
command to check if the directory still exists after the RD
command has been executed. If the directory exists, it means that the RD
command was not successful, and you can set the errorlevel manually.
Here's an example:
bash@echo off
setlocal
rd /s /q C:\Temp\MyDirectory
if exist C:\Temp\MyDirectory (
echo Failed to delete directory.
exit /b 1
)
echo Directory deleted successfully.
exit /b 0
In this example, the RD
command is used to remove the C:\Temp\MyDirectory
directory and all its contents. The IF EXIST
command is then used to check if the directory still exists. If the directory exists, the script outputs an error message and sets the errorlevel to 1
. If the directory does not exist, the script outputs a success message and sets the errorlevel to 0
.