0

trying to copy files with four or less characters in the file name with a certain extension to a new folder. I tried to do this for several hours now! getting frustrated. I used the copy command and the wildcard ? and *

1 Answers1

1

As AFH pointed out in comments, a simple "COPY ????.ext" will work on file names of four or less chars. In glob-style pattern matching in DOS-based systems, * means "0 or more chars" and apparently ? means zero or one chars. However, be sure to understand that this type of pattern matching is done at the application level--that is, Windows (and DOS, for that matter), pass the command line exactly as typed to the program to run. If you type "????.ext", that is exactly what the COPY command sees.

Contrast this to other shells I've worked with, like in un*x (bash, tcsh, etc), where the command interpreter first expands all the pattern matches and passes the full file list to the command. That is, in those shells, if you type "copy file.?", the shell calls "copy file.1 file.2 file.3", "copy" normally never sees the literal "file.?" argument.

(And this is why my original first answer was incorrect--I tested it with a command that was not expanding the way Windows' CMD.EXE would have.. Just something to be aware of!)

There is no way in normal Windows console/command line (which is not DOS, by the way!) to do this with a single command because of the way ? and * work in Windows: ? matches a single character, and * matches as many as possible. Neither let you specify a count. But using a batch file to run each command in sequence, you can achieve the same effect:

@echo off
copy "????.ext" "target folder"
copy "???.ext" "target folder"
copy "??.ext" "target folder"
copy "?.ext" "target folder"

Using various batch file techniques, you could also modify it to work for other file name lengths, or use different target folders, et cetera.

Also, with modern Windows OSes, you most likely have VBScript available via Windows Script Host, and with Win8, you should have PowerShell, both of which allow much more extensive programming logic, such as powerful regular expressions and so on.

C. M.
  • 777
  • 1
    ????.ext matches four or fewer characters in the name, so you do not need more than the first copy line. – AFH Jul 19 '14 at 15:12
  • 1
    If you use all four copy lines, then files with single-character names will be copied four times, those with two characters three times, and those with three characters twice. – AFH Jul 19 '14 at 15:40
  • @AFH: I just tested it and you are correct. I will have to find my old batch file library to refresh my memory of how I did it years ago and update my answer. Thank you for pointing this out! – C. M. Jul 19 '14 at 22:38