Solved

Checking and Moving Folders Using VBScript

Posted on 2010-09-15
5
347 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

15 Experts available now in Live!

Get 1:1 Help Now