Solved

Checking and Moving Folders Using VBScript

Posted on 2010-09-15
5
348 Views
Last Modified: 2012-05-10
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\Record_Of_Moved_Files.txt"
set fileScrObj = CreateObject("Scripting.FileSystemObject")
set createFile = fileScrObj.CreateTextFile(createFileName)

'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.FileSystemObject").GetAbsolutePathName(".")
'wscript.echo CurrentDir
strFolder = "C:\Documents and Settings\T104ahe\Desktop\Menu\VBScripts"
Set objFSO = CreateObject("Scripting.FileSystemObject")
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(objFile.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(objFile.path)
            objFile.Move destDir & "\"
         End If
    Next
    ShowSubFolders(objSubFolder)
  Next
End Sub
0
Comment
Question by:Lico_w
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 33687199
Hi, what you need to is treat files and folders separately in the ShowSubFolders procedure, instead of looping the files inside the looping of the subfolders.

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.
'Create an output file to store original directory of file/folder

createFileName = "C:\Copied_Files_Dir\Record_Of_Moved_Files.txt"

set fileScrObj = CreateObject("Scripting.FileSystemObject")

set createFile = fileScrObj.CreateTextFile(createFileName)



'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.FileSystemObject").GetAbsolutePathName(".")

'wscript.echo CurrentDir

strFolder = "C:\Documents and Settings\T104ahe\Desktop\Menu\VBScripts"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolder)

'WScript.Echo extType



ShowSubFolders(objFolder)



Sub ShowSubFolders(objFolder)

	For Each objFile In objFolder.Files

		If (Right (objFile.Path, inputLength) = extType) Then

			createFile.WriteLine(objFile.path)

			objFile.Move destDir & "\"

		End If

	Next

	For Each objSubFolder In objFolder.SubFolders

		'WScript.Echo objSubFolder.Path

		ShowSubFolders(objSubFolder)

	Next

End Sub

Open in new window

0
 

Author Comment

by:Lico_w
ID: 33689904
Hi Rob, I'm slightly confused.

Could I do the subfolders bit in a seperate script? If so how would I do that.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 33690411
No sorry, I don't mean a separate script, just separate to the iteration of the files.
You see, in your procedure you had:
  For Each objSubFolder In colFolders
    .....
    For Each objFile In colFiles
        ....
    Next
    .....
  Next

Whereas you should have (as I have shown in a slightly more compact way)
  For Each objFile In colFiles
     .....
  Next
  For Each objSubFolder In colFolders
     ....
  Next

If you study my version of the script, you will see that it does that, and it should work.

Regards,

Rob.
0
 

Author Closing Comment

by:Lico_w
ID: 33725266
Thanks Rob, I had another look at this today and got it working, u da man!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33730090
No problem. Thanks for the grade.

Rob.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now