Results 1 to 8 of 8

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,464

    Re: Improve code with better syntax

    You can use String Interpolation…

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

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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,711

    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,711

    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

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