1

I am trying to build a .bat file to move files from 1 folder into another one, based off of the oldest time-stamped file.

Folder 1:

Filename:        Date Modified:
test1.dat        5/12/2014 4:03pm
test2.dat        5/12/2014 4:04pm

The goal is to move the oldest file into Folder 2. Currently, my batch file below moves everything from folder 1 to folder 2, obviously because I don't have any time related code entered.

set Delay=10
move /-y "C:\Users\bingersoll001\Desktop\folder1\*.dat" "C:\Users\bingersoll001\Desktop\folder2"
timeout 10 /nobreak
call move.bat

Is there any way to accomplish this?

1
  • Which version of Windows? If it's reasonably recent, PowerShell is far easier.
    – Bob
    Commented May 13, 2014 at 16:04

2 Answers 2

2

Assuming you're using a relatively recent version of Windows (Vista or newer), I'd recommend you use PowerShell. It's much easier to do this kind of task there. You can also install PS on XP if you want.

gci | where {!$_.PSIsContainer} | sort LastWriteTime | select -f 1 | move -destination "C:\some\path\"

Or if you have PS 3.0/Windows 8:

gci -file | sort LastWriteTime | select -f 1 | move -destination "C:\some\path\"

Explanation:

  • gci - Short for Get-ChildItem. It gets all files in a directory. -file makes it return files only, no directories. Analogous to dir and ls.

  • where - Short for Where-Object. Filter the input by some condition. Before PS 3.0, -file didn't exist for gci, so it was necessary to use where filter.

  • sort - Short for Sort-Object ascending by LastWriteTime.

  • select - Short for Select-Object. Only take the first (-f 1) result.

  • move - Short for Move-Item. Moves the result of the previous pipeline into the -destination folder.

Further help available with the help <commandname> command.

2

The following batch script will move the file with the oldest write time from its original folder to the target one.

@echo off
setlocal

set source=C:\Users\bingersoll001\Desktop\folder1
set dest=C:\Users\bingersoll001\Desktop\folder2
set exts=*.dat

pushd "%source%"

for /f "delims=" %%G in ('dir /b /o:d %exts%') do (
move /-y "%%~G" "%dest%"
goto :break
)
:break

popd
endlocal & exit /b

How it works

  1. Turn off the command echoing feature for a less verbose output.

    @echo off
    
  2. Make any change to environment variables local to the batch file.

    setlocal
    
  3. Set a new local variable called source which holds the source folder path.

    set source=C:\Users\bingersoll001\Desktop\folder1
    
  4. Similar to the previous line, define a new local variable for the destination folder.

    set dest=C:\Users\bingersoll001\Desktop\folder2
    
  5. Creates a new local variable which contains the file extensions to filter. Multiple extensions have to be separated by a semicolon (;).

    set exts=*.dat
    
  6. Change the current working folder.

    pushd "%source%"
    
  7. Execute the dir /b /o:d /t:w %exts% command, and then parse its output. The /b parameter, the dir command displays a bare file list with no heading or summary; /o:d makes the list sorted by date/time (oldest first). By default files are sorted by their last write time. The "delims=" part is required to avoid issues with file paths containing space characters.

    for /f "delims=" %%G in ('dir /b /o:d %exts%') do (
    
  8. Move the file to the destination folder.

    move /-y "%%~G" "%dest%"
    
  9. No other file needs to be processed; jump to the :break label thus exiting the for loop.

    goto :break
    
  10. Restore the previous working folder and exit.

    popd
    endlocal & exit /b
    

Further reading

You must log in to answer this question.

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