检查 Dir 是否存在 Visual Basic(System.IO.Directory 不工作)
Check if Dir exists Visual Basic (System.IO.Directory not working)
调用此方法时只是returns错误,系统变量未定义。
我目前正在尝试制作一个 GPO 登录脚本,用于检查目录是否存在以及是否未创建目录。
我很困惑,因为有太多的 visual basic 变体,我似乎找不到我需要的东西。是我需要 vbs 或 vb.net 或 vb 的视觉基础我真的丢失了。
System.IO.Directory 只是 returns 给我一个错误,我尝试了很多其他方法,但收到相同的错误。
Option Explicit
Dim l: l = "Z:"
Dim s: s = "\TEST-SERVER\Shared Folder"
Dim Network: Set Network = CreateObject("WScript.Network")
Dim CheckDrive: Set CheckDrive = Network.EnumNetworkDrives()
Dim DriveExists: DriveExists = False
Dim i
'check to see if drive exists
For i = 0 to CheckDrive.Count - 1
If CheckDrive.Item(i) = l Then
DriveExists = True
End If
Next
'if drive doesnt map it
If DriveExists = False Then
Network.MapNetworkDrive l, s, False
Else
'drive already mapped
End If
Dim strDirectory
strDirectory = "C:\Screensaver"
If(Not System.IO.Directory.Exists(strDirectory)) Then
System.IO.Directory.CreateDirectory(strDirectory)
End If
System.* 适用于 VB.net,但脚本的其余部分看起来像是 VBS。 VBS 可以使用 FileSystemObject 与文件夹进行交互。
在您尝试创建目录的部分试试这个:
Dim strDirectory
strDirectory = "C:\Screensaver"
Set fso = CreateObject("Scripting.FileSystemObject")
If(Not fso.FolderExists(strDirectory)) Then
fso.CreateFolder(strDirectory)
End If
调用此方法时只是returns错误,系统变量未定义。
我目前正在尝试制作一个 GPO 登录脚本,用于检查目录是否存在以及是否未创建目录。
我很困惑,因为有太多的 visual basic 变体,我似乎找不到我需要的东西。是我需要 vbs 或 vb.net 或 vb 的视觉基础我真的丢失了。
System.IO.Directory 只是 returns 给我一个错误,我尝试了很多其他方法,但收到相同的错误。
Option Explicit
Dim l: l = "Z:"
Dim s: s = "\TEST-SERVER\Shared Folder"
Dim Network: Set Network = CreateObject("WScript.Network")
Dim CheckDrive: Set CheckDrive = Network.EnumNetworkDrives()
Dim DriveExists: DriveExists = False
Dim i
'check to see if drive exists
For i = 0 to CheckDrive.Count - 1
If CheckDrive.Item(i) = l Then
DriveExists = True
End If
Next
'if drive doesnt map it
If DriveExists = False Then
Network.MapNetworkDrive l, s, False
Else
'drive already mapped
End If
Dim strDirectory
strDirectory = "C:\Screensaver"
If(Not System.IO.Directory.Exists(strDirectory)) Then
System.IO.Directory.CreateDirectory(strDirectory)
End If
System.* 适用于 VB.net,但脚本的其余部分看起来像是 VBS。 VBS 可以使用 FileSystemObject 与文件夹进行交互。
在您尝试创建目录的部分试试这个:
Dim strDirectory
strDirectory = "C:\Screensaver"
Set fso = CreateObject("Scripting.FileSystemObject")
If(Not fso.FolderExists(strDirectory)) Then
fso.CreateFolder(strDirectory)
End If