Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

representing quotes

Status
Not open for further replies.

cykill

Technical User
Feb 9, 2005
16
0
0
US
i'm having some issues representing quotes within the exec(" "). here's what i have.
dim user_name
user_name = "Test test"
Set wshShell = createobject("wscript.shell")
Set permissions = wshShell.Exec("xcacls.exe "d:\calendar\""& Lcase(user_name) &""" /G administrator:F ""& Lcase(user_name) &"":F /Y)


I'm trying to get the code above to do this on a win2k command line.
xcacls "d:\calendar\test test" /F administrator:F "test test":F /y
 
try concat of the Chr(34) in your string.

on that note I would build the command prior to passing it in a variable to make this more managable.

something to the effect of

Code:
Dim cmd
cmd = "xcacls.exe "
cmd = cmd & Chr(34) & d:\calendar\" & Lcase(user_name)
...etc...



General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
It is pretty common for people to have trouble with quote escaping in VB/VBScript for some reason. I'm not sure why, but it does lead to code that looks like onpt's above.

That's one solution of course, but I find it pretty forced and awkward myself. It can really slow your code down in compiled VB too, which is why I avoid it in VBScript as well.

There is another way to approach writing string expressions like yours. It's pretty basic (no pun intended). First, avoid writing long one-liners by breaking things over several lines and using continuation characters. Then try writing the string expression first by using some other symbol besides the quotation mark:
[tt]
Set permissions = _
wshShell.Exec("xcacls.exe $d:\calendar\" _
& LCase(user_name) _
& "$ /G administrator:F $" _
& LCase(user_name) _
& "$:F /Y")
[/tt]
Then replace the substitute symbol ($ here) with escaped (doubled) quotes:
[tt]
Set permissions = _
wshShell.Exec("xcacls.exe ""d:\calendar\" _
& LCase(user_name) _
& """ /G administrator:F """ _
& LCase(user_name) _
& """:F /Y")
[/tt]

 
I agree concatenation is a performance hit and I would not recommend a long liner in any way. However I also have never liked the """ for the most part of maintenance. All to often when these quotes get out of hand it can be hair pulling to get things resolved when the business asks for it to be done now. In scripting land and in reality the usage is not commonly a hit on performance either. Take that lightly though and always test your code with benchmark scripts and or test different methods to see if performance is effected to the point the change should be made.

All that being said, PHV's method above is prety nifty and I think I may adapt it. however it leaves the next person maintaining or fixing it still in the wind of counting quotes ;)

-onpnt


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
apologies dilettante I was reading too many different postings at once and made the handle error.

a thousand apologies for that. Very disrespectful where I come from and qill not happen again


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
No problem onpt and I fully agree with your concerns expressed following my post.

I realize that people often struggle to both create and maintain expressions with embedded quote characters. You are right in saying that most script isn't very time critical, my concern was in developing habits that would impact performance in my own non-script code - related to excessive concatentation.

In WSH scripts I often resort to an alternative for lengthy strings such as SQL expressions:

sample.wsf
Code:
<job>
  <resource id="xcacls">
    xcacls.exe "d:\calendar\$UN$" /G administrator:F "$UN$":F /Y
  </resource>
  <script language="VBScript">
    Option Explicit

    Dim user_name

    Function GetResClean(ResID)
      GetResClean = Trim(Replace(getResource(ResID), vbNewLine, ""))
    End Function

    user_name = "Fred"
    MsgBox Replace(GetResClean("xcacls"), "$UN$", LCase(user_name))
  </script>
</job>
Once again there is a runtime penalty, but as you said this is often irrelevant in scripts or when speed is important one can fall back on other approaches.

You don't need a helper function like my [tt]GetResClean()[/tt] either for one-liners if you type it all on one line with the open and close [tt]<resource>[/tt] tags inline.

For a case like a 10 line SQL expression with lots of quoted literals in it this is ever so much cleaner than using concatenated [tt]Chr(34)[/tt] all over the place, and a lot better than trying to get lots of escaped quotes entered correctly too.


It just shows there are multiple ways to deal with some simple coding issues.
 
I still doesn't work. any ideas?
 
Try;

"xcacls.exe " & Chr$(34) & "d:\calendar\" & LCase(user_name) & Chr$(34) & " / G; administrator: F " & Chr$(34) & LCase(user_name) & Chr$(34) & ":F /Y"

Hugh,
 
Chr$() is no vbs, only vb.

Op wrote:
>I still doesn't work. any ideas?
Op has to show the revised line of script for the claim. Even though it is fully spelt out in all the good advices, it is not uncommon, more often than not, not understood word for word.
 
i've pretty much given up on xcacls to change permissions. is there another way to change permissions?
 
Try;

dim user_name,dq,StrRequired

user_name = "Test test"
dq = """"
StrRequired = "xcacls.exe " & dq & "d:\calendar\" & LCase(user_name) & dq & " / G; administrator: F " & dq & LCase(user_name) & dq & ":F /Y"

msgbox StrRequired

Debugged in VBS this time, sorry about the chr$(34) last time.

Hugh
 
I agree with the suggestion to MsgBox or otherwise display the result of that string expression. This may point out the flaw.
 
ok. the msgbox output is

xcacls.exe "d:\calendar\test test" /G administrator:F "test test":F /Y

i don't think the problem is in here. the problem is where i put this whole line into wshshell.exec(" stuff here "). there's an error where exec(" is saying ) expected. its getting the two outer quotes in exe(" ")mixed with the stuff inside. do anybody get what i mean?
 
This is the syntax spec. (It seems there is a typos too in the documentation.)
[tt] xcaclsFileName[/t][/e][/x][/c][red][[/red][/gUser:permissions;Spec][/rUser][/pUser:permissions;Spec] [highlight][...][/highlight]][/dUser [...]][/y][/?|/h][/tt]

What is the highlighted part meant?

The commandline to do the job is like this.
[tt]
dim scmd
scmd="xcacls.exe ""d:\calendar\" & Lcase(user_name) & """ /G administrator:F /G " & Lcase(user_name) & """:F /Y"
Set permissions = wshShell.Exec(scmd)
do while permissions.status=0 : wscript.sleep 200 : loop
[/tt]
Also, note you're dealing with local user accounts. If you want to take on domain accounts, make sure append the domainname to the username (domainname\username).
 
Amendment

I miss a quotes too. This is a re-take.
[tt]
dim scmd
scmd="xcacls.exe ""d:\calendar\" & Lcase(user_name) & """ /G administrator:F /G [red]""[/red]" & Lcase(user_name) & """:F /Y"
Set permissions = wshShell.Exec(scmd)
do while permissions.status=0 : wscript.sleep 200 : loop
[/tt]
 
Just to add a note of worst scenario. Do it in two separate exec, and then all the ambiguity should be gone and you even might do it in loop each for different user so that the line scmd would be all stuffed up.
[tt]
dim scmd
scmd="xcacls.exe ""d:\calendar\" & Lcase(user_name) & """ /G administrator:F /Y"
Set permissions = wshShell.Exec(scmd)
do while permissions.status=0 : wscript.sleep 200 : loop
scmd="xcacls.exe ""d:\calendar\" & Lcase(user_name) & """ /G """ & Lcase(user_name) & """:F /Y"
Set permissions = wshShell.Exec(scmd)
do while permissions.status=0 : wscript.sleep 200 : loop
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top