Batch file to move files based on creation date of the file


I am looking for a bat file which when run through Windows Scheduler will perform this task:

Move files from a folder (rawdata) to a new folder (DataArchive/<date>) based on the creation date of the file.

So if there are 3 files in my folder "RawData":
a.txt - Creation Date 08/01/2012
b.txt - Creation Date 08/01/2012
c.txt - Creation Date 08/03/2012

Two folders should be created in a new folder called :  "DataArchive"
Folder 1 = 08012012 -> Should contain files a.txt and b.txt
Folder 2 = 08032012 -> Should contain file c.txt

What would be the best way to take care of it? Code sample would be helpful.
Who is Participating?
Try this; set the variable "RawData" to the path and file mask of the raw data files, and "ArchiveFolder" to where you want the archive folders created.
The script is currently in test mode and will only show the commands it would normally run; remove the two uppercase "ECHO"s to run it for real.
And personally, I'd create the time stamp as "YYYYMMDD" instead of "MMDDYYYY", makes for better sorting ...
@echo off
setlocal enabledelayedexpansion
set RawData=C:\Temp\*.txt
set ArchiveFolder=C:\Temp\DataArchive
for %%a in ("%RawData%") do (
	echo Processing %%~nxa ...
	set File=%%~fa
	for /f "tokens=1* delims=," %%a in ('wmic datafile where "name='!File:\=\\!'" get 'CreationDate' /format:csv ^| find /i "%ComputerName%"') do (set CreationDate=%%b)
	echo %%~nxa: !CreationDate!
	set cYear=!CreationDate:~0,4!
	set cMonth=!CreationDate:~4,2!
	set cDay=!CreationDate:~6,2!
	set TimeStamp=!cMonth!!cDay!!cYear!
	if not exist "%ArchiveFolder%\!TimeStamp!" (
		ECHO md "%ArchiveFolder%\!TimeStamp!"
	ECHO move "!File!" "%ArchiveFolder%\!TimeStamp!"

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.