我是一个带有S卷曲和流动披肩的菜鸟。我有一位朋友将压缩字体文件下载到她的桌面。然后,她打开文件,将字体复制到C:\ fonts文件夹并安装字体。该脚本旨在用于XP。我提到我会帮她自动完成这个过程。所以我的方法是创建一个脚本,扫描她的桌面上的她下载的zip文件,将这些文件从她的桌面移到我称之为ZipFonts的文件夹,该文件夹将位于她的My Documents文件夹中。创建ZipFonts时,将在其中创建两个文件夹ZipStaging和CompletedZips。计划是将压缩文件移动到ZipFonts文件夹。然后我想循环通过ZipFonts并将压缩文件的内容清空到ZipStaging文件夹中,然后压缩文件将被移动到CompletedZips文件夹中。我一直在做这个脚本,以确保它成功运作。我已成功创建文件夹并将压缩文件从桌面移动到ZipFonts文件夹中。当我运行循环打开压缩文件时,我收到错误“Line 44 / Char 4 / Object Required:'objShellApp.Namespace(...)'/ Code:800A01A8”
我确信我拼写正确,我使用Notepad ++来确保所有内容都已定义。我把我的Set对象引用放在循环的内部和外部,看看是否有所不同,但我得到了同样的错误。我错过了什么? [代码在下面]
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
答案 0 :(得分:0)
从@Rich修改代码,并在代码的每个部分使用MsgBox语句跟踪值,我做了一些奇怪的事情,并提出了以下代码。其中循环,解压缩每个文件并将文件移动到ZipStaging文件夹。这是主要障碍。
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
我知道我遗漏了很多未使用的变量而且我没有声明其中的一些,但我确实说过我是SuperNoob!现在我需要实现@Rich提到的代码来清理临时文件,然后我应该好好去!