Atlassay
Atlassay

Reputation: 1

Getting Error (800A01A8) When Creating Folder Network Share with WMI in VBS

I'm trying to create a network share for a folder using .vbs. However, when I attempt to set up the share with WMI, I encounter an 800A01A8 error. The folder creation works fine, but when I use the Win32_Share class to enable sharing, I get an error.

This is the .vbs file:

Dim sourceFolder
Dim destinationFolder
sourceFolder = "E:\"
destinationFolder = "C:\Users\ETKullanici\FMSBuild\"
folderPath = "C:\Users\ETKullanici\FMSBuild"
shareName = "FMSBuild"


Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}").Get("Win32_Share")


If Not fso.DriveExists("E:") Then
    WScript.Quit
End If

If Not fso.FolderExists(destinationFolder) Then
    fso.CreateFolder(destinationFolder)
End If

Set share = wmi.Create(folderPath, shareName, 0, 10)
If share <> 0 Then
    WScript.Echo "Paylaşım oluşturulamadı. Hata kodu: " & share
Else
    WScript.Echo "Paylaşım başarıyla oluşturuldu: " & shareName
End If

Function GetUniqueFileName(destinationPath, fileName)
    Dim baseName, ext, counter, newFileName
    
    baseName = fso.GetBaseName(fileName)
    ext = fso.GetExtensionName(fileName)
    
    counter = 1
    newFileName = fileName
    
    Do While fso.FileExists(destinationPath & "\" & newFileName)
        newFileName = baseName & " (" & counter & ")." & ext
        counter = counter + 1
    Loop
    
    GetUniqueFileName = newFileName
End Function

Sub ProcessFolder(folder)
    For Each file In folder.Files
        If LCase(fso.GetExtensionName(file.Name)) = "pdf" Or _
           LCase(fso.GetExtensionName(file.Name)) = "doc" Or _
           LCase(fso.GetExtensionName(file.Name)) = "docx" Then
            Dim uniqueFileName
            uniqueFileName = GetUniqueFileName(destinationFolder, fso.GetFileName(file))
            fso.CopyFile file.Path, destinationFolder & "\" & uniqueFileName
        End If
    Next

    For Each subfolder In folder.SubFolders
        ProcessFolder subfolder
    Next
End Sub

Set folder = fso.GetFolder(sourceFolder)
ProcessFolder folder

When I run the code, I get the following error:

Error: Object required: 'wmi.Create(...)'
Code: 800A01A8
Source: Microsoft VBScript runtime error

Attempting to set up the share using net share instead, but couldn't fully activate the network share this way either.

How can I properly set up the share using Win32_Share in WMI? Or is there an alternative way to grant network share permissions through WMI in .vbs?

What I'm trying to do is actually take some filtered files from drive E on device A and create a folder. Then transfer the folder from device A to device B with the same IP. I showed the code I will run on device A in the previous section, and this is the code I will run on device B:

Dim sourceFolder, destinationFolder, fso

sourceFolder = "\\hostname\FMSBuild\"
destinationFolder = "C:\Users\ETKullanici\FMSBuild\"

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FolderExists(destinationFolder) Then
    fso.CreateFolder(destinationFolder)
End If

fso.CopyFolder sourceFolder, destinationFolder

I tried to do the same thing using net share, it didn't give an error, but it didn't do what I wanted either. That's why I tried this method with the guidance of chat gpt.

Upvotes: 0

Views: 43

Answers (0)

Related Questions