troubleshooting Question

Robocopy - copying empty folders even when it is told not to

Avatar of Darrell Porter
Darrell PorterFlag for United States of America asked on
VB ScriptWindows Server 2008Windows Batch
17 Comments3 Solutions2202 ViewsLast Modified:
Summary of question:
Is Robocopy working as expected, or should I be using different command line parameters to prevent empty folder creation?

Detail Issue:
Below is the RoboCopy Job File, which is saved using the /SAVE parameter.

Note the /S parameter is used and not the /E parameter.

When the command is run, via a VBScript which assembles the command line, it works without issue EXCEPT it is creating empty folders.  From what I can tell from the documentation and every site I have looked at on the Internet for this issue, RoboCopy should NOT be creating these empty folders..

From what I understand, if a folder does not contain one of the desired file types, it should be considered empty.
If I have 1,200 folders nested 30 levels deep and those folders contain files of type .FUBAR, it should NOT create empty destination folders of the same name, yet it is.


:: Source Directory :
	/SD:\\ComputerName\c$\	:: Source Directory.

:: Destination Directory :
	/DD:E:\Migrated Data\ccnllp\Username\ComputerName\Root\ByExtension\	:: Destination Directory.

:: Include These Files :
	/IF		:: Include Files matching these names
	/XJD		:: eXclude Junction points for Directories.

:: Exclude These Directories :
	/XD		:: eXclude Directories matching these names

:: Exclude These Files :
	/XF		:: eXclude Files matching these names
:: Copy options :
	/S		:: copy Subdirectories, but not empty ones.
	/PF		:: check run hours on a Per File (not per pass) basis.
	/COPY:DAT	:: what to COPY for files (default is /COPY:DAT).
	/ZB		:: use restartable mode; if access denied use Backup mode.
	/EFSRAW		:: copy all encrypted files in EFS RAW mode.
:: Retry Options :
	/R:1		:: number of Retries on failed copies: default 1 million.
	/W:10		:: Wait time between retries: default is 30 seconds.
:: Logging Options :
	/V		:: produce Verbose output, showing skipped files.
	/X		:: report all eXtra files, not just those selected.
	/TS		:: include source file Time Stamps in the output.
	/FP		:: include Full Pathname of files in the output.
	/NP		:: No Progress - don't display percentage copied.
	/LOG+:C:\Robocopy\Username_ComputerName_YYYYMMDD_HHMMSS.log	:: output status to LOG file (append to existing log).

If Robocopy is, in fact, performing as expected, I presume I am going to need to run something like the following to recursively remove empty folders.  This is a step I would rather not take, though would write as a secondary housekeeping script due to time constraints on the primary script.

'Sub to recursively delete all empty folders. 
Sub RecursiveDeleteEmptyFolders(ByVal strDirectory)
	On Error Resume Next
	Dim objFolder, objSubFolder
	Set objFolder = objFSO.GetFolder(strDirectory)
	WScript.Echo "Checking Folder: " & strDirectory & ". Contains " & objFolder.Files.Count & " files and " & objFolder.SubFolders.Count & " folders."

	'If the RemoveDesktopIni or RemoveThumbsDB Flag is set to True then remove any files called Desktop.ini or thumbs.db
	If objFSO.FileExists(strDirectory & "\desktop.ini") Then 
		objFSO.DeleteFile(strDirectory & "\desktop.ini")
		If Err Then
			WScript.Echo "Error deleting:" & strDirectory & "\desktop.ini" & " - " & Err.Description
			intErrorCount = intErrorCount + 1
		End If
   	End If
	If objFSO.FileExists(strDirectory & "\thumbs.db") Then 
		objFSO.DeleteFile(strDirectory & "\thumbs.db")
			If Err Then 
				WScript.Echo "Error deleting:" & strDirectory & "\thumbs.db" & " - " & Err.Description
				intErrorCount = intErrorCount + 1
		End If
   	End If
	'Check if there are any subfolders, and if so go through each of those recursively deleting them
    If objFolder.SubFolders.Count > 0 Then
    	For Each objSubFolder in objFolder.SubFolders
			RecursiveDeleteEmptyFolders objSubFolder.Path
    End If

    'Now check if the folder contains any files.
    If objFolder.Files.Count = 0 And objFolder.SubFolders.Count = 0 Then
		'Check that the folder name does not begin with a tildeand if it does not then delete it
		If Left(objFolder.Name,1) <> "~" Then
			WScript.Echo "Deleting: " & objFolder.Path
			If Err Then
				WScript.Echo "Error deleting:" & objFolder.Name & " – " & Err.Description
				intErrorCount = intErrorCount + 1
			End If
		End If
    End If 
End Sub
Darrell Porter
Enterprise Business Process Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros