Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 451
Gabriel Avatar asked Apr 07 '11 22:04

Gabriel


People also ask

How do I move a file to a different directory?

Select the file you want to move by clicking on it once. Right-click and pick Cut, or press Ctrl + X . Navigate to another folder, where you want to move the file. Click the menu button in the toolbar and pick Paste to finish moving the file, or press Ctrl + V .

How do I move multiple files from one folder to another?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want. If you need to, click on any folder you see to access its subfolders.


1 Answers

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.

like image 62
Ken White Avatar answered Oct 27 '22 00:10

Ken White