stevecross
stevecross

Reputation: 5684

How to copy files with a specific pattern?

I have some folders that contain at least one zip-file. However there could also be more zip-files in it. The folders look something like this:

parent
|
+--X7G000738512
|  |
|  `--X7G000738512_20141028_1210.zip
|
+--X7G000843295
|  |
|  +--X7G000843295_20141028_1210.zip
|  |
|  `--X7G000843295_20141028_1210_OLD.zip
|
+--X7G000482756
|  |
|  +--X7G000482756_20141028_1210.zip
|  |
|  `--X7G000482756_20141028_1210_ungueltig.zip
|
`--X7G000285672
   |
   +--X7G000285672_20141028_1210.zip
   |
   +--X7G000285672_20141028_1210_OLD.zip
   |
   `--X7G000285672_20141028_1210_ungueltig.zip

Now I want to copy the zip-files that don't end with "_OLD" or "_ungueltig" to the parent directory. How can i accomplish this task?

Upvotes: 1

Views: 37

Answers (1)

MC ND
MC ND

Reputation: 70923

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "target=c:\somewhere\parent"

    for /f "delims=" %%a in ('
        dir /s /b /a-d "%target%\*.zip"
        ^| findstr /r /i /c:"%target:\=\\%\\[^\\]*\\.*"
        ^| findstr /l /v /i /e /c:"_OLD.zip" /c:"_ungueltig.zip"
    ') do for %%b in ("%%~dpa\..") do (
        copy "%%~a" "%%~fb"
    )

This uses a dir command to retrieve the full list of zip files, that is filtered with findstr to only process files under a subdirectory (not process files in target folder), and the remaining list is filtered to only process files that does not contain the indicated strings. The for %%a command processes this list of files, and for each one, a copy command is executed.

I was not sure about the parent in the question, so, in this code the parent is determined from the folder where the .zip file is stored. This is the reason for the for %%b, to determine the place where to copy the file.

If the parent folder is the %target% folder, remove the last for and use

....
    ^| findstr /l /v /i /e /c:"_OLD.zip" /c:"_ungueltig.zip"
') do copy "%%~a" "%target%"

Upvotes: 1

Related Questions