Hi Everyone,
I am trying to make up this script but am needing someones expertise. What I am trying to do is a recursive search of folder a, if any of the sub-folders within contain files with a particular extension, then move the sub-folder, all folders within and all the files to folder b maintaining the same folder structure. Continue this until all sub-folders, folders within and the specified extension files have been moved to folder b in the same structure as within folder a. I have seen on other websites that this is referred to as "nested folder" and is quite difficult. So I also seen that "robocopy" would achieve this, so I was thinking of using robocopy, but don't know how to do it. Here is the recursive script I began with:
OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder,strDestination,WSHshell
DIM objFSO
' ************************************************************
' Setup
' ************************************************************
' Folder to delete files from (files will also be deleted from subfolders)
strFolder = "D:\My Downloads\FireFox Downloads"
strDestination = "D:\My Downloads\Incomplete\"
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "txt"
' ************************************************************
set objFSO = createobject("Scripting.FileSystemObject")
RecursiveDeleteByExtension strFolder,strExtensionsToDelete
WScript.echo "Finished"
sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
DIM objFolder, objSubFolder, objFile
DIM strExt
set objFolder = objFSO.GetFolder(strDirectory)
WScript.Echo "strDirectory = " & strDirectory
WScript.Echo "objFolder = " & objFolder
for each objFile in objFolder.Files
WScript.Echo objFile
for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
WScript.echo "Deleting:" & objFile.Path
WScript.Echo "objFolder = " & objFolder
WScript.Echo "objFile.Path = " & objFile.Path
WScript.Echo "objFolder.Path = " & objFolder.Path
Set WSHShell = CreateObject("Wscript.Shell")
Call WshShell.Run("cmd robocopy" & objFolder & strDestination)
'objFSO.CreateFolder "D:\My Downloads\Incomplete\New Folder"
'objFolder.Move StrDestination
WScript.Echo "strDestination = " & strDestination
'WScript.Echo "objFolder = " & objFolder
exit for
end if
next
next
for each objSubFolder in objFolder.SubFolders
WScript.Echo "objSubFolder = " & objSubFolder
RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
next
end sub
I am trying to make up this script but am needing someones expertise. What I am trying to do is a recursive search of folder a, if any of the sub-folders within contain files with a particular extension, then move the sub-folder, all folders within and all the files to folder b maintaining the same folder structure. Continue this until all sub-folders, folders within and the specified extension files have been moved to folder b in the same structure as within folder a. I have seen on other websites that this is referred to as "nested folder" and is quite difficult. So I also seen that "robocopy" would achieve this, so I was thinking of using robocopy, but don't know how to do it. Here is the recursive script I began with:
OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder,strDestination,WSHshell
DIM objFSO
' ************************************************************
' Setup
' ************************************************************
' Folder to delete files from (files will also be deleted from subfolders)
strFolder = "D:\My Downloads\FireFox Downloads"
strDestination = "D:\My Downloads\Incomplete\"
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "txt"
' ************************************************************
set objFSO = createobject("Scripting.FileSystemObject")
RecursiveDeleteByExtension strFolder,strExtensionsToDelete
WScript.echo "Finished"
sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
DIM objFolder, objSubFolder, objFile
DIM strExt
set objFolder = objFSO.GetFolder(strDirectory)
WScript.Echo "strDirectory = " & strDirectory
WScript.Echo "objFolder = " & objFolder
for each objFile in objFolder.Files
WScript.Echo objFile
for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
WScript.echo "Deleting:" & objFile.Path
WScript.Echo "objFolder = " & objFolder
WScript.Echo "objFile.Path = " & objFile.Path
WScript.Echo "objFolder.Path = " & objFolder.Path
Set WSHShell = CreateObject("Wscript.Shell")
Call WshShell.Run("cmd robocopy" & objFolder & strDestination)
'objFSO.CreateFolder "D:\My Downloads\Incomplete\New Folder"
'objFolder.Move StrDestination
WScript.Echo "strDestination = " & strDestination
'WScript.Echo "objFolder = " & objFolder
exit for
end if
next
next
for each objSubFolder in objFolder.SubFolders
WScript.Echo "objSubFolder = " & objSubFolder
RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
next
end sub