Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to use a files date to move sort the file. Say I have file test.txt created on 5/10/2011, I want to be able to move the the file to C:\2011\05\10\test.txt.

You ca do something like enter image description here

I guess my question is simply: How can you put the files date onto the commandline? What should replace 'DATE /t' to get test.txt "created on" date?

EDIT Further research: I can get the date time via dir & find... but it won't work in the batch file...enter image description here

share|improve this question
up vote 3 down vote accepted

I did this as a batch file, hence the double % signs. Just make those single if you're doing this from the command line.

for %%f in (*.txt) do (
for /f "tokens=1-3 delims=/ " %%i in ("%%~tf") do (
if not exist "C:\%%k\%%i\%%j" mkdir "C:\%%k\%%i\%%j"
move "%%f" "C:\%%k\%%i\%%j" >NUL

So the short answer to your question about getting the file's date is to use %~tI in a for statement, where I is the variable used in the for statement. See "help for" for more info on what else is available.

share|improve this answer
How does the "%%~tf" work? I assume ~t is date of file like the above dir | find? – WernerCD May 10 '11 at 16:15
It's really %~tf, but since this is a batch file, I doubled the %. My first line gets me a list of the text files, storing each in a variable f. The FOR statement has a set of special substitutions to get file properties when you do that. %~t gets the date and time, %~z gets the size, etc. The f is appended to the end of those special substitutions, because that's the variable I'm telling FOR to look at. I'm using the second FOR statement to parse the contents of %~tf, since it gives me the date and time and I don't want the time in this case. – Patrick S. May 10 '11 at 16:19

What you need to do is take a FOR command and process the output of a DIR command. You'll probably have to nest two FOR commands to get the individual numbers of the date.

Suggested DIR command to use:

dir c:\ /TC /A-D

Suggested FOR command:

FOR /F " TOKENS=1,5 delims= " %A IN ('dir c:\ /TC /A-D') DO @ECHO A-%A B-%B

This should give you mostly what you need in order to get this to work. The problem is that CMD is terrible at this kind of work. While you can do it, I would recommend that you use a more advanced language like powershell. It's free for windows and Quest Software makes a great GUI editor called powerGUI.

Here is what I wrote in Powershell to do this. It's a lot easier for me to follow.

#Get files in C:\temp and filter out directories
$tarfol = "C:\temp" #Target Folder
$var = Get-ChildItem $tarfol | Where-Object {$_.mode -inotmatch "d"} 

#Process Files
foreach($item in $var)
    #Build New Folder Path
    $folder = "$tarfol\$item.CreationTime.Year\$item.CreationTime.Month\$item.CreationTime.Day"

    #Test for for folder
    if(!(Test-Path $folder)) {
        #Create folder
        New-Item -Path C:\temp\$y\$m\$d -ItemType directory
    #Move item
    Move-Item -Path $item.FullName -Destination "$folder"

Hope this helps.

share|improve this answer

enter image description here
I had to escape the | with a ^.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .