Bryan
Bryan

Reputation: 13

Start application with VBS whos path is different between users

All, I am attempting to script the start of an application whose path is different between each user. Is there any way to find the true path of the app (per user) and then start the program with the scripting below?

Also, what is the best way to run the application as the logged in user i.e. %username%. I have tried the scripting below (for this issue), but doesn't seem to work.

on error resume next
theDir = "C:\Users\" & createobject("wscript.shell").expandenvironmentstrings("%username%") & "\AppData\Local\Apps\2.0\ZJVRE3RK.4TQ\VLGML47Q.TPN\2rin..tion_5bfb425a74ceb3d8_0003.0004_c52ddbfe44f7690b"

theCmd = "2RingIPPSClient.exe"

Set objSh = WScript.CreateObject("WScript.Shell")

objSh.CurrentDirectory = theDir

objSh.Run theCmd

Upvotes: 1

Views: 294

Answers (2)

Hackoo
Hackoo

Reputation: 18827

You can give a try for this vbscript :

Option Explicit
Dim Ws,fso,Location,FileName,Command,Result,ReadFile,Contents
Set Ws = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Location=Ws.ExpandEnvironmentStrings("%userprofile%\AppData\Local\2.0")
FileName="2RingIPPSClient.exe"
Command = "Cmd /C Where /r "& Location &" "& FileName &" > %Tmp%\Found.txt"
Result = Ws.Run(Command,0,True)
Set ReadFile = fso.OpenTextFile (Ws.ExpandEnvironmentStrings("%Tmp%\Found.txt"), 1)
Contents = ReadFile.ReadLine
'wscript.echo Contents
Ws.run Contents 

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

Try searching for the file with a recursive function like this:

Function FindFile(name, fldr)
    For Each f In fldr.Files
        If f.Name = name Then
            res = f.Path
            Exit For
        End If
    Next

    If IsEmpty(res) Then
        For Each sf In fldr.SubFolders
            res = FindFile(name, sf)
            If res <> "" Then Exit For
        Next
    End If

    FindFile = res
End Function

Set sh  = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

cmd = FindFile("2RingIPPSClient.exe", fso.GetFolder(sh.ExpandEnvironmentStrings("%USERPROFILE%\Appdata\Local\2.0")))

If cmd <> "" Then
    'do stuff
End If

Upvotes: 1

Related Questions