VBScript to loop through folder containing zipped files and unzip files to a different folder

StackOverflow https://stackoverflow.com/questions/22134334

  •  19-10-2022
  •  | 
  •  

Question

I am a noob with an S-curl and flowing cape. I have a friend who downloads zipped font files to her desktop. She then opens the files up, copies the fonts to the C:\fonts folder and installs the fonts. The script is intended to be used on XP. I mentioned that I would help her automate the process. So my methodology was to make a script that will scan her desktop for the zip files she downloads, move those files off of her desktop to a folder I call ZipFonts which will be located in her My Documents folder. When ZipFonts is created, two folders, ZipStaging and CompletedZips will be created inside of it. The plan is to move the zipped files to the ZipFonts folder. I then want to loop through ZipFonts and empty the contents of the zipped files into the ZipStaging folder and then the zipped files will be moved into the CompletedZips folder. I have been doing this script piece by piece to make sure it works successfully. I have successfully been able to create the folders and move the zipped files from the desktop into the ZipFonts folder. When I run the loop to open the zipped files, I get the error "Line 44 / Char 4 / Object Required: 'objShellApp.Namespace(...)' / Code: 800A01A8"

I'm sure I spelled everything correctly and I use Notepad++ to make sure everything is defined. I put my Set object references inside and outside of the loop to see if that made a difference but I get the same error. What am I missing? [Code is Below]

    1 Option Explicit
    2
    3 Public objFSO, objShell
    4 Public UserPoint, UserDesktop, UserDocs, ZFFolder, ZSFolder, CZFolder
    5 Public SourceFolder, file
    6 Public zFolder
    7
    8
    9 Set objFSO = CreateObject("Scripting.FileSystemObject")
    10 Set objShell = CreateObject("Wscript.Shell")
    11
    12 UserPoint = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
    13 UserDesktop = (UserPoint & "\Desktop")
    14 UserDocs = (UserPoint & "\My Documents")15 ZFFolder = (UserDocs & "\ZipFonts\")
    16 ZSFolder = (ZFFolder & "ZipStaging\")
    17 CZFolder = (ZFFolder & "CompletedZips\")
    18
    19 Sub MoveFromDesktop 'Move folders from Desktop To ZipFonts folder
    20    Set SourceFolder = objFSO.GetFolder(UserDesktop)
    21    FOR EACH file In SourceFolder.Files 'Loop through the user's Desktop folder         for files ending with .zip
    22       If Right(LCase(file.Name),4) = ".zip" Then
    23       'move the file into the %USERPROFILE%\My Documents\ZipFonts folder
    24       objFSO.CopyFile file.Path, ZFFolder, TRUE
    25          objFSO.DeleteFile file.Path
    26          '**figure out how to report that each file is being moved or display some sort of progress bar
    27          '**figure out how to report that file relocation has been completed for a few seconds
    28       End If
    29    NEXT
    30    Set SourceFolder = NOTHING
    31    Set SourceFolder = objFSO.GetFolder(ZFFolder)
    32    If SourceFolder.Files.Count = 0 Then   
    33       MsgBox("There are no compressed files (files ending with extension .zip) on your desktop.")
    34       MsgBox("Please click OK to end.")
    35       WScript.Quit
    36    End If
    37 End Sub
    38
    39 Sub Extract(file, folder) 'Using Extract for UnZipFiles
    40    Dim objShellApp, objSource, zFile, i : i = 1
    41   'sa = objShellApp||filesInZip = objSource||zfile = zFile||fso is already declared and defined as objFSO in PUBLIC
    42   
    43    Set objShellApp = CreateObject("Shell.Application")
    44    Set objSource = objShellApp.NameSpace(folder&file).Items
    45    FOR EACH zFile in objSource 'Eliminating file checking since this is done in my MoveFromDesktop Sub
    46      objShellApp.NameSpace(folder).Copyhere(zFile), &H100
    47      i = i + 1
    48      If i = 99 THEN
    49     Call zCleanup(file, i)
    50         i = 1
    51      End If
    52   NEXT
    53   If i > 1 THEN
    54      Call zCleanup(file, i)
    55   End If
    56   objFSO.DeleteFile(folder&file)
    57   'objTarget.CopyHere objSource, intOptions
    58   'intOptions = 256
    59   
    60   Set objSource = Nothing
    61 End Sub
    62
    63 Sub zCleanUp(file, count)
    64   Dim i, text   'fso is already declared and defined as objFSO in PUBLIC
    65   
    66   For i = 1 TO count
    67      If objFSO.FolderExists(objFSO.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file) = TRUE THEN
    68       text = fso.DeleteFolder(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file, True)
    69      Else
    70        Exit For
    71      End If
    72   Next
    73 End Sub
    74
    75 If objFSO.FolderExists(ZFFolder)= FALSE Then
    76   Set SourceFolder = objFSO.CreateFolder(ZFFolder)'Create folder %USERPROFILE%\My Documents\ZipFonts
    77   Set SourceFolder = Nothing
    78   Set SourceFolder = objFSO.CreateFolder(ZSFolder)'Create folder %USERPROFILE%\My Documents\ZipFonts\ZipStaging
    79   Set SourceFolder = objFSO.CreateFolder(CZFolder)'Create folder %USERPROFILE%\My Documents\ZipFonts\CompletedZips
    80   Set SourceFolder = NOTHING
    81 End If
    82
    83 MoveFromDesktop
    84
    85 Set SourceFolder = objFSO.GetFolder(ZFFolder)
    86 FOR EACH file In SourceFolder.Files 'Loop through all of the files in the %USERPROFILE%\My Documents\ZipFonts folder ending with .zip
    87   EXTRACT file, ZSFolder
    88 NEXT
    89 Msgbox("Ending Script")
    90 Wscript.quit
Was it helpful?

Solution

Modifying the code from @Rich ever so slightly and tracking values with MsgBox statements in each part of the code, I did some weird stuff and came up with the following code. Which loops through, and unzips each file and moves the files to the ZipStaging folder. This was the major hurdle.

Public objFSO, objShell, objshellApp
Public SourceFolder, file
Public UserPoint, UserDesktop, UserDocs, ZFFolder, ZSFolder, CZFolder

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

UserPoint = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
UserDesktop = (UserPoint & "\Desktop")
UserDocs = (UserPoint & "\My Documents")
ZFFolder = (UserDocs & "\ZipFonts\")
ZSFolder = (ZFFolder & "ZipStaging\")
CZFolder = (ZFFolder & "CompletedZips\")

Sub DoesZipExist
   Set SourceFolder = objFSO.GetFolder(UserDesktop)
   FOR EACH file In SourceFolder.Files 'Loop through the user's Desktop folder for files ending with .zip
      If Right(LCase(file.Name),4) <> ".zip" Then    
         KeepLooking = TRUE
      Else
         KeepLooking = FALSE
         Exit FOR
         Exit Sub
      End If
   Next
   If KeepLooking = TRUE Then
      MsgBox("There are no compressed files (files ending with extension .zip) on your desktop.")
      MsgBox("Please click OK to end.")
      WScript.Quit   
   End If
End Sub

Sub InitialFolderCreation
   If objFSO.FolderExists(ZFFolder) = FALSE Then
      Set SourceFolder = objFSO.CreateFolder(ZFFolder)'Create folder %USERPROFILE%\My Documents\ZipFonts
      Set SourceFolder = Nothing
      Set SourceFolder = objFSO.CreateFolder(ZSFolder)'Create folder %USERPROFILE%\My Documents\ZipFonts\ZipStaging
      Set SourceFolder = objFSO.CreateFolder(CZFolder)'Create folder %USERPROFILE%\My Documents\ZipFonts\CompletedZips
      Set SourceFolder = NOTHING
   End If
End Sub

Sub MoveFromDesktop 'Move folders from Desktop To ZipFonts folder
   Set SourceFolder = objFSO.GetFolder(UserDesktop)
   FOR EACH file In SourceFolder.Files 'Loop through the user's Desktop folder for files ending with .zip
      If Right(LCase(file.Name),4) = ".zip" Then
     'move the file into the %USERPROFILE%\My Documents\ZipFonts folder
         objFSO.CopyFile file.Path, ZFFolder, TRUE
         objFSO.DeleteFile file.Path
         '**figure out how to report that each file is being moved or display some sort of progress bar
         '**figure out how to report that file relocation has been completed for a few seconds
     End If
   NEXT
End Sub

Function Unzip(strFileName,strFolderName)
   'Create Shell.Application so we can use the CopyHere method
    Set objshellApp = CreateObject("Shell.Application")
   'Use CopyHere to extract files
   objshellApp.NameSpace(strFolderName).CopyHere objshellApp.NameSpace(FileHolder).Items
   Set objfso = Nothing
   Set objshellApp = Nothing
End Function

DoesZipExist
InitialFolderCreation
MoveFromDesktop

Set SourceFolder = objFSO.GetFolder(ZFFolder)
FOR EACH file in Sourcefolder.files
   FileHolder = file
   Unzip FileHolder, ZSFolder
NEXT
MsgBox ("All compressed (.zip) files have been extracted. Ending script.")
Wscript.quit

I know I left out a lot of unused variables and I didn't declare some of them, but I did say I was SuperNoob! Now I need to implement the code that @Rich mentioned to cleanup the temporary files, then I should be good to go!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top