|
-
Feb 12th, 2008, 02:52 PM
#1
Thread Starter
Addicted Member
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
-
Feb 12th, 2008, 04:16 PM
#2
Re: Importing data, is there an easyier way?
Why not pass the file name as a parameter to a sub:
vb Code:
Private Sub RegImport(RegFileName as String)
Dim wShell As Object
Set wShell = CreateObject("wScript.Shell")
wShell.Run "regedit /s C:\Maintainer\Video Stream\" & RegFileName
End Sub
Then all you have to do call the sub in a loop, passing the file names.
-
Feb 12th, 2008, 04:18 PM
#3
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
-
Feb 12th, 2008, 04:46 PM
#4
Thread Starter
Addicted Member
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.
-
Feb 12th, 2008, 05:02 PM
#5
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:
Private Sub cmdVideoStream_Click()
RegImport "Firstfile.reg"
RegImport "Second.reg"
RegImport "Third.reg"
'etc
End Sub
EDIT: Forgot the quotation marks for the parameters......
Last edited by schoolbusdriver; Feb 12th, 2008 at 05:14 PM.
-
Feb 12th, 2008, 05:12 PM
#6
Thread Starter
Addicted Member
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
-
Feb 12th, 2008, 05:13 PM
#7
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.
-
Feb 12th, 2008, 05:16 PM
#8
Thread Starter
Addicted Member
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.
-
Feb 12th, 2008, 05:17 PM
#9
Re: Importing data, is there an easyier way?
 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:
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 "Second.reg"
regimprt "Third.reg"
End Sub
Last edited by schoolbusdriver; Feb 12th, 2008 at 05:24 PM.
-
Feb 12th, 2008, 05:20 PM
#10
Thread Starter
Addicted Member
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
-
Feb 12th, 2008, 05:20 PM
#11
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).
-
Feb 12th, 2008, 05:27 PM
#12
Thread Starter
Addicted Member
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.
-
Feb 12th, 2008, 05:32 PM
#13
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
-
Feb 12th, 2008, 05:35 PM
#14
Thread Starter
Addicted Member
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.
-
Feb 12th, 2008, 06:10 PM
#15
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 ?
-
Feb 12th, 2008, 06:14 PM
#16
Thread Starter
Addicted Member
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.
-
Feb 12th, 2008, 06:34 PM
#17
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:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_HIDE = 0
Private Sub Form_Load()
MsgBox ImportRegFile "Comments.reg"
End Sub
Private Function ImportRegFile(strFilName As String) As String
Dim lngRetval As Long
Dim strParameters As String
strParameters = Chr(34) & strFilName & Chr(34)
lngRetval = ShellExecute(0&, "open", "regedit.exe", strParameters, "C:\", SW_HIDE)
'The following isn't really needed unless you want to check.......
'Comment it out if need be..... together with "As String" on the end of
'"Public Function ImportRegFile(strFilName As String) As String"
'and remove "MsgBox" (in Form_Load) when calling this.
If lngRetval <= 32 Then
ImportRegFile = "Error"
Else
ImportRegFile = "Success"
End If
End Function
-
Feb 12th, 2008, 06:46 PM
#18
Thread Starter
Addicted Member
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..
-
Feb 12th, 2008, 07:28 PM
#19
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|