'<% 
Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Some handy global variables
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim TabStop
Dim NewLine
Dim counter
dim FolderPath
dim day 

Dim FSO


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Folder: SyncRootFolder\_sync_Cache_\FOLDER1
' This moves SyncRootFolder\_sync_Cache_\FOLDER1\01-11-2014-23_History\Folder1Contents to:
' SyncRootFolder\_sync_Cache_\FOLDER1\Folder1Contents
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function MoveFolder(Folder)
    Dim SubFolders
    Dim SubFiles
	Dim SubFolder
	Dim SubFile

    Dim firstSubFolder

    'set firstSubFolder = Null
    
	Set SubFolders = Folder.SubFolders ' the folder with the name: day, such as '05-08-2014'

    If SubFolders.Count = 1 Then
		For Each subFolder In SubFolders
            set firstSubFolder = subFolder  ' like: SyncRootFolder\_sync_Cache_\FOLDER1\01-11-2014-23_History
		Next
    else
        Exit function
	End If
    

    set SubFolders = firstSubFolder.SubFolders
    set SubFiles = firstSubFolder.Files

	If SubFolders.Count <> 0 Then
		For Each SubFolder In SubFolders
            FSO.MoveFolder SubFolder.path, Folder.path & "\" & SubFolder.name
		Next
	End If

	If SubFiles.Count <> 0 Then
		For Each SubFile In SubFiles
            FSO.MoveFile SubFile.path, Folder.path & "\" & SubFile.name
		Next
	End If

    FSO.DeleteFolder(firstSubFolder)

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Recursively traverse subfolders, Folder is like: SyncRootFolder\_sync_Cache_
' which contains: SyncRootFolder\_sync_Cache_\FOLDER1\01-11-2014-23_History\Folder1Contents
'
' Usually, the SyncRootFolder contains: SyncRootFolder\FOLDER1\Folder1Contents
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RecurseFolder(Folder)
	Dim SubFolders
	Dim SubFolder
    dim Files
	Set SubFolders = Folder.SubFolders  ' Contains Folder1, Folder2 etc. in the SyncRootFolder\_sync_Cache_ folder.
	
	If SubFolders.Count <> 0 Then
		For Each SubFolder In SubFolders
            MoveFolder(subFolder) ' subFolder is: SyncRootFolder\_sync_Cache_\FOLDER1 
		Next
	End If

'	wscript.echo S
	Set Files = Folder.Files
	Set SubFolders = Folder.SubFolders
	If Files.Count=0 and subFolders.count=0 Then
		Folder.delete
	end if
	RecurseFolder = ""

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Used to Start recursively traverse all folders and files in the folder.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function StartRecurse(FSO)
	Dim TestFolder
	If Not FSO.FolderExists(FolderPath) Then 
		Wscript.echo "path not exist."
		Exit Function
	end if

	Set TestFolder = FSO.GetFolder(FolderPath & "\_sync_Cache_")
	StartRecurse = RecurseFolder(TestFolder) 
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The main routine, input foldername and the Day.
' We want to move folders/files like: SyncRootFolder\_sync_Cache_\FOLDER1\01-11-2014-23_History\filesFolders 
' to:       SyncRootFolder\_sync_Cache_\FOLDER1\filesFolders

' or to:    SyncRootFolder\FOLDER1\filesFolders
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Main
	Dim oArgs
	set oArgs =wscript.arguments
	If oArgs.Count<2 Then
		wscript.echo "Usage: BatchMoveFolders.vbs FolderPath DAY   (where FolderPath is the SyncFolderRoot, DAY is like: 01-11-2014-23_History "
		wscript.quit(1)
	End If

	FolderPath = oArgs.item(0)
	day = oArgs.item(1)
	
	wscript.echo "Starting traversing " & oArgs.item(0) & " " & oArgs.item(1) & " ..."

	' Set up global data.
	TabStop = Chr(9)
	NewLine = Chr(10)
	counter = 0
	Set FSO = CreateObject("Scripting.FileSystemObject")

	wscript.echo  StartRecurse(FSO) & NewLine & NewLine
End Sub

call main
'%>