0

Having an issue with if-else statements/error handling. Been trying to get this to work, cannot seem to do it. What I'm looking to do is have user input client id, then delete the directory associated with said client if it exists.

Code:

@echo off
set /p id=Enter ID:
rd /s /q "\\Images\Public on Images\DMS Scanned Clients\%id%"
echo \\Images\Public on Images\DMS Scanned Clients\%id%
pause

So user would input 956862 for example. Then if directory at \Images\Public on Images\DMS Scanned Clients\956862 exists, it would be removed, otherwise, message would display stating it does not exist.

Thanks

Zach
  • 1

1 Answers1

0
@Echo off
pushd %1 >nul 2>&1  
If errorlevel 0 if not errorlevel 1 Echo %~nx1 is a folder
If errorlevel 1 Echo %~nx1 is not a folder
Popd

If /i "%cmdcmdline:~0,6%"=="cmd /c" pause

However in programming we do then test, not test then do. Testing requires a lot of system resources. Therefore to test then do requires two expensive disk accesses. Doing and testing the result (ie is the returned number 0) is only one disk access and a quick test of a number.

rd /s c:\somefolder && Echo Folder deleted || Echo Folder didn't exist

&& and || mean do if errorlevel is zero or not zero. It's easier than If errorlevel 0 if not errorlevel 1 echo errorlevel is 0 on multiple lines. See my post here Is typing %^ into cmd.exe a Windows easter egg?

bill
  • 31