简体   繁体   中英

How do I copy the 4 latest files from a password-protected online directory to another directory?

As a preface, I am a complete noob at batch file processing (which I am assuming is the way to achieve what I'm trying to do). I have done extensive searching on this subject, but the answers given only help me partially.

I work with our HRIS each morning to upload 4 files it produces into our payroll system. They are not all created at the exact same time, but roughly in the same minute and I would like to copy all 4 of these files to a new location. I can't go by file name, because the files change names every day.

The Main Question: How can I script a way to copy the 4 latest files from a password protected directory (not just the latest file) to a new directory? (Another possibility would be to copy all files created on the day the script is run)

The code I found that seems closest is below, but it is only the latest single file, when I would like to copy multiple files.

    :Variables
    SET DatabaseBackupPath=\\virtualserver1\Database Backups

    echo.
    echo Restore WebServer Database
    FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B /O:D') DO SET NewestFile=%%I
    copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"

    sqlcmd -U <username> -P <password> -d master -Q ^
    "RESTORE DATABASE [ExampleDatabaseName] ^
    FROM  DISK = N'D:\%NewestFile%' ^
    WITH  FILE = 1,  ^
    MOVE N'Example_CS' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example.mdf',  ^
    MOVE N'Example_CS_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example_1.LDF',  ^
    NOUNLOAD,  STATS = 10"

I hope this makes sense, and I appreciate your responses.

alright, i have a code that is a litle bit goofy, what i do is making a temporary folder. now i move your latest file into that temporary folder and i repeat this action 4 times.
After that's done, i move the complete temp folder to your d drive

This code is a replacement of the original code between line 6 and 8

if not exist "%DatabaseBackupPath%\WebServer\temp" md "%DatabaseBackupPath%\WebServer\temp"

for /l %%a in (1,1,4) do (

 FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B  /O:D') DO (
SET NewestFile=%%I
move "%DatabaseBackupPath%\WebServer\%NewestFile%" "%DatabaseBackupPath%\WebServer\temp\"
)
)
for /r "%DatabaseBackupPath%\WebServer\temp" %%b in (*.*) do (
       move "%%b" "D:\"
)

i hope this helped you

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