Lico_w
asked on
Checking and Moving Folders Using VBScript
I am writing some VBScript to cycle through directories on a server and move any files or folders which have a specified piece of text on the end to a directory on the C:\
I have successfully created the script to cycle through and move the files but am struggling with the folders, my code is below:
'Create an output file to store original directory of file/folder
createFileName = "C:\Copied_Files_Dir\Recor d_Of_Moved _Files.txt "
set fileScrObj = CreateObject("Scripting.Fi leSystemOb ject")
set createFile = fileScrObj.CreateTextFile( createFile Name)
'Gather end part of name via an input box
DisplayText1 = "Enter either the extension type or the full file name you want to clear."
extType = InputBox(DisplayText1)
wscript.echo "You have chosen to move files ending in " & extType & ". A record of all moved files will be created here: " &
vbCr & vbCr & "C:\CopiedFilesRecord"
inputLength = len(extType)
destDir = "C:\Copied_Files_Dir"
CurrentDir = CreateObject("Scripting.Fi leSystemOb ject").Get AbsolutePa thName("." )
'wscript.echo CurrentDir
strFolder = "C:\Documents and Settings\T104ahe\Desktop\M enu\VBScri pts"
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objFolder = objFSO.GetFolder(strFolder )
'WScript.Echo extType
Set colFiles = objFolder.Files
For Each objFile In colFiles
If (Right (objFile.Path, inputLength) = extType) Then
createFile.WriteLine(objFi le.path)
objFile.Move destDir & "\"
End If
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If (Right (objFile.Path, inputLength) = extType) Then
createFile.WriteLine(objFi le.path)
objFile.Move destDir & "\"
End If
Next
ShowSubFolders(objSubFolde r)
Next
End Sub
I have successfully created the script to cycle through and move the files but am struggling with the folders, my code is below:
'Create an output file to store original directory of file/folder
createFileName = "C:\Copied_Files_Dir\Recor
set fileScrObj = CreateObject("Scripting.Fi
set createFile = fileScrObj.CreateTextFile(
'Gather end part of name via an input box
DisplayText1 = "Enter either the extension type or the full file name you want to clear."
extType = InputBox(DisplayText1)
wscript.echo "You have chosen to move files ending in " & extType & ". A record of all moved files will be created here: " &
vbCr & vbCr & "C:\CopiedFilesRecord"
inputLength = len(extType)
destDir = "C:\Copied_Files_Dir"
CurrentDir = CreateObject("Scripting.Fi
'wscript.echo CurrentDir
strFolder = "C:\Documents and Settings\T104ahe\Desktop\M
Set objFSO = CreateObject("Scripting.Fi
Set objFolder = objFSO.GetFolder(strFolder
'WScript.Echo extType
Set colFiles = objFolder.Files
For Each objFile In colFiles
If (Right (objFile.Path, inputLength) = extType) Then
createFile.WriteLine(objFi
objFile.Move destDir & "\"
End If
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If (Right (objFile.Path, inputLength) = extType) Then
createFile.WriteLine(objFi
objFile.Move destDir & "\"
End If
Next
ShowSubFolders(objSubFolde
Next
End Sub
ASKER
Hi Rob, I'm slightly confused.
Could I do the subfolders bit in a seperate script? If so how would I do that.
Could I do the subfolders bit in a seperate script? If so how would I do that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Rob, I had another look at this today and got it working, u da man!
No problem. Thanks for the grade.
Rob.
Rob.
Use a For loop for the files of objFolder, then use another For loop *after* that to iterate through the subfolder names, calling ShowSubFolders again.
In doing this, you don't the colFiles For loop that you have outside of the ShowSubFolders procedure in the main code.
Regards,
Rob.
Open in new window