Copying files that match one pattern but not another
Solution 1
foo.bat:
dir /b %3 > exclude_list.txt
xcopy %1 %2 /f /y /EXCLUDE:exclude_list.txt
For exclusions like in your example:
call foo.bat "C:\source\*.ext" "C:\destination\" "*-*.ext"
For no exclusions (inelegant hack"):
call foo.bat "C:\source\*.ext" "C:\destination\" "A_STRING_THAT_CANNOT_MATCH"
See also Robocopy switch /xf {filename}
Solution 2
So this is what I'm using right now:
for /f "delims=" %%f in ('dir /b "*.ext" ^| findstr "%TAG%"') do (
xcopy "%ff" %DEST% /f /y
)
So to copy a given tag, you just have to specify TAG=-aaa-
, and you could even specify multiple tags separated by spaces, like TAG=-aaa -bbb-
.
The down side is that to deploy the files that don't have a tag, you'd have to do something really ugly like TAG=^[^-]*$
. Although I could refactor it in the future so that it also takes a variable to specify the /v
flag (which inverts the behavior of findstr
), but that doesn't seem necessary at the moment.
p.s.w.g
Updated on September 18, 2022Comments
-
p.s.w.g over 1 year
I have a number of files that follow a pattern like this:
<BaseName> . <Version> [ - <Tag> - <#> ] . <Extension>
For example:
basename.1.0.0.ext basename.1.0.0-aaa-1.ext basename.1.0.0-aaa-2.ext basename.1.0.0-aaa-3.ext basename.1.0.0-bbb-1.ext basename.1.0.0-bbb-2.ext basename.1.0.0-bbb-3.ext
Now I need to write a strict that will copy just file with a certain 'tag' part (or no 'tag') an target folder. For example:
xcopy *-aaa-*.ext \\server\path /f /y
Will copy only files with the 'aaa' tag. So far so good.
The problem is that the script also needs to work for files with no 'tag', basically I need everything that matches
*.ext
except what matches*-*.ext
.Unfortunately, I have a number of restrictions here:
- I am not able to change the name pattern or location of these files.
- The 'version' part is not known, so
*.1.0.0.ext
will not work. - The 'version' is not a fixed length, so
*.?.?.?.ext
will not work. xcopy
's exclude parameter doesn't accept a pattern, only a list of files, so this won't work.- I am stuck using batch scripting for this. Invoking PowerShell is not entirely out of the question, but it would require a fair amount of overhead to get it working, so I'd like to avoid this if possible.
The actual script will have to look like this:
xcopy "%SOURCE%" "%DEST%" /f /y
And the variables used by this script will be saved in an external configuration file.
How can I write this script so that it works with any set of files, both a specific 'tag' and without any 'tag'?
-
p.s.w.g about 10 years+1 thanks for taking the time to respond. This would work, but it seems a bit messy. I've never used robocopy before, but I'll look into that as a possibility. I'd also welcome any comments on my current solution.
-
henry700 about 10 yearsNo more or less messy than yours, and both have a hack for the case of no exclusions. If it works, it works. One minor plus for my example is the exclusion list can be examined afterwards. (obviously, the params can be changed to ENV and the
call
can be eliminated)