简体   繁体   中英

Batch command to move files to a new directory

I want to write a batch job that when executed will grab all the files in the C:\\Test\\Log folder and move them to a new directory in the C:\\Test . This new directory will have a name called "Backup-" and CURRENT DATE.

So once completed, the log folder should be empty with all the files now located in the new folder.

I know I would have to use the MOVE command, but have no idea how to dynamically create a new folder, and use the date to name it.

Something like this might help:

SET Today=%Date:~10,4%%Date:~4,2%%Date:~7,2%
mkdir C:\Test\Backup-%Today%
move C:\Test\Log\*.* C:\Test\Backup-%Today%\
SET Today=

The important part is the first line. It takes the output of the internal DATE value and parses it into an environmental variable named Today , in the format CCYYMMDD , as in '20110407`.

The %Date:~10,4% says to extract a *substring of the Date environmental variable 'Thu 04/07/2011' (built in - type echo %Date% at a command prompt) starting at position 10 for 4 characters ( 2011 ). It then concatenates another substring of Date: starting at position 4 for 2 chars ( 04 ), and then concats two additional characters starting at position 7 ( 07 ).

*The substring value starting points are 0-based.

You may need to adjust these values depending on the date format in your locale, but this should give you a starting point.

this will also work, if you like

 xcopy  C:\Test\Log "c:\Test\Backup-%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%" /s /i
 del C:\Test\Log

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