1

I have a Directory with a deep Directory->Sub-directory tree structure. I need to write a batch file to copy all the numbered files (files with names as digits and no alphabetic characters) from all the sub-directories.

For example, a sub-directory might contain the following files:

  • WR10091.txt
  • AX10091.htm
  • 10091.txt
  • AX10091.xml
  • 10091.xml

I need to copy 10091.txt and 10091.xml to another location. I can copy files like AX10091.xml and AX10091.htm by specifying AX*.*. But I cannot figure out how to copy just numbered files with no alphabetic characters. There are thousands of directories and the directory structure does not have any pattern (the depth of a tree branch can vary considerably).

Any help will be appreciated.

Dipped Bits
  • 161
  • 7

3 Answers3

2
@echo off
    setlocal enableextensions disabledelayedexpansion
    set "source=%cd%"
    set "target=x:\target\folder"

    for /r "%source%" %%a in (*) do (
        (for /f "delims=0123456789" %%b in ("%%~na") do (
            break
        )) || echo copy "%%~fa" "%target%"
    )

In this code the for %%a will iterate over all the files under the indicated folder. For each of them, the for /f %%b will try to tokenize the file name (%%~na) using numbers as delimiters. If the file name only contains numbers, there will be nothing to process (only delimiters) and the inner for raises errorlevel. This is checked with conditional execution (the code after the || is executed if the previous command fails) and if errorlevel was raised the copy operation is echoed to console.

If the output is correct, remove the echo to perform the copy.

note: the break in the inner for loop is included just to have a command that does nothing when files with non numeric names are found.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 1
    Oooh - very efficient, clever, and elegant. Use of DELIMS to remove digits is interesting reverse thinking, and BREAK and the FOR loop return code are both rarely seen constructs. – dbenham May 18 '15 at 13:06
  • 1
    This can be simplified into a one line command to be executed from the command line (no batch). I'm assuming the current directory is the source root: `for /r %a in (*) do (for /f "delims=0123456789" %%b in ("%%~na") do break)||copy "%a" "targetPath"` – dbenham May 18 '15 at 15:40
  • @dbenham, nice. Just some corrections (copy/paste `%%` problems) and "improvements" (less verbose) if someone wants to use it from command line: `for /r %a in (*) do @(for /f "delims=0123456789" %b in ("%~na") do @break)||echo copy "%a" "targetPath"` – MC ND May 18 '15 at 16:30
  • I've never seen the `for loop return code` before. Very, very nice! `:-)` – Endoro Jul 08 '15 at 09:26
0
@echo off

for /f "tokens=* delims="  %%a in ('dir /b /s /a:-d "*"') do (
    echo %%~na|findstr /e /b /r "[1-9]*" >nul 2>nul && (
        copy %%~fa c:\somewhere\
    )

) 

should be executed in the same directory as the files.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0
for /f "delims=" %%a in ('dir /b/s/a-d ^| findstr /reic:"\\[0-9][0-9]*\..*" /c:"\\[0-9][0-9]*"') do copy "%%~a" "targetDir"

This might not work with XP and/or Vista, but this can be fixed if needed (see What are the undocumented features and limitations of the Windows FINDSTR command).

Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63