Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2012-08-20
Medium Priority
Last Modified: 2012-08-25

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.
Question by:nainil
1 Comment
LVL 86

Accepted Solution

oBdA earned 1000 total points
ID: 38312020
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


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question