I wrote the below condition to get all *.js files from a folder and its subfolder. I wrote the below command to get the list:
c:/svn/myfolder/**/*.js
"myfolder" has 4 sub folders a, b, c, d in it and i want to get list of .js files only from three of the subfolders and skip checking the subfolder 'c'.
Currently it returns all .js files as i put myfolder/**/*.js
Ruby's glob syntax borrows from regex. You can specify a range of characters using brackets.
Dir['c:/svn/myfolder/[abd]/*.js']
This will include a, b, and d but not c. You can also negate:
Dir['c:/svn/myfolder/[^c]/*.js']
This will include all but c
See http://ruby-doc.org/core-2.1.3/Dir.html#method-c-glob for more info on Ruby globs.
You can use Dir glob to get all the files and directories as a list. To accomplish this with regex do:
Dir.glob(File.join(['c:/svn/myfolder', '*[^excluded_folder]', '**', '*.js']))
The *[^excluded_folder] includes everything but excluded_folder.
Keeping the ** as part of your path will grab files from the current directory path it's placed at as well as all subdirectories.
Related
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.
I face the following issue in Windows. I have thousands of subfolders in a main folder (let's call it data\ ) and I want to move a big subset of them in another folder (let's call it data2\ ). In a .txt file I have a list of the exact names of the folders that I want to move.
Is there a way to do that with the cmd?
I make a simple example to make the issue more clear. In the folder data\ I have the subfolders A, B, C, D and E. In a text file I have the list of names:
A C E (one name in each line)
I want to move the subfolders A, C and E to the folder data2.
Thanks a lot :)
I suggest using PowerShell instead of plain cmd. You can take advantage of various Linux command aliases. To move the folder data\A to data2\A it is as easy as mv data\A data2. Now you also need to do this as part of a loop, because you have a text file containing the names of all these folders that you would like to move. I am not on a windows system right now, so I can't test the below code. I adapted a for loop that I saw here
$files = cat list.txt
foreach ($f in $files)
{ mv data\$f data2 }
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.
I have a bunch of files I'm trying to organize quickly, and I had two questions about how to do that. I really appreciate any help! I tried searching but couldn't find anything on these specific commands for OSX.
First, I have about 100 folders in a directory - I'd like to place an folder in each one of those folders.
For example, I have
Cars/Mercedes/<br>
Cars/BMW/<br>
Cars/Audi/<br>
Cars/Jeep/<br>
Cars/Tesla/
Is there a way I can create a folder inside each of those named "Pricing" in one command, i.e. ->
Cars/Mercedes/Pricing <br>
Cars/BMW/Pricing<br>
Cars/Audi/Pricing<br>
Cars/Jeep/Pricing<br>
Cars/Tesla/Pricing
My second question is a little tougher to explain. In each of these folders, I'd like move certain files into these newly created folders (above) in the subdirectory.
Each file has a slightly different filename but contains the same string of letters - for example, in each of the above folders, I might have
Cars/Mercedes/payment123.html
Cars/BMW/payment432.html
Cars/Audi/payment999.html
Cars/Jeep/payment283.html
Is there a way to search each subdirectory for a file containing the string "payment" and move that file into a subfolder in that subdirecotry - i.e. into the hypothetical "Pricing" folders we just created above with one command for all the subdirectories in Cars?
Thanks so much~! help with either of these would be invaluable.
I will assume you are using bash, since it is the default shell in OS X. One way to do this uses a for loop over each directory to create the subdirectory and move the file. Wildcards are used to find all of the directories and the file.
for DIR in Cars/*/ ; do
mkdir "${DIR}Pricing"
mv "${DIR}payment*.html" "${DIR}Pricing/"
done
The first line finds every directory in Cars, and then runs the loop once for each, replacing ${DIR} with the current directory. The second line creates the subdirectory using the substitution. Note the double quotes, which are necessary only if the path could contain spaces. The third line moves any file in the directory whose name starts with "payment" and ends with ".html" to the subdirectory. If you have multiple files which match this, they will all be moved. The fourth line simply marks the end of the loop.
If you are typing this directly into the command line, you can combine it into a single line:
for DIR in Cars/*/ ; do mkdir "${DIR}Pricing"; mv "${DIR}payment*.html" "${DIR}Pricing/"; done
In visual studio when searching for files, how can I find all files that do not contain a certain string in their directory path or file name?
For example:
I want to find all files that have the word MainRegion but I do not want files such as:
c:\myfiles\file1Fixture.cs
c:\myfiles\somedirectory\a.b.tests\filename.xaml
So I want to exclude file names with "Fixture" in and directory paths with "tests" in.
JD
^.*\\[^\\]*Fixture[^\\]*$
should match files that contain Fixture in the file name (but not in the path).
^.*tests.*\\[^\\]*$
should match files that contain tests in the path, but not in the filename.