Wildcards for directory in Windows batch command

13,948

Solution 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 [].

Solution 2

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

Solution 3

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.

Share:
13,948
Stephen O'Gorman
Author by

Stephen O'Gorman

Updated on June 28, 2022

Comments

  • Stephen O'Gorman
    Stephen O'Gorman almost 2 years

    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...

  • phuclv
    phuclv over 7 years
    Beware locale settings. Dates should be taken from wmic so that they can be locale-independent
  • DavidPostill
    DavidPostill over 7 years
    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 for the locale independent solution.
  • DavidPostill
    DavidPostill over 7 years
    "the parent folders will change every day, being named after today's date" so not a single folder then :)
  • aschipfl
    aschipfl over 7 years
    Hm... the OP stated there is only one folder below R1... did I misinterpret anything? perhaps @StephenO'Gorman should clarify...
  • user1016274
    user1016274 over 7 years
    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.
  • Stephen O'Gorman
    Stephen O'Gorman over 7 years
    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.