In MATLAB, actually Octave, I would like to find a list of all subfolders in the current folder so I use this:
subFolder = dir;
This gives the list of all subfolders in the current folder. This returns a structure whose one element is the name. Assume I have two subfolders with names subfolder 1A and subfolder 1B.
Now I want to go to these folders. Then I do this:
cd subFolder(1).name
But I get this error:
error: subFolder(1).name: No such file or directory
If I do this:
cd "subfolder 1A"
everything works fine. What is the solution?
The space in the folder name is a red herring in this case. It's not the source of the problem. The actual issue is that you need to call the cd function using function syntax instead of command syntax (i.e. use parentheses; related question here):
cd(subFolder(1).name);
When you use the command syntax, subFolder(1).name is itself being treated as the string argument to cd (i.e. it's looking for a folder called 'subFolder(1).name'). With the function syntax, the string contained within the structure array field is used as the argument.
To make your code a little more robust, you could also use the 'folder' field returned by dir:
cd(fullfile(subFolder(1).folder, subFolder(1).name));
This will go to the desired folder regardless of the directory you are currently in, since it specifies an absolute path instead of a partial path (which is relative to the current directory).
Related
I have a directory that contains few sub directories. In each sub directory there are few files, and I'd like to get the full paths of the files that contain the word "raw" in their filename and that ends with ".txt" (in each directory there is a file with "raw" in it's name).
Anyone knows how to do it elegantly with python 3?
We can use the Path.resolve method to get the absolute path to the directory, then use Path.glob to collect the Path objects representing the paths of the files we want to retrieve.
from pathlib import Path
paths = [path for path in Path('path/to/dir').resolve().glob("**/*raw*.txt")]
Here your search is specified by **/*raw*.txt. This will return some number of Path objects (the exact type will depend on the operating system you use).
If you want them as strings, you can substitute [str(path) for path in ..] above
How to get files starting specific name?
I have script like this
open Area_4#10.54.18.18
get -neweronly "/Payload/Daily/2017/Daily_Payload_BH_2*.csv" "D:\FTP-NSQM\2_Payload"
get -neweronly "/Payload/Daily/2017/Daily_Payload_BH_3*.csv" "D:\FTP-NSQM\3_Payload"
I used batch file for execute this script, but nothing happened
If i don't use a specific name (Daily_Payload_BH_2*.csv or Daily_Payload_BH_3*.csv) for get a new files
get -neweronly "/Payload/Daily/2017" "D:\FTP-NSQM\2_Payload"
This script executed properly
Your code is ambiguous.
To get comparable results use:
get -neweronly "/Payload/Daily/2017/Daily_Payload_BH_2*.csv" "D:\FTP-NSQM\2_Payload\"
get -neweronly "/Payload/Daily/2017/Daily_Payload_BH_3*.csv" "D:\FTP-NSQM\3_Payload\"
and
get -neweronly "/Payload/Daily/2017/*" "D:\FTP-NSQM\2_Payload\"
These should get you consistent results.
Note the /* in source paths and the trailing backslash in target paths.
Excerpts from get command documentation (emphasis mine):
Filename can be replaced with wildcard to select multiple files. To download all files in a directory, use mask *.
The last parameter specifies target local directory and optionally operation mask to store file(s) under different name. Destination directory must end with backslash.
If this does not help, we need to see complete log files of both scripts.
I have a folder which has another folder inside it (lets say test and insidetest-some random number). Now what I am trying to do is to copy the content of the insidetest-... into another folder. The problem is that I know half of the name of the folder which in test folder and I do not know the the randon number attached to it. (Just for more explanation I get the a zip file from bitbucket api and then after unzip it it has this structure. So I can never know the exact name of the folder inside test. If I knew that I could simply use sth like this:
cp home/test/* /home/myfolder/
But I cannot do it in this situation. Can anyone help?
If some part of the name is constant then use the command like this:-
cp home/test/halfname* /home/folder/ -r
I don't want to specify the full directory of a folder or object within my program. I do not want to do this because if a user decides to change the installation folder, it will not function properly. I've seen in HTML you can do something like: ./folder/directory/name and it would work perfectly fine. Is there a way to do something like that within Python?
From https://docs.python.org/3/reference/datamodel.html
__file__ is the pathname of the file from which the module was loaded
You may find it helpful to apply os.path.abspath() to '.' or __file__.
I'm looking to determine if a file system path is directory or file. I'm not looking to checking for the type an existing path. I'm trying to determine if the path function argument string is a referring to a directory or file.
How do I make a distinction between a file and a directory when this:
/Users/thomas/Desktop/node
The following path could refer to a directory node, or a file node without an extension.
I was thinking about using a trailing / to connote directory.
So this would mean a directory:
/Users/thomas/Desktop/node/
And this would mean a file:
/Users/thomas/Desktop/node
However node's path methods like .resolve() and .join() do not take into consideration the trailing / and always remove it. So is this good practice?
There is no way to check if an arbitrary string is a directory or file if it does not exist.
However for existing paths, you can use fs.stat() on the path, which will give you an object that has methods for checking the path type (e.g. isDirectory(), isFile(), etc).