0

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'?

2 Answers 2

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}

2
  • +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.
    – p.s.w.g
    Apr 17, 2014 at 20:12
  • No 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)
    – horatio
    Apr 17, 2014 at 20:38
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .