Results 1 to 19 of 19

Thread: Importing data, is there an easyier way?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Importing data, is there an easyier way?

    Hey, anyone got any ideas of an easy way to set the following up with several .reg files:

    Code:
    Private Sub cmdVideoStream_Click()
    Dim wShell As Object
    Set wShell = CreateObject("wScript.Shell")
        wShell.Run "regedit /s C:\Maintainer\Video Stream\User Name.reg"
        MsgBox "Files Copied", vbInformation + vbOKOnly, Me.Caption
    End Sub
    This code works perfectly well, but I'm not so sure of how I can import more .reg files. I suppose it would be possible to just copy them all into the code 1 by 1 with the wshell.run "regedit /s location of file" but surely there is an easyier way?.

    thanks in advance

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    Why not pass the file name as a parameter to a sub:
    vb Code:
    1. Private Sub RegImport(RegFileName as String)
    2.    Dim wShell As Object
    3.    
    4.    Set wShell = CreateObject("wScript.Shell")
    5.    wShell.Run "regedit /s C:\Maintainer\Video Stream\" & RegFileName
    6. End Sub
    Then all you have to do call the sub in a loop, passing the file names.

  3. #3
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Importing data, is there an easyier way?

    Why not.. lose the scripts all together and write the file copying stuff into your program?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    Got the script as from my investigation it was required to open .reg files.

    So I presume I add the code stated above external to the command button and then add something that loops in the command button?. (sorry, limited knowledge)
    Last edited by jbennett; Feb 12th, 2008 at 04:52 PM.

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    Yep. You'd have to name the files appropriately (ie - the same name but with a number prefix: 1Filename.reg, 2Filename.reg etc)

    If they all have different names then just call the sub:

    vb Code:
    1. Private Sub cmdVideoStream_Click()
    2.  
    3.    RegImport "Firstfile.reg"
    4.    RegImport "Second.reg"
    5.    RegImport "Third.reg"
    6.  
    7. 'etc
    8.  
    9. End Sub

    EDIT: Forgot the quotation marks for the parameters......
    Last edited by schoolbusdriver; Feb 12th, 2008 at 05:14 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    This is what i've come up with, but seem to get an variable not defined error on the comments.reg

    Code:
    Private Sub regimprt(RegFileName As String)
    End Sub
    Dim wShell As Object
    Set wShell = CreateObject("wScript.Shell")
    wShell.Run "regedit /s ""C:\Maintainer\Video Stream\"" & RegFileName"
    End Sub
    
    Private Sub cmdVideoStream_Click()
    regimprt Comments.reg
    regimprt Second.reg
    RegImport Third.reg
    End Sub
    End Sub
    Update
    ------
    I presume this may be the case because I haven't added the loop

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Importing data, is there an easyier way?

    I'm more confused about the use of WShell's Run() method instead of just using VB's Shell() function.

    This must be a copy/paste job from some existing WSH script.


    Put Option Explicit up at the top. It'll help you find your typos.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    I have tried to use shell with .reg files and it doesn't seem to work, hence the reason for using wshell


    Update
    -----------

    I see the quotations, that might have helped :-) Thats the issue fixed, brilliant.

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    Quote Originally Posted by jbennett
    I presume this may be the case because I haven't added the loop
    Erm... no. See my edit in my last post - forgot the quotation marks.
    You've got a couple of extraneous "End Sub"s in your code though.

    vb Code:
    1. Private Sub regimprt(RegFileName As String)
    2. Dim wShell As Object
    3. Set wShell = CreateObject("wScript.Shell")
    4. wShell.Run "regedit /s ""C:\Maintainer\Video Stream\"" & RegFileName"
    5. End Sub
    6.  
    7. Private Sub cmdVideoStream_Click()
    8. regimprt "Comments.reg"
    9. regimprt "Second.reg"
    10. regimprt "Third.reg"
    11. End Sub
    Last edited by schoolbusdriver; Feb 12th, 2008 at 05:24 PM.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    Yea I see that I have some end subs in the wrong place. Corrected that issue now. Going to go try this out, cheers

  11. #11
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    @ dilettante. Shell has problems with long paths/filenames that contain spaces. I'd use ShellExecute personally (for reg files).

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    Had a bit of a tidy up, this is what i've got now.

    Code:
    Private Sub regimprt(RegFileName As String)
    
    Dim wShell As Object
    Set wShell = CreateObject("wScript.Shell")
        wShell.Run "regedit /s ""C:\Maintainer\Video Stream\"" & RegFileName"
    End Sub
    
    Private Sub cmdVideoStream_Click()
        regimprt "Comments.reg"
        regimprt "FirstName.reg"
        regimprt "EmailName.reg"
    End Sub

    Seems to be accepting the code, but I notice it doesn't change the file in the registry now. I changed RegImport to regimprt as it was complaining about it being ambiguous.

  13. #13
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    You seem to have gained some extra quotation marks. Try

    "regedit /s C:\Maintainer\Video Stream\" & RegFileName

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    quotations removed but issue remains. Hmmmm. I do remember adding the extra quotations originally as it is how a command prompt would recognize the action:

    in cmd if I run regedit "C:\Maintainer\Video Stream\FirstName.reg" then it prompts me if i would like to add file to registry, and I just add an /s switch. Just presumed it would be the same case in VB.

    Anyhow, I changed the code as suggested which is displayed below still seems to not work, strange)

    Code:
    Private Sub regimprt(RegFileName As String)
    
    Dim wShell As Object
    Set wShell = CreateObject("wScript.Shell")
        wShell.Run "regedit /s C:\Maintainer\Video Stream\" & RegFileName
    End Sub
    
    Private Sub cmdVideoStream_Click()
        regimprt "Comments.reg"
        regimprt "FirstName.reg"
        regimprt "EmailName.reg"
    End Sub
    Last edited by jbennett; Feb 12th, 2008 at 05:47 PM.

  15. #15
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    I've just tried your original code (post 1) without the /s switch, and it gives an error . Looks like WSH simply doesn't like the syntax - possibly because of the spaces with long path/file names I mentioned earlier (with Shell). Tried wShell.Run a few different ways without success. I think you'll have to use ShellExecute instead. Do you want to do this ?

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    Yeah I noticed that when removing the /s switch that it comes up with all sorts of crazy ideas, asking to copy parts of the string etc, which i presume when the /s is turned on you don't see these errors, but in the background there is nothing actually copying to the registry, hence it doesn't work.

    I've never used shellexecute before, but if it can get me around this then i'm more than willing to try it out.

  17. #17
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing data, is there an easyier way?

    No problem. I always seem to find myself in the registry whenever I write a prog, so I've got many pre-built routines. I think I've ripped this out properly....

    Note that you'll have to provide the full path/filename to ImportRegFile with the code "as is" unless you want to modify "strParameters = Chr(34) & strFilName & Chr(34)" to include the path.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    4.    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    5.    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    6.  
    7. Private Const SW_HIDE = 0
    8.  
    9. Private Sub Form_Load()
    10.    MsgBox ImportRegFile "Comments.reg"
    11. End Sub
    12.  
    13. Private Function ImportRegFile(strFilName As String) As String
    14.    Dim lngRetval As Long
    15.    Dim strParameters As String
    16.    
    17.    strParameters = Chr(34) & strFilName & Chr(34)
    18.    lngRetval = ShellExecute(0&, "open", "regedit.exe", strParameters, "C:\", SW_HIDE)
    19. 'The following isn't really needed unless you want to check.......
    20. 'Comment it out if need be..... together with "As String" on the end of
    21. '"Public Function ImportRegFile(strFilName As String) As String"
    22. 'and remove "MsgBox" (in Form_Load) when calling this.
    23.    If lngRetval <= 32 Then
    24.       ImportRegFile = "Error"
    25.    Else
    26.       ImportRegFile = "Success"
    27.    End If
    28. End Function

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    cool thanks, of to bed now but will post back tommorow with my results.

    Have a good evening..

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    195

    Re: Importing data, is there an easyier way?

    Hey again,
    I have now managed to get the original idea working, I just had a good fiddle around with the code and found that wShell.exec allows me to pull in the files from the loop. Code is the following:

    Code:
    Private Sub regimprt(RegFileName As String)
    Dim wShell As WshShell
    Set wShell = CreateObject("wScript.Shell")
    wShell.Exec "regedit /s ""C:\Maintainer\Video Stream\" & RegFileName
    MsgBox "Files Copied", vbInformation + vbOKOnly, Me.Caption

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