Refer to a directory in your working directory - macos

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.

Related

Stata can't open file saved in local cloud storage folder on Mac

I am using Stata on a Mac. I updated the OS to Monterey today. If I try to open a .dta file---that is saved on my local computer in a folder that syncs to OneDrive---from code in the Do File Editor/Command prompt (where I refer to the file using its full file path), it doesn't open. The error is r(601) - File ... cannot be found.
But if I click on it in Finder, it opens in Stata just fine. After I have done this, I can then open this file in Stata by running the exact same Stata code that didn't work before. Somehow it seems like Stata can't see the file/folder initially, but then it can. I am not sure if this has to do with the OneDrive file/folder permissions or something else.
If, in Stata, I change the directory to the correct directory using cd and then load the file (without referring to the path), it works. However, I would like to be able to refer to the file using the entire file path and not have to change the directory each time.
Interestingly, if I change the directory to the correct directory using Change working directory in the File drop down and then try to load the file using the file path, it also works. But if I do not do the drop down and write out the cd using code, and try accessing the file using the path it doesn't work!
Somehow Stata has to initially "see"/"access" the correct folder (in a very particular way) before being able to access it with the file path. Very strange. (Just to be clear, if I do not change the directory to the correct directory and then try to load the file using its file path, it doesn't work.)
I also tried creating a file using texdoc init... (in a folder that already exists, but that has not been used by Stata before) and get this error:
could not create directory .....
mkdir(): 693 could not create directory
texdoc_mkdir(): - function returned error
texdoc_init(): - function returned error
<istmt>: - function returned error
I do not get this error if I instead use another folder in the same directory that I have previously used.
I think that the error I am encountering is the same or similar to this one (with no acceptable answer): https://answers.microsoft.com/en-us/msoffice/forum/all/onedrive-folder-doesnt-exist/e6b97d47-3f6a-4863-bf68-d3a02832f2bb

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!

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.

Relative file paths

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.

Resources