VBScript For Loop folder variable not working, Trying to move, rename, and delete based on file/folder names

NateAtWork

New Member
Joined
Nov 11, 2014
Messages
6
Hi all,

I am new to vbscript, but I have some experience with vba. I am trying to move, rename, and delete files based on their names and where they are stored in the folder structure. But when I run the script I get an error "Invalid 'for' loop control variable" that takes place at the start of the second for loop. I don't see the difference between the way the variables are set up for the first and second for loop so this doesn't make sense to me. If anyone could take a look and figure out what I am doing wrong I would appreciate it:
Rich (BB code):
'Should fix folder structure for Dim fso, Folder, file, MasterFolder, FolderInFolder, Temp
Dim MasterFolderName, SubFolderName, searchFileName, renameFileTo, _
    SubmittalNumber, Submittals_wo_Approved_Folder, _
    Submittals_wo_Approved_Folder_prev


' Parameters
MasterFolderName     = """C:\desktop\Submittals\"""
SubFolderName = """\Approved\"""
searchFileName = """Procore Print"""
' Create filesystem object and the folder object
' how the FSO works: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Set fso = CreateObject("Scripting.FileSystemObject")  
Set MasterFolder = fso.GetFolder(MasterFolderName)
Set Temp = fso.CreateFolder("C:\desktop\Submittals\temp")


' Loop over all files in the folder until the searchFileName is found
For each Folder In MasterFolder.SubFolders
    Set Folder = fso.GetFolder(Folder.name)
    ' See if the file starts with the name we search
    ' how instr works: http://www.w3schools.com/vbscript/func_instr.asp
    renameFileTo = left(Folder.Name,instr(file.name, "-") - 2) _
                            & " " & searchFileName
    For each FolderInFolder in folder.SubFolders
        Submittals_wo_Approved_Folder_prev = _
                            Submittals_wo_Approved_Folder
        Submittals_wo_Approved_Folder = _
                            Submittals_wo_Approved_Folder _
                            & ", " & folder.name
        if FolderInFolder.Name = SubFolderName then
            Submittals_wo_Approved_Folder = _
                            Submittals_wo_Approved_Folder_prev
            exit for
              For each FolderInFolder in folder.SubFolders '<----ERROR OCCURS AT RED CHARACTOR
                if FolderInFolder.Name = SubFolderName then
                    For Each file in FolderInFolder
                        If instr(file.name, searchFileName) = 1 Then
                            file.name = renameFileTo
                            REM REM ' Exit the loop, we only want to rename one file
                            REM REM Exit For
                        End If
                        fso.copyFile file.path, temp
                    Next File
                    FolderInFolder.DeleteFolder FolderInFolder.Path, True
                else 
                    FolderInFolder.DeleteFolder FolderInFolder.Path, True
            Next FolderInFolder
            For each file in Folder.subfolders
                file.DeleteFile File.Path, True
            Next file
        end if
    Next FolderInFolder
    For each file in temp.files
        fso.copyFile file.path, folder.path
    next file
Next
writeline.print Submittals_wo_Approved_Folder
fso.deleteFolder temp.path, True
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

You have a loop using FolderInFolder inside another one using the same variable. You need to rename one of them.
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,811
Members
449,262
Latest member
hideto94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top