How to check for empty directories and return a custom error

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jon80
Posts: 1
Joined: 06 Mar 2013 03:36

How to check for empty directories and return a custom error

#1 Post by jon80 » 06 Mar 2013 03:41

Is it possible to create some conditional e.g. ERRORLEVEL, which checks:
a) whether files exist, and if they do not do not run a command
b) alter the error message displayed so it is more user-friendly to the uninitiated i.e. those who are not familiar with DOS error messages

Taking backup of DMS Bugtrak files...
Backup finished
Moving temporary files
The filename, directory name, or volume label syntax is incorrect. //I want to change this message...
Press any key to continue . . .

@echo off
ECHO Taking backup of DMS Bugtrak files...
XCOPY "C:\Users\jcamilleri\Documents\Correspondence\DeMaSy Financials\Bugtrak" "Z:\backup\DMS_Arch" /Y /D /E > backup_bugtrak.log
echo Backup finished
ECHO Moving temporary files
MOVE /Y "C:\temp\*.*" Z:\backup\DMS_development_DBs\ > backup_localDBs.log
PAUSE

The problem is that sometimes C:\temp does not have any files, so there is nothing to backup, but the error message is not very user-friendly.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to check for empty directories and return a custom e

#2 Post by foxidrive » 06 Mar 2013 03:53

Try this:

Code: Select all

@echo off
ECHO Taking backup of DMS Bugtrak files...
XCOPY "C:\Users\jcamilleri\Documents\Correspondence\DeMaSy Financials\Bugtrak" "Z:\backup\DMS_Arch" /Y /D /E > backup_bugtrak.log
echo Backup finished
ECHO Moving temporary files if they exist
dir "c:\temp" /b /a-d >nul 2>&1 && (MOVE /Y "C:\temp\*.*" Z:\backup\DMS_development_DBs\ > backup_localDBs.log) || (echo no temp files in c:\temp >backup_localDBs.log)
PAUSE

Post Reply