Copy file names with a certain number of characters in command line (CMD)

5

2

I need to copy all text files on one folder whose names are five characters long. I know this command is for listing:

$ dir folder /B | findstr /R "^.....\.txt"

but I want to copy all files whose names are listed by the above command to a different folder.

Stefano Manolo Costantini Ragn

Posted 2015-10-11T20:31:25.490

Reputation: 51

Answers

5

I was asking (not only to myself) "doesn't it work a simple..."?

copy  C:\ORIG_DIR\?????.txt C:\Dest_Dir

Thanks to G-Man it is tested that in powershell it works, meanwhile in cmd.exe the above command will copy each match up to five characters and with all the extension that start with .txt.

Question mark (?)

Use the question mark as a substitute for a single character in a name. For example, if you type gloss?.doc, you will locate the file Glossy.doc or Gloss1.doc but not Glossary.doc.

Reference:

Hastur

Posted 2015-10-11T20:31:25.490

Reputation: 15 043

(1) D’Oh!  Brain freeze.  I took one look at the question and the first two answers, and I forgot about ?.  But: (2) Did you try it?  On my machine, ?????.txt matches files whose names are *up to* five characters long, including A.txt, be.txt, sea.txt, deep.txt, and water.txt.  (3) Also, Windows wildcards have a bug: *.txt matches what *.txt* matches — including cat.txt1 and dog.txtfoo.  It’s a problem of matching against both the short and the long name — see this.  And so, in fact, ?????.txt will match cat.txt1 and dog.txtfoo.

– G-Man Says 'Reinstate Monica' – 2015-10-12T08:35:59.850

@G-Man (1) Thanks, your way of answer made me smile, really thank you. (2) Of course not, I had no possibility to do it (else I didn't put the ? in the answer). But: (3) From table 13.7 of powershell I read "exactly one of any characters", maybe it works so under powershell and/or with something like [a-Z]????.txt under a cmd.exe. What do you think?

– Hastur – 2015-10-12T08:57:28.897

Good catch!  In PowerShell, ?????.txt works correctly — it doesn’t match the shorter-than-five names or the *.txt* names. – G-Man Says 'Reinstate Monica' – 2015-10-12T17:58:42.500

@G-Man. Thanks for the feedback; answer updated. I liked your approach too. – Hastur – 2015-10-12T20:26:28.083

dbenham wrote a comprehensive (and highly up-voted) answer to How does the Windows RENAME command interpret wildcards? (on Super User); it says, “? - Matches any 0 or 1 character except .”. – G-Man Says 'Reinstate Monica' – 2015-10-12T23:25:32.177

2

This is possible in Batch but is easy in PowerShell.

ls | foreach { if (($_.BaseName.Length -eq 5) -and ($_.Extension -eq ".txt")) 
    { $_.CopyTo("\Name\Of\Target\Folder\" + $_.Name) }

You could also do it by checking the file names against a regular expression, but this method works fine.

CBHacking

Posted 2015-10-11T20:31:25.490

Reputation: 5 045

2

In Batch you could do something like this:

@Echo OFF

Set "targetDir=C:\Dir"

For %%# In ("*.txt") DO (

    (Echo "%%~nx#" | findstr /R "^......\.")1>Nul 2>&1 && (

        Echo Copying "%%~nx#" ...
        (Copy /Y "%%~f#" "%targetDir%\%%~nx#")1>NUL

    )
)

Pause&Exit /B 0

ElektroStudios

Posted 2015-10-11T20:31:25.490

Reputation: 1 282

2

To test with echo in one-liner (it should work with subfolder if you add /s in dir command)

for /f "delims=" %a in ('dir /b /a-d "?????.txt"^|findstr /R "\\.....\.txt$"') do @echo %~fa

one-liner copy command

for /f "delims=" %a in (
'dir /b /a-d "?????.txt"^|findstr /R "\\.....\.txt$"'
) do @copy  "%~fa" "destination-dir"

Batch file

for /f "delims=" %%a in (
  'dir /b /a-d "?????.txt"^|findstr /R "\\.....\.txt$"'
) do (
  copy  "%%~fa" "destination-dir"
)

Edit:

/a-d scan files
/ad scan folders
Without /a[d|-d] it will scan both see dir /? for further reading

If I added "?????.txt" in command "dir" is to reduce the scope of scan and thereby reduce the time to scan. You can always expand the scope of the scan in the command "dir" but the most important is the pattern in command "findstr"

I changed "^.....\.txt" to "\\.....\.txt$" to be able to scan folder name.

.   Wildcard: any character
ˆ   Line position: beginning of line
$    Line position: end of line
\.  Escape: literal use of metacharacter .

See findstr /? for further reading

Why it doesn't work in some case? Because there is no universal solution, you have to adapt the command on your case. If you use /b without /s the dir command will produce an output without trailing slash \ therefore the findstr pattern with \\ will fail.

This will fail: dir /b "*.txt"|findstr /R "\\........$"
When this will success: dir /b "*.txt"|findstr /R "^........$"

Just as with a dir command that produces an output when the search pattern word is in the end of the line separated by a trailing slash will also fail.

As this will fail: dir /b /s "*.txt"|findstr /R "^........$"
but this will success: dir /b /s "*.txt"|findstr /R "\\........$"

Paul

Posted 2015-10-11T20:31:25.490

Reputation: 188

I’ve been so busy corresponding with Hastur that I ignored your answer.  :-)  (1) Good point; dir lists files and directories, but the requirement calls for handling files only.  It would have been better, though, if you had explained why you slipped the /a-d into your answer.  (2) The first two versions of your answer worked fine.  (I believe that the ^ in the regular expression is unnecessary, but I’m not sure, so I’m not criticizing you for including it.)  But, when you replaced ^ with \\, you broke your answer, unless /s is added. – G-Man Says 'Reinstate Monica' – 2015-10-12T23:06:53.600

@G-Man You're right as always, I've updated the answer. :) – Paul – 2015-10-13T01:36:35.460

"right as always"!  Wow!  Thanks for that. – G-Man Says 'Reinstate Monica' – 2015-10-13T01:56:57.630

1

Here’s another batch approach:

@echo off
setlocal enabledelayedexpansion
for %%f in (*.txt) do (
      set foo=%%f
      set foo=!foo:~5,5!
      if !foo!==.txt (
            (This filename (%%f) matches the pattern ?????.txt; do what you want with it.)
      )
)

The !foo:~5,5! is an instance of the !variable:~offset:length! substring mechanism; it extracts the substring of the filename starting at the 6th character (offset 5) and with a length of 5 (enough to capture .txt plus one character more, if there is more).  So variable foo has the value .txt if the 6th, 7th, 8th, and 9th characters of the filename are ., t, x, and t, and there is no 10th character.

G-Man Says 'Reinstate Monica'

Posted 2015-10-11T20:31:25.490

Reputation: 6 509

I thought I you compare !foo:~0,5!.txt with !foo! – Paul – 2015-10-12T07:04:34.247

Yes, I guess that would work, too. – G-Man Says 'Reinstate Monica' – 2015-10-12T08:35:29.913