简体   繁体   中英

Search for specific directory names within subdirectories and copy files (Windows batch)

need some help with this one I have a directory that contains subdirectories from various applications so let's say directory is c:\\home and each application has a subdirectory called the application name so we will have

    c:\home\app1
    c:\home\app2

etc.

These applications write large log files and they then get recreated every hour but into a different directory, called according t date and time like dd/mm/yyyy/hr and this is created within the actual subdirectory and a log file with the exact same name will be within each directory for each app. so we will end up with this

    c:\home\app1\1015201410\app1.log
    c:\home\app1\1015201411\app1.log
    c:\home\app1\1015201412\app1.log
    c:\home\app2\1015201410\app2.log
    c:\home\app2\1015201411\app2.log
    c:\home\app2\1015201412\app2.log

I want to list through the directories every hour and collect the latest log from each application, in other words in this instance I want to collect the following 2 only as they are the latest (end time 12 shows it is the 12th hour)

    c:\home\app1\1015201412\app1.log
    c:\home\app4\1015201412\app2.log

Now getting the file one by one is easy enough but the script is going to become too long and needs to be edited on a regular base to allow for new applications added to the directories. I am able to do the copying, formatting the time/date section etc. I just need to find a way to search through the home directories for all subdirectories containing the latest timedate and then copy a file from it elsewhere.

So I tried this. Note timedateformat has been predefined:

    for /D %%d in (c:\home\*\%timedateformat%\*) do (
    for %%f in (%%d\.log) do (
    xcopy %%f C:\destination\
    )
    )

but this obviously does not like the * part and therefore I will get no result. Please if anyone is able to assist, I would greatly appreciate.

for /d %%F in ("c:\home\*") do xcopy "%%F\%timedateformat%\*.log" "c:\destination\"

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