简体   繁体   中英

Wildcards for directory in Windows batch command

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM