I'm using 'copy' in a console window in Windows XP Pro (So I'm using DOS commands basically)

I have files with three characters plus an extension and files with two chars plus an extension. I have the following in an ms-dos batch file...

copy ???.gif n:\inetpub\wwwroot\images

which as far as my understanding of windows/dos wildcards is concerned should copy ONLY those files that have three characters in their name. But it's copying the files with two characters also.

My understanding must therefore be wrong, so is there another way in an ms-dos batch file to limit a copy to only files with a specific number of chars in their name?


In order to select only the files you want you need to get regular expressions involved. One way to do this is to use to findstr command. (http://ss64.com/nt/findstr.html)

In order to get the output commands strung together it looks at little ugly but the basic format is as follows:

for /f "delims=" %a in ('dir /b ^| findstr "\<[a-z][a-z][a-z].gif"') do copy %a n:\inetpub\wwwroot\images

To explain a little:

for /f "delims=" %a in

This is the setup so that each piece of output from the command in the '()' will be put in %a

('dir /b ^| findstr "\<[a-z][a-z][a-z].gif"')

This command uses dir /b to get a listing of the files in the current directory and passes that to findstr. findstr looks for input lines which match the following pattern: They contain a word which starts(\<) with three letters ([a-z][a-z][a-z]) followed by with .gif (.txt).

do copy %a n:\inetpub\wwwroot\images

This copies the file who's name is in %a.

| improve this answer | |

the ? means one character or a NULL in the end of the filename.

So BA??.TXT will match BARR.TXT and BAR.TXT. Since you're using 3 ?, it will also match 2 characters and 1 character...

In windows Vista there's a command to copy called robocopy and it have the option to exclude some files. You could try:

robocopy ???.txt c:\temp\ /xf??.TXT

but I'm not sure it will work in windows XP

| improve this answer | |
  • ? even worked in proper DOS. Can you explain /xf??.TXT ? – barlop Mar 23 '15 at 9:54

You can do that this way:

@echo off
setlocal EnableDelayedExpansion
for %%f in (???.gif) do (
    set filename=%%f
    set fourthchar=!filename:~3,1!
    if !fourthchar! == . copy %%f n:\inetpub\wwwroot\images

That is, copy files that have three characters before the dot only.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.