I want to copy multiple directories from one location to another location only if any of the subdirectories of those contain connect.txt file in them.
Example:
ANIMAL\DOG\CONNECT.TXT
PLANET\EARTH\CONNECT.TXT
SYSTEM\USER\ADMIN.TXT
Then I ONLY want to copy ANIMAL & PLANET directories to C:\DESKTOP.
move *\*\connect.txt C:\Desktop
This uses a regular expression which would work if you're really wanting to look only under the subdirectories of all directories at some location.
How to check if the file exists in all subdirectories within chef recipe (not_if guard).
There is no specific answer other than "write some Ruby code to check what you want". You'll probably want to do a Dir glob and then compare the various outputs but maybe you just need an all? and File.exist?.
I'm trying to find a way to copy all *.exe files (and more, *.dtd, *.obj, etc.) from a directory structure to another path.
For example I might have:
Code
\classdirA
\bin
\classA.exe
\classdirB
\bin
\classB.exe
\classdirC
\bin
\classC.exe
\classdirD
\bin
\classD.exe
And I want to copy all *.exe files into a single directory, say c:\bins
What would be the best way to do this?
Constraints for my system are:
Windows
Can be Perl, Ruby, or .cmd
Anyone know what I should be looking at here?
Just do in Ruby, using method Dir::glob :
# this will give you all the ".exe" files recursively from the directory "Code".
Dir.glob("c:/Code/**/*.exe")
** - Match all directories recursively. This is used to descend into the directory tree and find all files in sub-directories of the current directory, rather than just files in the current directory. This wildcard is explored in the example code.
* - Match zero or more characters. A glob consisting of only the asterisk and no other characters or wildcards will match all files in the current directory. The asterisk is usually combined with a file extension, if not more characters to narrow down the search.
Nice blog Using Glob with Directories.
Now to copy the files to your required directory, you need to look into the method, FileUtils.cp_r :
require 'fileutils'
FileUtils.cp_r Dir.glob("c:/Code/**/*.exe"), "c:\\bins"
I just have tested, that FileUtils.cp method will also work, in this case :
require 'fileutils'
FileUtils.cp Dir.glob("c:/Code/**/*.exe"), "c:\\bins"
My preference here is to use ::cp method. Because Dir::glob is actually collecting all the files having .exe extensions recursively, and return them as an array. Now cp method is enough here, now just taking each file from the array and coping it to the target file.
Why I am not liking in such a situation, the method ::cp_r ?
Okay, let me explain it here also. As the method name suggests, it will copy all the files recursively from the source to target directory. If there is a need to copy specific files recursively, then ::cp_r wouldn't be able to do this by its own power ( as it can't do selections by itself, which ::glob can do ). Thus in such a situation, you have to give it the specific file lists, it would then copy then to the target directory. If this is the only task, I have to do, then I think we should go with ::cp, rather than ::cp_r.
Hope my explanation helps.
From cmd command line
for /r "c:\code" %f in (*.exe) do copy "%~ff" "c:\bins"
For usage inside a batch file, double the percent signs (%% instead of %)
Windows shell (cmd) command:
for /r code %q in (*.exe) do copy "%q" c:\bin
Double the % characters if you place this in a batch file.
Can someone explain me exactly (better if with examples) the meaning of the dereference_root option in FileUtils.cp_r and in other class method of the same class?
Thank you in advance.
It applies only if your source file/directory is symbolic link. If it is and you specify this option, then FileUtils.cp_r will follow this link and copy the original files otherwise you will get just a copy of the symlink.
But this doesn't apply recursively. So if you will have other symlinks inside your source folder they will be just copied as is, without 'dereferencing'.
How can I write glob that will match files only if one-level subfolders? I have a structure like that: src/items/item-name/file.ext
And I need a glob that will match only these files, not src/items/item-name/subfolder/file.ext. I tried to use src/blocks/*/*.*, but it still looks into subfolders.
I'm trying to set up watch action for Compass, and I have to do it with ruby. I'm not familiar with it, and can't find a way to do that myself.
In Ruby, you can catch all the files following the pattern *.* in all subfolders of src/items as following:
Dir.glob('src/items/*/*.*').select({ |f| File.file?(f) }).each do|file|
puts file
end
It doesn't dig deeper (if you don't use ** it doesn't do it recursively ), and it only considers file thanks to File.file? (in case of a subsubfolder name would match the pattern *.*).