Recursively find folder names only (not files) - ruby

Is it possible to display the folder names (only) recursively. I know, to display the files from the specific folder using the following command.
Dir.glob("/home/test/**/*.pdf")
or
Dir['/home/test/**/*.*']
But, i need to display folder name only.

you put a slash, like this
Dir["**/"].each {|x| puts x}

Related

How to be able to find folders whose name keeps changing in shell/bash?

So I'm looking for a way for my shell to find the folder with the code behind the folder name that keeps changing, in the data/app/ folder, for example the folder "com.tencent.ig-yHGLSvh42dYO-GNMFS9WxA==". Which always changes only in the part after "com.tencent.ig-", the second "-" and "==". Full path "data/app/com.tencent.ig-yHGLSvh42dYO-GNMFS9WxA==/"
Code that can find the location of a folder so I can change the contents of that folder. If possible the code is contained in a variable so I can use it like so:
findfolder = (Code)؜
؜printf $findfolder

How to check the content of each .txt file in a folder with Ruby

I have a folder that contains files. I was wondering how I can chech every .txt file in the folder if it contains the word "BREAK". I know it must be very easy but I kinda miss the way of getting it done.
This is what I've tried so far
Dir.glob('/path/to/dir/*.txt') do |txt_file|
# And here I need a method that opens the 'txt_file'
# and checks if it contains "BREAK"
end
The below would return an array of files containing "BREAK"
files = Dir.glob('/path/to/dir/*.txt').select do |txt_file|
File.read(txt_file).include? "BREAK"
end

ruby return section of directory name

I have a directory, which contains a series of folders, which are of the pattern YYYY-MM-DD_NUMBER . If I am navigating through one of these folders using Dir, how can I return part of the folder name that contains YYYY-MM-DD ?
For example, 2013-05-23_160332 would be a name of a folder. And it would be apart of a larger directory, called main_dir. I use Dir to get access to some file names and store them into an array, like so:
array = Dir["/main_dir/**/data/*.csv"]
I then iterate through the array and print the files. How can I also return/print the part of the title directory that I am currently accessing with each iteration (again, in the form of YYYY-MM-DD)?
I might do something like this.
re = Regexp.new('\d{4}-\d{2}-\d{2}')
array.each do |folder|
puts folder[re]
# folder.each or other processing ...
end

How to read images from folders in matlab

I have six folders like this >> Images
and each folder contains some images. I know how to read images in matlab BUT my question is how I can traverse through these folders and read images in abc.m file (this file is shown in this image)
So basically you want to read images in different folders without putting all of the images into one folder and using imread()? Because you could just copy all of the images (and name them in a way that lets you know which folder they came from) into a your MATLAB working directory and then load them that way.
Use the cd command to change directories (like in *nix) and then load/read the images as you traverse through each folder. You might need absolute path names.
The easiest way is certainly a right clic on the forlder in matlab and "Add to Path" >> "Selected Folders and Subfolders"
Then you can just get images with imread without specifying the path.
if you know the path to the image containing directory, you can use dir on it to list all the files (and directories) in it. Filter the files with the image extension you want and voila, you have an array with all the images in the directory you specified:
dirname = 'images';
ext = '.jpg';
sDir= dir( fullfile(dirname ,['*' ext]) );;
sDir([sDir.isdir])=[]; % remove directories
% following is obsolete because wildcarded dir ^^
b=arrayfun(#(x) strcmpi(x.name(end-length(ext)+1:end),ext),sDir); % filter on extension
sFiles = sDir(b);
You probably want to prefix the name of each file with the directory before using:
sFileName(ii) = fullfile(dirname, sFiles(ii));
You can process this resulting files as you want. Loading all the files for example:
for ii=1:numel(sFiles)
data{i}=imread(sFiles(ii).name)
end
If you also want to recurse the subdirectories, I suggest you take a look at:
How to get all files under a specific directory in MATLAB?
or other solutions on the FEX:
http://www.mathworks.com/matlabcentral/fileexchange/8682-dirr-find-files-recursively-filtering-name-date-or-bytes
http://www.mathworks.com/matlabcentral/fileexchange/15505-recursive-dir
EDIT: added Amro's suggestion of wildcarding the dir call

How to automatically find files of a specified type in the current directory or any specified sub-folders in Ruby?

I am using the following code to convert files from php to html. In order for it to work, I have to enter the name of each file on the second line.
p "convert files"
%w(file1 file2 file3).each do |name|
system %(php #{DIR}/#{name}.php > #{DIR2}/#{name}.htm)
end
Can someone tell me how to make it so it will automatically find any .php files in the main directory and look in any defined folder and their sub-folders for additional .php and save them in similar folder names?
For example:
file1.php -> file1.htm
about-us/file2.php -> about-us/file2.htm
contact-us/department/file3.php -> contact-us/department/file3.htm
The easiest way is to use Dir:
Dir.chdir('where_the_php_files_area')
Dir['**/*.php'].each do |php|
htm = 'where_the_html_files_should_go/' + php.sub(/\.php$/, '.htm')
system("php #{php} > #{htm}")
end
The ** pattern for Dir.glob (AKA Dir[]) matches directories recursively so Dir[**/*.php] will give you all the PHP files under the current directory.

Resources