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

Posted on 2012-08-20
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 82

    Accepted Solution

    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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
    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…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now