Solved

Creating a registry ValueName with a backslash in VB6 / VBScript

Posted on 2007-03-29
23
1,600 Views
Last Modified: 2010-07-27
I need to write a registry value name that has a '\' in it but this is interpreted as a key. How do I get around this?
EG.
ValueName ="CN=User\, Test1,OU=Users.,OU=Departments,DC=ni,DC=ad,DC=newsint"
oShell.RegWrite "HKCU\Software\App" & ValueName, vbNullString, "REG_SZ"
...creates a new key under "App" called "CN=User", with a value of ", Test1,OU=Users.,OU=Departments,DC=ni,DC=ad,DC=newsint".
There must be an easy way around this! All help appreciated, thanks
0
Comment
Question by:NewsInternationalLtd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
  • 2
  • +1
23 Comments
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 18819226
You must "escape" the backslash.
The backslashes in the registry key are escaped by a backslash.
For example: instead of writing "User\" you will write "User\\", the first backslash being used to escape the backslash you are using and which is interpreted as a key.

Wesley
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18819310
Is this what you're looking for?

oShell.RegWrite "HKCU\Software\App\", ValueName, "REG_SZ"
0
 

Author Comment

by:NewsInternationalLtd
ID: 18819505
Both the above solutions seem to have the same outcome, ie the ValueName is ends up as the datavalue. I need it to be the name.
I can understand the theory behind what Wesley is saying regarding the escape character as this is needed in some DS commands but if I change the string with a replace statement so it now reads:
CN=User\\, Test1,OU=Users.,OU=Departments,DC=ni,DC=ad,DC=newsint
...this still ends up as a data value. I I have also tried
HKCU\Software\App\\
which still doesn't work! What am I doing wrong?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 18819885
RegWrite "HKCU\Software\App", "CN=User\\,Test1,OU=Users.,OU=Departments,DC=ni,DC=ad,DC=newsint", "REG_SZ" should work. It's a simple regwrite statement as at: http://psacake.com/web/func/
Check the syntax carefully.

Wesley
0
 

Author Comment

by:NewsInternationalLtd
ID: 18820098
This creates the dn as a DataValue under HKCU\Software\App\Default.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18820246
So, what is the value name you're trying to assign it to? CN?
0
 

Author Comment

by:NewsInternationalLtd
ID: 18820305
I simply want the dn (which obviously includes the backslash) to be the valuename, it doesn't really matter what the value is. As I'm sure you know this is usually accomplished by removing the trailing '\' but I'm really struggling to script this. Atypical value name is below.

"CN=User\, Test,OU=Users,OU=I.T.,OU=Departments,DC=ni,DC=ad,DC=newsint", "test", "REG_SZ"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18820361
Then this should work...does for me...drop the value at the end - no need to escape the backslash...

oShell.RegWrite "HKCU\Software\App\Test", ValueName, "REG_SZ"
0
 

Author Comment

by:NewsInternationalLtd
ID: 18822146
Surely this creates a key called 'App' with a VALUENAME of 'Test' and VALUEDATA of the dn (disName)? This is what happens when I run it. I would like the VALUENAME to be the dn.
For maximum clarity, this is what I'm writing:

set oshell = createobject("Wscript.Shell")
disName = "CN=User\, Test1,OU=Users,OU=I.T.,OU=Departments,DC=ni,DC=ad,DC=newsint"
oShell.RegWrite "HKCU\Software\App\Test", disName, "REG_SZ"

0
 
LVL 3

Expert Comment

by:drbayer
ID: 18823243
I haven't tested it, but have you tried

set oshell = createobject("Wscript.Shell")
disName = "CN=User\\, Test1,OU=Users,OU=I.T.,OU=Departments,DC=ni,DC=ad,DC=newsint"
oShell.RegWrite "HKCU\Software\App\" & disName, ValueData, "REG_SZ"

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18823249
Can you show me the full path youre looking for...
This creates it under the Software key...

oshell.regwrite "HKCU\Software\App", disName, "REG_SZ"
0
 

Author Comment

by:NewsInternationalLtd
ID: 18823268
Yep, just creates a new key called "CN=User" with a value of " ,Test1,OU=Users,OU=I.T.,OU=Departments,DC=ni,DC=ad,DC=newsint".
I know it's stupid and it can be done maually so I can only assume there is a way to do this programmatically. I have just logged a call with Microsoft PSS so I'll let you know how I get on.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18823318
Not 'exactly' a fix for the regwrite method, but replace this line...it will work (sets the valuedata to 0, since I'm not sure what you want that set to).

set oshell = createobject("Wscript.Shell")
disName = "CN=User\, Test1,OU=Users,OU=I.T.,OU=Departments,DC=ni,DC=ad,DC=newsint"
oShell.Run "reg add HKCU\Software\App /v " & Chr(34) & disName & Chr(34) & " /d 0", 0


0
 

Author Comment

by:NewsInternationalLtd
ID: 18823342
Sirbounty:
What you have there actually creates a VALUENAME of App with VALUEDATA of the dn under the Software key. I need it to appear as below in the registry:

Name                                                                                    Type                                       Data
CN=User\, Test1,OU=Users,OU=I.T.,OU=Departments...   REG_SZ                                    Whatever
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18823367
Alright, I was under the impression that App was already created...

Use this

set oshell = createobject("Wscript.Shell")
disName = "CN=User\, Test1,OU=Users,OU=I.T.,OU=Departments,DC=ni,DC=ad,DC=newsint"
oShell.Run "reg add HKCU\Software\App\ /v " & Chr(34) & disName & Chr(34) & " /d 0", 0
0
 

Author Comment

by:NewsInternationalLtd
ID: 18823420
Sorry, our replies are a little out of sync.
SirBounty, your last solution does work exactly as I would like although as you say not an exact fix as such. However it might be enough. I am certainly happy to award you points for this solution though. I shall do this when I find the official one from MS which I will post (if indeed there is one any better).

Thanks a lot for your efforts.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 18823546
Cool.
What I meant was - it wasn't a 'fix' for the regwrite method.
It's actually using your shell reference to run the command-line reg.exe version (which is a bit more intuitive because you can explicitly decide what you want the value (/v) and data (/d) to be...)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18835693
I thought I put in a fair amount of work for you here...I'm a bit surprised by the "B" grade...
0
 

Author Comment

by:NewsInternationalLtd
ID: 18835709
I have accepted Sir Bounty's solution as it is simple and it does work. However having spoken to MS PSS about this they tell me it can't be done with the Regwrite method, hence the problems encountered. They did howver provide me with a 'purer' solution to this which I have included here. It's a WMI solution but at least it doesn't rely on a third party utility (of sorts):
Const HKEY_CURRENT_USER = &H80000001
Set objRegistry = GetObject("winmgmts://./root/default:StdRegProv")
sPath = "SOFTWARE\App"
lRC = objRegistry.SetStringValue(HKEY_CURRENT_USER, sPath, "CN=User\, Test3, OU=Users, OU=Departments, DC=ni,DC=ad,DC=newsint", "Test")
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18835720
Fair enough...glad you got it working.
0
 

Author Comment

by:NewsInternationalLtd
ID: 18835725
Sorry, SirB, the only reason for the B was, as I said above, that I was looking for a purer solution. I VERY MUCH appreciate your efforts and you have been extremlely helpful on this, don't get me wrong! I just thought in the interests of what was originally required it wan't an exact solution for what I needed although the end result was the same.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18835756
"as I said above, that I was looking for a purer solution."
In truth, it's really 6 vs 1/2 dozen, but now I'm a bit embarrased I didn't suggest the wmi method.  I guess I couldn't see the forest for the trees...
0
 

Author Comment

by:NewsInternationalLtd
ID: 18835841
No worries thanks for you help anyway. You always manage to pull out a solution of sorts in the end!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question