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

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.

Related

How to test if a folder can be deleted

I am writing a Lua function to delete a folder using Luacom in Windows (version 7 onwards and I can't dictate the version). The folder path is specified in UTF-8 and will contain non-ASCII characters, so os.remove, io.whatever, Penlight and lfs will not work. So far I have (using Luacom to access the Windows com model):
function delFolder(sPath, bForce)
--sPath is a fully specified folder path
--bForce is a Boolean indicating whether the folder should be
--deleted even if it contains read-only files
require('luacom')
fso = luacom.CreateObject("Scripting.FileSystemObject")
--code in here to test that the folder exists,
--and return an error if it does not
fso:DeleteFolder(sPath, bForce)
end
My problem is that, if bForce = false, and the folder is effectively read-only, the operation errors out. I need to be able to test for this situation and return an error instead of attempting the operation.
One possibility is to manipulate the Luacom error handling not to abort on error, and test the last error after the operation:
luacom.config.abort_on_error = false
luacom.config.last_error = nil
fso:DeleteFolder(sPath, bForce)
if luacom.config.last_error then
--return error indicating that the folder cannot be deleted
end
but is there a simpler way, using the com model or some other alternative available in Lua?
Reference for FileSystemObject

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

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)

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".

How do I open multiple Excel files using Ruby script?

I am working on writing a ruby script to iterate through a file containing a list of file paths to open in Microsoft Excel. I read the file like this:
file_names = IO.readlines('D:\TEST_1\file_names.txt')
Next, I create an array of file names from each line of the parsed file (thus containing an array of file paths). Finally, I loop through that array with the following code, to open the documents:
require 'win32ole'
xl = WIN32OLE.new('Excel.Application')
xl.Visible = 1
file_names.each do |file_name|
wb1=xl.Workbooks.Open(file_name)
ws1=wb1.worksheets(1)
end
That first call to parse file_names.txt produces this exception, which I am having difficulty understanding:
Test4.rb:6:in 'method_missing'
OLE error code:800A03EC in Microsoft Office
Excel 'D:\Test_1\1.xlsx' couldnot 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 most recently used
files, make sure that the file has not been renamed, moved or deleted
HR Error code : 0x80020009 Exception occurred. from Test4.rb:6:in
'block in ' from Test4.rb:5:in 'each' from Test4.rb:5:in
''
This error does not appear when I pass a single file name (instead of a file path) as my parameter - so why do I get it here? Any help would be much appreciated.
At first look you are not using the variable "file_name" but a symbol :file_name.
file_array.each do |file_name|
wb1=xl.Workbooks.Open(file_name)
ws1=wb1.worksheets(1)
end

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