Results 1 to 9 of 9

Thread: Improve code with better syntax

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Improve code with better syntax

    Hello, can someone help me improve this query syntax to be better input without so much """ i use

    Code:
    Using batFile As New StreamWriter(batFileName)
                'User1 Create
                batFile.WriteLine($"net user " & TxtAutoUser.Text & " " & TxtAutoPass.Text & " /ADD /FULLNAME:" & """" & TxtAutoFullName.Text & """" & " /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never")
                batFile.WriteLine($"wmic path Win32_UserAccount WHERE Name=" & """" & TxtAutoUser.Text & """" & " set PasswordExpires=False")
                batFile.WriteLine($"net localgroup Administrators " & TxtAutoUser.Text & " /ADD")
                'User2 Create
                batFile.WriteLine($"net user " & TxtTabletUser.Text & " " & TxtTabletPassword.Text & " /ADD /FULLNAME:" & """" & TxtTabletFullName.Text & """" & " /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never")
                batFile.WriteLine($"wmic path Win32_UserAccount WHERE Name=" & """" & TxtTabletUser.Text & """" & " set PasswordExpires=False")
                batFile.WriteLine($"net localgroup Administrators " & TxtTabletUser.Text & " /ADD")
                'End Users
                batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v AutoAdminLogon /t REG_SZ /d " & """1""" & " /f")
                batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v DefaultDomainName /t REG_SZ /d %USERDOMAIN% /f")
                batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v DefaultUsername /t REG_SZ /d " & TxtAutoUser.Text & " /f")
                batFile.WriteLine($"reg add " & """HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon""" & " /v DefaultPassword /t REG_SZ /d " & TxtAutoPass.Text & " /f")
                batFile.WriteLine($"icacls " & """" & FileSavePath & "TestFolder" & """" & " /grant " & TxtAutoUser.Text & ":(OI)(CI)F /T " & "/grant " & TxtTabletUser.Text & ":(OI)(CI)F /T")
                batFile.WriteLine($"net share " & """ShareFolder""" & "=" & """" & FileSavePath & "TestFolder" & """" & " /Remark:""Shared by System Tool"" /GRANT:Everyone,Read /GRANT:" & TxtAutoUser.Text & ",Full " & "/GRANT:" & TxtTabletUser.Text & ",Full " & "/UNLIMITED")
            End Using

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,771

    Re: Improve code with better syntax

    You can use String Interpolation…

    https://learn.microsoft.com/en-us/do...olated-strings

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,680

    Re: Improve code with better syntax

    Also, you don't need to do this:
    Code:
    WriteLine($"reg add " & """HKLM\SOFTWARE\Microso
    When this will suffice:
    Code:
    WriteLine($"reg add ""HKLM\SOFTWARE\Microso
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Improve code with better syntax

    thank you, forgot about {TxtField.Text} option

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,997

    Re: Improve code with better syntax

    Also, strings accept line breaks now. You don't need all those WriteLine calls:
    Code:
    Dim message = $"net user {TxtAutoUser.Text} {TxtAutoPass.Text} /ADD /FULLNAME: "" {TxtAutoFullName.Text} "" /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never
    wmic path Win32_UserAccount WHERE Name="" {TxtAutoUser.Text} "" set PasswordExpires=False
    etc..."
    
    batFile.WriteLine(message)
    Fiddle: https://dotnetfiddle.net/Wt0q4j
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Improve code with better syntax

    Quote Originally Posted by techgnome View Post
    Also, you don't need to do this:
    Code:
    WriteLine($"reg add " & """HKLM\SOFTWARE\Microso
    When this will suffice:
    Code:
    WriteLine($"reg add ""HKLM\SOFTWARE\Microso
    -tg
    the problem is that cmd needs to have the "Text" for the reg command that's why i input like that

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,997

    Re: Improve code with better syntax

    I have also moved this to the Code It Better forum.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Improve code with better syntax

    i got a question regarding my last 2 rows codes
    Code:
    batFile.WriteLine($"icarls ""{FileSavePath}TestFolder"" /grant {TxtAutoUser.Text}:(OI)(CI)F /T /grant {TxtTabletUser.Text}:(OI)(CI)F /T")
    batFile.WriteLine($"net share SharedFolder=""{FileSavePath}TestFolder"" /Remark:""Shared by System Tool"" /GRANT:Everyone,Read /GRANT:{TxtAutoUser.Text},Full /GRANT:{TxtTabletUser.Text},Full /UNLIMITED")
    so i have issue with this i think because it's already shared and not allow me to add users in advanced sharing option

  9. #9
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    550

    Re: Improve code with better syntax

    I know this is an old thread. I just had some time on my hands:

    Code:
    ' Function to write the user creation commands
    Private Sub CreateUserCommands(ByVal batFile As StreamWriter, ByVal userName As String, ByVal userPassword As String, ByVal fullName As String)
        Dim userCommands As String = $"net user {userName} {userPassword} /ADD /FULLNAME:""{fullName}"" /COMMENT:""Created User by System Tool"" /PASSWORDCHG:NO /ACTIVE:Yes /EXPIRES:Never"
        userCommands &= $" wmic path Win32_UserAccount WHERE Name=""{userName}"" set PasswordExpires=False"
        userCommands &= $" net localgroup Administrators {userName} /ADD"
        batFile.WriteLine(userCommands)
    End Sub
    
    
    ' Function to add registry entries for auto login
    Private Sub AddAutoLoginRegistry(ByVal batFile As StreamWriter, ByVal userName As String, ByVal userPassword As String)
        ' Added line breaks to make the command more readable and ensure proper formatting
        Dim registryCommands As String = $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v AutoAdminLogon /t REG_SZ /d ""1"" /f"
        registryCommands &= vbCrLf & $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultDomainName /t REG_SZ /d %USERDOMAIN% /f"
        registryCommands &= vbCrLf & $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultUsername /t REG_SZ /d {userName} /f"
        registryCommands &= vbCrLf & $"reg add ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultPassword /t REG_SZ /d {userPassword} /f"
        
        batFile.WriteLine(registryCommands)
    End Sub
    
    
    ' Function to handle folder permissions and sharing
    Private Sub ConfigureFolderPermissionsAndShare(ByVal batFile As StreamWriter, ByVal fileSavePath As String, ByVal autoUser As String, ByVal tabletUser As String)
        ' Delete any existing share with the same name to avoid conflict
        batFile.WriteLine("net share SharedFolder /delete")
    
        ' The rest of the commands for setting permissions and sharing
        Dim permissionsAndShareCommands As String = $"icacls ""{fileSavePath}\TestFolder"" /grant {autoUser}:(OI)(CI)F /T /grant {tabletUser}:(OI)(CI)F /T"
        permissionsAndShareCommands &= vbCrLf & $"net share SharedFolder=""{fileSavePath}\TestFolder"" /Remark:""Shared by System Tool"" /GRANT:Everyone,Read /GRANT:{autoUser},Full /GRANT:{tabletUser},Full /UNLIMITED"
        batFile.WriteLine(permissionsAndShareCommands)
    End Sub
    
    
    ' Main code to create the batch file
    Sub CreateBatchFile()
        Using batFile As New StreamWriter(batFileName)
            ' User1 Creation
            CreateUserCommands(batFile, TxtAutoUser.Text, TxtAutoPass.Text, TxtAutoFullName.Text)
    
            ' User2 Creation
            CreateUserCommands(batFile, TxtTabletUser.Text, TxtTabletPassword.Text, TxtTabletFullName.Text)
    
            ' Auto-login registry entries
            AddAutoLoginRegistry(batFile, TxtAutoUser.Text, TxtAutoPass.Text)
    
            ' Folder permissions and sharing
            ConfigureFolderPermissionsAndShare(batFile, FileSavePath, TxtAutoUser.Text, TxtTabletUser.Text)
        End Using
    End Sub
    Last edited by Peter Porter; Nov 23rd, 2024 at 01:22 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width