首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBScript多文件夹检查if then语句

VBScript多文件夹检查if then语句
EN

Stack Overflow用户
提问于 2013-10-22 05:02:01
回答 2查看 1.7K关注 0票数 0

我之前已经很好地解决了这个问题,但如果其中一个文件夹不在那里,就会出现错误,所以我试着修复它。我花了一段时间(在工作中尽我所能)寻找解决方案,并尝试了不同的方法,但仍然一无所获,我的IT罚单在工作中堆积如山。感谢您提供的帮助。第60行60个字符出现语法错误,再次感谢。

代码语言:javascript
复制
Option Explicit 
Dim objFSO, Folder1, Folder2, Folder3, zipFile
Dim ShellApp, zip, oFile, CurDate, MacAdd, objWMIService
Dim MyTarget, MyHex, MyBinary, i, strComputer, objItem, FormatMAC
Dim oShell, oCTF, CurDir, scriptPath, oRegEx, colItems
Dim FoldPath1, FoldPath2, FoldPath3, foldPathArray
Const FOF_SIMPLEPROGRESS = 256

'Grabs MAC from current machine
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colItems
    MacAdd = objItem.MACAddress
Next

'Finds the pattern of a MAC address then changes it for
'file naming purposes. You can change the FormatMAC line of the code
'in parenthesis where the periods are, to whatever you like
'as long as its within the standard file naming convention
Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Pattern = "([\dA-F]{2}).?([\dA-F]{2}).?([\dA-F]" _
& "{2}).?([\dA-F]{2}).?([\dA-F]{2}).?([\dA-F]{2})"
FormatMAC = oRegEx.Replace(MacAdd, "$1.$2.$3.$4.$5.$6")

'Gets current date in a format for file naming
'Periods can be replaced with anything that is standard to
'file naming convention
CurDate = Month(Date) & "." & Day(Date) & "." & Year(Date)

'Gets path of the directory where the script is being ran from
Set objFSO = CreateObject("Scripting.FileSystemObject")
scriptPath = Wscript.ScriptFullName
Set oFile = objFSO.GetFile(scriptPath)
CurDir = objFSO.GetParentFolderName(oFile)

'where and what the zip file will be called/saved
MyTarget = CurDir & "\" & "IRAP_LOGS_" & CurDate & "_" & FormatMAC & ".zip"

'Actual creation of the zip file
MyHex = Array(80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0)
For i = 0 To UBound(MyHex)
    MyBinary = MyBinary & Chr(MyHex(i))
Next
Set oShell = CreateObject("WScript.Shell")
Set oCTF = objFSO.CreateTextFile(MyTarget, True)
oCTF.Write MyBinary
oCTF.Close
Set oCTF = Nothing
wScript.Sleep(3000)

folder1 = True
folder2 = True
folder3 = True

'Adds folders to the zip file created earlier
'change these folders to whatever is needing to be copied into the zip folder
'Folder1
If not objFSO.FolderExists("C:\Windows\Temp\SMSTSLog") and If not objFSO.FolderExists("X:\Windows\Temp\SMSTSLog") then
    Folder1 = false
End If

If objFSO.FolderExists("C:\Windows\Temp\SMSTSLog") Then
    Folder1 = "C:\Windows\Temp\SMSTSLog"
    Set FoldPath1 = objFSO.getFolder(Folder1)
Else
    Folder1 = "X:\windows\Temp\SMSTSLog"
    Set FoldPath1 = objFSO.getFolder(Folder1)
End If

'Folder2
    If not objFSO.FolderExists("C:\Windows\System32\CCM\Logs") and If not objFSO.FolderExists("X:\Windows\System32\CCM\Logs") then
    Folder2 = false
End If
If objFSO.FolderEXists("C:\Windows\System32\CCM\Logs") Then
    Folder2 = "C:\Windows\System32\CCM\Logs"
    Set FoldPath2 = objFSO.getFolder(Folder2)
Else
    Folder2 = "X:\Windows\System32\CCM\Logs"
    Set FoldPath2 = objFSO.getFolder(Folder2)
End If
'Folder3
If not objFSO.FolderExists("C:\Windows\SysWOW64\CCM\Logs") and If not objFSO.FolderExists("X:\Windows\SysWOW64\CCM\Logs") then
    Folder3 = false
End If
If objFSO.FolderExists("C:\Windows\SysWOW64\CCM\Logs") Then
    Folder3 = "C:\Windows\SysWOW64\CCM\Logs"
    set FolderPath3 =objFSO.getFolder(Folder3)
Else
    Folder3 = "X:\Windows\SysWOW64\CCM\Logs"
    Set FoldPath3 = objFSO.getFolder(Folder3)
End If

set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.OpenTextFile(MyTarget, 2, True).Write "PK" & Chr(5) & Chr(6) _
& String(18, Chr(0))

Set ShellApp = CreateObject("Shell.Application")
Set zip = ShellApp.NameSpace(MyTarget)





'checks if files are there before trying to copy
'otherwise it will error out
If folder1 = True And FoldPath1.files.Count >= 1 Then
    zip.CopyHere Folder1
End If
WScript.Sleep 3000
If folder2 = true And FoldPath2.files.Count >= 1 Then
     zip.CopyHere Folder2
End If
WScript.Sleep 3000
If folder3 = true And FoldPath3.files.Count >= 1 Then
     zip.CopyHere Folder3
End If


WScript.Sleep 5000

set ShellApp = Nothing
set ZipFile = Nothing
Set Folder1 = Nothing
Set Folder2 = Nothing
Set Folder3 = Nothing

createobject("wscript.shell").popup "Zip File Created Successfully", 3
EN

回答 2

Stack Overflow用户

发布于 2013-10-22 05:34:57

我认为。第60行是:

代码语言:javascript
复制
If not objFSO.FolderExists("C:\Windows\Temp\SMSTSLog") and If not objFSO.FolderExists("X:\Windows\Temp\SMSTSLog") then

合取条件应如下所示:

代码语言:javascript
复制
If somecondition And othercondition Then

所以去掉第二个/嵌入的"If“。

票数 1
EN

Stack Overflow用户

发布于 2013-10-22 11:50:16

您可以使用这个逻辑Not A And Not B等于Not (A Or B)来更改这些块

代码语言:javascript
复制
Folder1 = Not (objFSO.FolderExists("C:\Windows\Temp\SMSTSLog") Or _
    objFSO.FolderExists("X:\Windows\Temp\SMSTSLog"))

Folder2 = Not (objFSO.FolderExists("C:\Windows\System32\CCM\Logs") Or _
    objFSO.FolderExists("X:\Windows\System32\CCM\Logs"))

Folder3 = Not (objFSO.FolderExists("C:\Windows\SysWOW64\CCM\Logs") Or _
    objFSO.FolderExists("X:\Windows\SysWOW64\CCM\Logs"))

此外,您还可以使用稍后不使用FolderPath3Set FolderPath3 = objFSO.getFolder(Folder3)。应该是Set FoldPath3 = objFSO.getFolder(Folder3)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19504649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档