2

I need to copy the contents of a folder to another folder using a batch file - the problem I'm facing is that one of the parent folders will change every day, being named after today's date. So, for example, I have the following command:

xcopy /Y /S "\\auto-jenkins\Builds\2017\R1\\[0822]\EN\\*.*" "C:\Users\Administrator\Desktop\EN"

This works fine today, unfortunately tomorrow the [0822] will not exist and the files I need will be under [0823]. Does anyone know of a way I can use a wildcard in place of [0822]?

The [08**] folder will be the only folder below \R1 if that helps...

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Stephen O'Gorman
  • 53
  • 1
  • 1
  • 4

3 Answers3

3

Does anyone know of a way I can use a wildcard in place of [0822]?

You don't need a wildcard. Use the current date (in the correct format) instead. Use the following batch file.

CopyFiles.cmd:

@echo off
setlocal
rem get the date
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1,2" %%g in (`wmic Path Win32_LocalTime Get Day^,Month ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _month=00%%h
  )
rem pad day and month with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
xcopy /Y /S "\auto-jenkins\Builds\2017\R1[%_month%%_day%]\EN*.*" "C:\Users\Administrator\Desktop\EN"
endlocal

Further Reading

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
1

You can use the automatic date variable %date which is country specific:

xcopy /Y /S "\auto-jenkins\Builds\2017\R1\[%date:~3,2%%date:~0,2%]\EN\*.*" "C:\Users\Administrator\Desktop\EN"

Here, the month and the day are extracted from the date string. First number is the start position (starting at 0), next number is the length.

user1016274
  • 4,071
  • 1
  • 23
  • 19
  • 2
    Beware locale settings. Dates should be taken from [wmic](http://stackoverflow.com/a/15378860/995714) so that they can be locale-independent – phuclv Aug 23 '16 at 16:21
  • Please don't promote locale dependent solutions. This won't work in the US for example. See my answer [Wildcards for directory in Windows batch command](https://stackoverflow.com/a/39103037) for the locale independent solution. – DavidPostill Aug 23 '16 at 16:26
  • I understand your point. Your code is a perfect solution, and I'll upvote it. Whereas my suggestion is practical, and simple enough to be adapted to local conventions by nearly everyone. At least people now have a choice. – user1016274 Aug 23 '16 at 20:09
1

Since there is only a single folder in the R1 directory anyway, you can use for /D to get its name:

for /D %%D in ("\\auto-jenkins\Builds\2017\R1\*") do (
    xcopy /Y /S "%%~D\EN\*.*" "C:\Users\Administrator\Desktop\EN"
)

The * is a global wild-card that stands for any number of arbitrary characters. Instead of it, you could also use [????] so your folder name must consist of exactly four characters in between [].

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • "the parent folders will change every day, being named after today's date" so not a single folder then :) – DavidPostill Aug 23 '16 at 16:27
  • Hm... the OP stated there is only one folder below `R1`... did I misinterpret anything? perhaps @StephenO'Gorman should clarify... – aschipfl Aug 23 '16 at 16:29
  • You're right, it's not exactly clear what I was saying. I should have said 'parent folder' singular, meaning the folder that's named after today's date. I'm now using this solution. – Stephen O'Gorman Aug 25 '16 at 09:32