0

Creating a script to share folders, having format issues.

Below works from CMD, but not the spaces:

mkdir "C:\Program Files (x86)\Blank"
net share "Blank=c:\Program Files (x86)\Blank" /GRANT:everyone,full

not working:

net share "Blank blank blank blank"=C:\"Program Files 
(x86)\Blank blank blank blank" /grant:everyone,full

How do I format this in a script to get the brackets and spaces in CMD?

'CMD is run as administrator.
'Then the script is run from CMD, by typing the name/location of the script.
'The sendkeys function is typing into CMD.
'Below is the .vbs

dim filesys 
set f=CreateObject("Scripting.FileSystemObject") 
Set ws = Wscript.CreateObject("Wscript.Shell")

ws.sendkeys "mkdir ""C:\Program Files {(}x86{)}\Blank blank blan blank"""
ws.sendkeys "{enter}"
wscript.sleep 1000
ws.sendkeys "net share ""Blank blank blank blank""=C:\""Program Files 
{(}x86{)}\Blank blank blank blank"" /grant:everyone,full"
ws.sendkeys "{enter}"

This adds the (), but I get a syntax error (below). Below is the CMD output with error.

C:\Scripts>net share "Blank blank blank blank"=C:\"Program Files 
(x86)\Blank blank blank blank" /grant:everyone,full
The syntax of this command is:

NET SHARE
sharename
      sharename=drive:path [/GRANT:user,[READ | CHANGE | FULL]]
                           [/USERS:number | /UNLIMITED]
                           [/REMARK:"text"]
                           [/CACHE:Manual | Documents| Programs | 
BranchCache | None]
      sharename [/USERS:number | /UNLIMITED]
                [/REMARK:"text"]
                [/CACHE:Manual | Documents | Programs | BranchCache | 
None]
      {sharename | devicename | drive:path} /DELETE
      sharename \\computername /DELETE
user692942
  • 16,398
  • 7
  • 76
  • 175
Jay
  • 1
  • 1
  • Why are you trying to use `SendKeys()`? – user692942 Sep 11 '19 at 23:14
  • 1
    Possible duplicate of [Error while sending ( character with sendkeys in vbscript](https://stackoverflow.com/questions/10090602/error-while-sending-character-with-sendkeys-in-vbscript) – user692942 Sep 11 '19 at 23:58
  • 1
    You have the starting quote `"` in `Program Files` in the wrong place it should come after the `=` before `C:\`. – user692942 Sep 12 '19 at 06:20

1 Answers1

0

Update

Still not clear on why you insist on using SendKeys() but after reviewing your latest edit it's clear you have the starting quote for the path after the equals sign in the wrong place, the quotes should encompass the entire path, the line should read;

ws.sendkeys "net share ""Blank blank blank blank""=""C:\Program Files {(}x86{)}\Blank blank blank blank"" /grant:everyone,full"

Output will be;

C:\Scripts>net share "Blank blank blank blank"="C:\Program Files (x86)\Blank blank blank blank" /grant:everyone,full

Not entirely sure why you are using SendKeys() instead of Run() to attempt to execute a command but it is SendKeys() that is tripping you up.

From SendKeys Method
The SendKeys method uses some characters as modifiers of characters (instead of using their face-values). This set of special characters consists of parentheses, brackets, braces
...

To specify that a combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, create a compound string argument with the modified keystrokes enclosed in parentheses.

The string you have crafted would work perfectly well with the Run() method but SendKeys() is not designed to be used like this because certain characters have special meaning including parentheses.

To be clear the parentheses aren't skipped they merely have special meaning. When constructing a string to be executed by SendKeys() the method expects a specially crafted string that denotes the keys to be pressed not a command line to execute.


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175
  • The sendkeys function is working OK. This script is running in Command prompt. The sendkeys are being sent to command prompt, to type a larger set of commands, It's tripping up this particular command. The () in Program Files (x86) is being skipped even though it's in quotes. I have also tried {(}{)}, but that doesn't seem to work either. – Jay Sep 11 '19 at 23:40
  • Possibly, I'd have to quote the "{(}" "{)}" as well? – Jay Sep 11 '19 at 23:43
  • @Jay you need to [edit your question](https://stackoverflow.com/posts/57897000/edit) and show what you have tried. – user692942 Sep 11 '19 at 23:53
  • I have edited the question; included the .vbs and command prompt output.The mkdir portion works. It's a syntax issue, but I'm not sure what needs to be changed. The same formatting works if I take out the spaces in the name of the folder (not a possible solution for me). – Jay Sep 12 '19 at 01:17
  • @Jay see my latest update in response to your edit. – user692942 Sep 12 '19 at 06:28