Relative file paths - windows

I am trying to read in from a few files using something like this
IO.foreach("TeamFields.txt") { |line| fieldNames.push(line.chomp) }
It works fine when running the from the command line, but when I package to an .exe with shoes and run it can't find the file. Is there a way to specify a path relative the the .exe or do I have to provide the full filepath (e.g. "c:\files\TeamFields.txt")? Thanks for the help.

This is because your executable is not run with the correct current directory.
Either fix the current directory (for example in the shortcut) or modify your Ruby program to automatically set the working directory to the program directory with:
Dir.chdir(File.dirname($PROGRAM_NAME))

You need to set "Current Application Directory" correctly before going relative.
The user can execute your app with different start up dir, or system can call your app with different dir.
If files in question are in the folder of your app, the only thing you need to do is to get that folder, and set it to be current.

I don't program in ruby, but I do with windows, and odds are the relative path will be based on the location of the .exe file.
So, yes, you're probably better off passing a full path for the file name.

The constant __FILE__ will contain the full path to the currently executing file. You can then use methods of the File class to strip off the filename, append the relative path for whatever other file in your package it is you want and resolve the result.

Related

Refer to a directory in your working directory

I'm working on the Mac Terminal with the program called QIIME. However, my question is more related to basic navigating in the terminal.
When I enter a command, and would like to refer to a file that is located in a directory/map inside my current working directory, how do I do this?
For example:
convert_fastaqual_fastq.py -f sequenceA.fastq
Now sequenceA is located in a map in my working directory, so I guess I'll have to add arguments before sequenceA.qual, or shouldn't I ?
from your working directory you should be able to access that file with:
the_directory_your_file_is_in/sequenceA.qual
else, you could use something like:
./the_directory_your_file_is_in/sequenceA.qual
A point stands for the directory your are currently in.
../the-directory-you-are-in/the_directory_your_file_is_in/sequenceA.qual
Two points stand for the directory above your current directory.

Setting a path in OSX for Matlab

I just shifted from a windows machine to a apple machine at work. I have no experience with apple and this is the first time I am using OSX.
I have a matlab script that I have to run on this machine but I cannot seem to get the path to my files.
My files are on a network drive. In windows its as simple as U:\Matlab Now I can access my files in the explorer but cannot seem to set them in Matlab using cd
To get the path of my files I right clicked on the folder and copied the where It provided me with this:
Volumes/home9/MATLAB/
Now to set these paths in Matlab I did this:
cd('Volumes/home9/MATLAB/')
But am provided with the error:
Cannot CD to Volumes/home9/MATLAB/ (Name is nonexistent or not a directory).
As you can tell I have no idea what I am doing. Some guidance would be appreciated.
Thank you
I think you meant to use which, not where. where doesn't exist in MATLAB! Now, your problem is probably due to the fact that you need to prepend your path with /. Therefore, your path should be: /Volumes/home9/MATLAB/. If you don't include the /, it assumes that the directory is local or where MATLAB has currently defined the working directory to be. Judging from the context, you want the absolute path of the directory, and that's why you need the / character as there is a Volumes directory in your root directory.
Try that and see if that works!

Setting filepaths inside your .rb file when converted into an exe with ocra

I've had a lot of trouble getting one my wxruby scripts to use an image file I included in the exe with ocra. If I didn't have the original image file in the same directory as the exe, the exe wouldn't find the image.
shape = File.join('warning3.png' )
I wanted the script to find the image I included in the exe.
In the ocra documentation it mentions Dir.chdir File.dirname($0) but I didn't get what it meant when it was mentioned.
If you need to use say an image file in your script you can make sure it works merely by having this Dir.chdir(File.dirname($0)) line before you try to use any of your images.
For example in my app I'm using an image file and I couldn't get my exe to work if it wasn't in the same dir as the file but with the below it works anywhere so far.
Dir.chdir(File.dirname($0))
shape = File.join('warning3.png' )
I think this sets the script's current directory to the exe's directory inside it? If I'm wrong please let me know!

How can I get my Cocoa command line tool to know the working path that it was called from in the terminal?

The goal of my cocoa command line program is to take an argument which is the name of a file and import it into the program for parsing. I want to be able to call my program lets say, "ParseFile" from the terminal in mac os, with the argument "Filename.txt"(example$ ParseFile filename.txt), but so far I cant understand how my program can know the absolute path to Filename.txt.
For instance when you use 'cp filename.txt /whateverfolder/filename2.txt' you are copying the file 'filename.txt' from the current working directory of the terminal to a folder called whateverfolder on the root directory. How does cp know the absolute path of filename.txt? if i had filename.txt on my desktop cp would know that i sent it /Users/username/Desktop/filename.txt. This is what I want my program to know.
I have been able to get my program to know where it is executed in, but this is very different from where the current working directory of the bash terminal was at the time of being called.
How do i solve this? Thanks a lot
Use NSFileManager's currentDirectoryPath method:
NSString *currentpath = [[NSFileManager defaultManager] currentDirectoryPath];
How does cp know the absolute path of filename.txt?
It does not know it and doesn't have to. It can just use the relative path. The current working directory is inherited from the parent process.
I have been able to get my program to know where it is executed in, but this is very different from where the current working directory of the bash terminal was at the time of being called.
What do you mean? The directory where the executable is in? That's usually not of much interest. The only other directory that plays a role in this context is the current working directory. If you already got that, what else do you need?

cocoa -- determine directory from which a tool was launched?

I have a command-line tool written in Cocoa. Let's call it processFile. So if I am in the terminal and I type in the command ./processFile foo, it looks for a file named foo.html in the same directory as the executable of processFile. If it finds one, it reads it and does some stuff to create fooProcessed.html.
Now I want to modify my tool so that it looks for foo.html in the directory from which it was launched. So if I am in the terminal with current directory ~/documents/html, and processFile executable is in usr/bin, and I type in
processFile foo
it will find and process the file ~/documents/foo.html.
The problem is that I don't know how to get the directory from which the tool was invoked. How can I do that?
That's the current working directory. First of all, any attempt to access the file just using its name and no path will automatically use the working directory. So, if you simply take "foo", append ".html", and attempt to open the file, that will work. If the user specified a relative path, like "subdir/foo", that would also work. It would resolve the relative path starting from the current working directory.
You can also query the working directory using the getcwd() routine.

Resources