File found with relative path but not absolute path? (VBS) - vbscript

I am trying to launch a shortcut from VBScript, but I am running into a very odd error.
When I use the relative path of the shortcut, the script opens the shortcut perfectly fine. However, if I use the absolute file path (copied from windows explorer so no typos or anything like that) it gives me an error saying file not found.
Relative path code:
dim x
set x = CreateObject("WScript.shell")
x.Run("Shortcut.lnk")
set x = Nothing
This opens the file.
Absolute path code:
dim x
set x = CreateObject("WScript.shell")
x.Run("C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\Shortcut.lnk")
set x = Nothing
As you can see, the code is exactly the same. However, it gives me a file not found error:
Script: C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\test.vbs
Line: 3
Char: 1
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)
The file path for the script is exactly the same in the error as the path I have put in the code, but it still gives me an error.
Any help would be appreciated.
Note: My username has been replaced with ***** just for the question.

For the shell (.Run, .Exec) pathes containing spaces need quotes. So replace
x.Run("C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\Shortcut.lnk")
with
x.Run """C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\Shortcut.lnk"""
(cf here and here)

Related

Clarification of code to change target of a Shortcut(.lnk) file

I am a Java programmer, and I do not know anything about VBScript. I needed a way to modify the target path of a certain shortcut, and I according to my research, only VBScript can help me. I tried it, but there were many errors.
I tried to resolve them by a butt ton of googling, but I think there is some other problem.
Here is the code:
Set sh = CreateObject("WScript.Shell")
Set shortcut = sh.CreateShortcut("C:\Users\pedne\Desktop\Zoom.lnk")
shortcut.TargetPath = "C:\Users\pedne\AppData\Roaming\Zoom\bin\Zoom.exe ""--url=zoommtg\://zoom.us/join?action=join&confno=955 1234 1234&pwd=12345"""
shortcut.Save
I do not know whether I need any prerequisites or external libraries for writing VBScript code, and I wrote it on notepad.I would also like to know whether there is any 100% Java or any other workaround to this. (Totally up to you)
Please excuse me if I do not understand anything about your answers as I copied the code from Change a shortcut's target from command prompt
The last error I got was at Line 3 char 1, Invalid procedure or call
Do not append the arguments to the target path. Use the Arguments property instead:
shortcut.TargetPath = "C:\Users\pedne\AppData\Roaming\Zoom\bin\Zoom.exe"
shortcut.Arguments = "--url=zoommtg\://zoom.us/join?action=join&confno=955 1234 1234&pwd=12345"
shortcut.Save

File Path When Including Nested Files in a Script

I have a script file which opens a text file located in the same directory. Let's call it SubScript.
SubScript.vbs
Function DoSomething(foo)
...
Dim Key
With CreateObject("Scripting.FileSystemObject")
Key = .OpenTextFile("key.txt", 1).ReadAll
End With
...
End Function
No problem here when the script is run on its own. However, I want to use the script above in another script file, "MainScript". The SubScript is located in a subfolder in the MainScript directory.
MainScript.vbs
With CreateObject("Scripting.FileSystemObject")
ExecuteGlobal .OpenTextFile(".\SubDir\SubScript.vbs", 1).ReadAll
End With
When I try and use the DoSomething function in the SubScript, I get a file not found error. I see what is happening, the subscript is trying to find the text file in the MainScript directory, where it doesn't exist.
Is there a way, without using an absolute file path, to make sure the SubScript loads the text file from the SubDir?
A relative path is resolved wrt the current directory of the process. Sometimes you can use the script's folder to get more flexibility. But in your case (.ExecuteGlobal), the SubScript's current directory is the current directory of the MainScript.
You should pass a path to DoSomething(), unless you can live with hardcoding ".\SubDir\key.txt".

VB script_CReating Folder inside folder

For i = 1 To 40 Step 1
ChildFolderPath = ChildFolderPath & "\" & "LargeFolder" & i
If fso.FolderExists(ChildFolderPath) Then
MsgBox ("Folder Exists")
else
fso.CreateFolder (ChildFolderPath)
End If
Next
But after creating 21 folders, I am getting error 53, "File path not found". Why?
You're hitting the Windows limitation on maximum path length, which is 260 characters.
The linked article also mentions a workaround to use extended-length paths up to (approximately) 32767 characters long – add \\?\ before the drive name. For example:
ChildFolderPath = "\\?\C:\MyFolder"
As also noted in that article, even though you can create extended-length paths programmatically, the Windows shell (e.g. Explorer) may be unable to handle them properly. For example, you may get the "The source file name(s) are larger than is supported by the file system" error when trying to delete a long path folder from Explorer or the command prompt.

Command Prompt error since i am using a generic path to open an excel file

Command Prompt its not working since i am using a generic path to open a excel file. Here is the error message:
T:\PointOfSale\Projects\Automated Testing\TASWeb\TP\TP_Branch>ruby -rubygems Tes
tTP_UK.rb
TestTP_UK.rb:19:in 'method_missing': (in OLE method `Open': )(WIN32OLERuntimeEr
ror)
OLE error code:800A03EC in Microsoft Excel
'./../../../MasterFile.xls' could not be found. Check the spelling of the
file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, m
ake sure that the file has not been renamed, moved, or deleted.
HRESULT error code:0x80020009
Exception occurred.
from TestTP_UK.rb:19:in `'
enter code here'
Generic path code
excel = WIN32OLE::new("excel.Application")
path = "#{File.dirname(__FILE__)}/../../../MasterFile.xls"
workbook = excel.Workbooks.Open(path)
worksheet = workbook.WorkSheets(1) # Get first workbook
site = worksheet.Range('A2').Value # Get the value at cell in worksheet.
workbook.Close
excel.Quit
Any Ideas
I believe you need to use an absolute path rather than a relative path when opening the file:
path = File.expand_path("../../../../MasterFile.xls", __FILE__)
Note that you will also need an additional '..' when using expand_path, since the first '..' is going back from the file.

Client-Side VBScript application, Incorrect Current Working Directory

I'm not understanding this behavior. Maybe someone can explain to me why my current working directory is not what I expect.
On my desktop, I have a folder called STKGui:
C:\Documents and Settings\Lauren\Desktop\STKGui
Located in that directory are the following files: gui.html, style.css, save.html, load.html Within STKGui there are also the following directories: Images, Scripts, and SaveData. Scripts contains various .vbs files, including gui.vbs.
I start with gui.html. I click a button which takes me to load.html. load.html uses scripts from Scripts\gui.vbs. One of the functions loads a database, and to do so I provide the location of the database: C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb Of course I want to use a relative file path instead of a fixed path. My initial attempt to load the database failed; it was trying to load from C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb. So to troubleshoot I printed out the current working directory; much to my chagrin it was C:\Documents and Settings\Lauren\Desktop
I don't understand why my desktop is my current working directory. Shouldn't it be where the file is running from? I figured it would be either C:\Documents and Settings\Lauren\Desktop\STKGui (the location of load.html) OR C:\Documents and Settings\Lauren\Desktop\STKGui\Scripts (the location of gui.vbs which contains the function that's trying to load the database/printing debug messages of the current working directory).
Can someone explain why the current working directory is what it is, or better yet tell me how to get what I really want, which is the location of the files executing? (I don't care if it's the main STKGui folder or the scripts folder--as long as it's within the application's directory structure I can work with it!)
EDIT (7/14/10 4:02 pm EDT):
Various attempts at printing the current working directory or grabbing files based on what I -thought- was the relative path from my executing script have resulted in my desktop's path instead of the path of the executed script. I stumbled across this link: http://leereid.wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ but none of the solutions are working for me, as I get run-time errors regarding the Wscript object. So while I don't know if any of the solutions on the aforementioned link will produce different results, if someone can help me get at least one of them working so I can find out that may be a step in the right direction.
One of the solutions, reproduced below:
Set oShell = CreateObject("WScript.Shell")
Set ofso = CreateObject("Scripting.FileSystemObject")
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
produces the following error:
Object required: 'Wscript' line: 659 char: 1
with line 659 being:
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
For Server-Side:
You should be using Server.MapPath() to get your "working directory". For instance, if you want to get the path to your database file in C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb, your app root being C:\Documents and Settings\Lauren\Desktop\STKGui, you would use Server.MapPath("SaveData\SaveData.accdb").
For Client-Side:
Upon closer examination and digging up some memories, I realized that MapPath is only available from the Server class. Instead, you need to create a file system object like this:
''get fs object
Set objFSO = CreateObject("Scripting.FileSystemObject")
''get actual file using path relative to calling vbs file
Set objFile = objFSO.GetFile("SaveData\SaveData.accdb")
''get path to the database
set sPathToDatabase = objFSO.GetAbsolutePathName(objFile)
In case it helps, here is a great resource for working with the file system in vbScript: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/
This solution was NOT ideal, but what I ended up doing was parsing the url in my browser to get the directory.
guiPath = Mid(location.PathName, 2, len(location.PathName))
Set regExp = New RegExp
regExp.IgnoreCase = False
regExp.Global = True
regExp.Pattern = ".*/"
Set matchCollection = regExp.Execute(guiPath)
Set match = matchCollection(0)
guiPath = match.value
regExp.Pattern = "%20"
guiPath = regExp.Replace(guiPath, " ")
systemsDBPath = guiPath & "SaveData\SaveData.accdb"
Like I said, less than ideal. May not even work once I'm working with the application this will be running in. But I couldn't find a better way.

Resources