Why does cmd DIR search include files that start with the 3 searched letters but then differ? - cmd

If in a directory I have two files, test.pro and test.properties, and I run dir /s *.pro to find all files with the .pro extension, it lists both files. However, when I run dir /s *.pr, it lists neither. Why does searching for a three-letter file extension list files with extensions that start with the three letters? How can I search for ONLY .pro files using dir?

The command DIR lets the file system search for file system entries (file names, directory names, reparse points (links)) matching the wildcard pattern *.pro in long name or in short 8.3 name on short name management also enabled for the file system of current drive.
The short file name of test.pro is TEST.PRO.
The short file name of test.properties is TEST~1.PRO.
Therefore the wildcard pattern *.pro matches test.pro and TEST~1.PRO displayed with long file name test.properties.
The wildcard pattern *.pr does not match with the two file names because of the file extension is pro respectively properties and not just pr. The wildcard pattern *.pr? would also find the file names test.pro and TEST~1.PRO displayed with long file name test.properties.
There can be used %SystemRoot%\System32\where.exe /R . *.pro to find recursively in current directory and all its subdirectories files with file extension .pro not matching file names with a longer file extension beginning with pro because of WHERE applies the wildcard pattern only on long file names.
DIR and also FOR use the Windows file I/O function which directly searches with the wildcard pattern *.pro for suitable file system entries. WHERE uses the Windows file I/O function to search for * to get a list of long names of all file system entries and then applies the wildcard pattern itself on each string returned by the file system. For that reason the usage of the wildcard pattern *.pro returns a positive result only on test.pro and not on test.properties on using WHERE.

Related

Batch File to copy files from one subdirectory to another subdirectory that contains a "_" character

I've been struggling to get this script working and have been doing a lot of searching but can't seem to figure out how to do.
I have a specific directory and this directory contains two additional subdirectories, "User Data" and "a_bcdefg". The first subdirectory name is static but the 2nd subdirectory name is always random characters but always contains a single character at the start then an underscore "_". I am trying to copy all the files from "User Data" into "a_bcdefg" without having to specify the directory since the name is always different.
I've been trying various iterations of the following but can't get it to work:
xcopy /e /c "C:\temp\test\User Data\*.*" "C:\temp\test\*_*"

*.* as pattern for folders and files

In batch file scripts and the doc pages (e.g. here) I often see *.* as (I guess) a way to specify multiple folder/ file names. My question is: How exactly this string *.* is interpreted by cmd.exe?
I know that specify folder/ file names two special characters can be used:
* means any number of character (including zero)
? one character
So *.txt would mean all files with extension .txt in the current directory. In light of this, I would read *.* as any folder/file name that has . (dot) in it.
Why then when I run DIR *.* in a folder that has only a subfolder named folder and a file named script.txt, it displays folder and script.txt instead of just showing script.txt?

What is the longest directory and file name?

I need to optimize my Java application that works with directories and files. I use String[] buffer; to keep file or dir name. So what's the longest possible file or dir name allowed in Windows 8.1?
Filename with path to the file is limited to 260 characters.

Recursively copy file-types from directory tree

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.

Find all folders excluding some paths

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.

Resources