[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checking and Moving Folders Using VBScript

Posted on 2010-09-15
5
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

649 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