Arch1medes
Arch1medes

Reputation: 39

wshShell.run single line command wont set variable and pass correctly

the script is executed from the network and is supposed to bring in a config file located in the local machines C: drive with a path for mapping.

I've tried a few different ideas but every time I get "System error 67 has occurred. The network name cannot be found." Any help is appreciated as I can't resolve the issue. (I'm a newbie to command line)

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /k cd.. & cd CIEB_Group3 & set /p RootServer=<Server.txt & net use K: %RootServer% /pers:yes", 1, True

Upvotes: 0

Views: 790

Answers (1)

Sam H
Sam H

Reputation: 362

Instead of creating a VBS file that calls a single long command, it would be possible to do this using more VBScript, allowing further development at a later date if you so wished.

The below script makes the assumption that your "CIEB_Group3" stays in the same place (one cd .. up, then one cd down again) and that the text file remains as "server.txt" and only contains one entry.

Set WshShell = CreateObject("WScript.Shell")

'strpath = script execution directory
strPath = WshShell.CurrentDirectory
strPathReverse = StrReverse(strpath)

'create the parent folder filepath, equivalent to cd..
strParentFilePath = Left(strPath, Len(StrPath) - InStr(strPathReverse,"\"))

'open the text file, making the assumption it is in CIEB_Group3 and is called server.txt
Set objfso = CreateObject("Scripting.FileSystemObject")
Set textfile = objfso.OpenTextFile (strParentFilePath & "\CIEB_Group3\Server.txt", 1)

'read the text file first line into a variable
strNetworkShare = textfile.ReadLine

'map the drive, chr(32) = <space>, chr(34) = "
WshShell.Run "net use K:" & Chr(32) & Chr(34) & strNetworkShare & Chr(34) & Chr(32) & "/pers:yes", 1, 1

Alternatively, if you still would like to use the single CMD line - you may wish to try expanding the environment variable outside the CMD command, as per below.

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /k cd.. & cd CIEB_Group3 & set /p RootServer=<Server.txt & net use K: " & wshShell.ExpandEnvironmentStrings("%RootServer%") & " /pers:yes", 1, True

Upvotes: 1

Related Questions