Solved Batch file to move newest XML files to a new folder

April 12, 2017 at 00:47:46
Specs: Windows 7
Hey again,

Dozens of XML files are created every day and stored in a folder with older XML files from previous days.

What I want to do is the following:

I want to copy (only) the newest XML files from folder 1 to folder 2. Or in other words: Extract only the newest XML files (newest date) from folder 1 to folder 2.

What is the simplest way to find and copy the newest XML files considering multiple XML files are created each day?

I tried it with:
@echo off
forfiles /d +04/11/2017 "delims=" %%a in ('dir /b /od "c:\Users\Q4223626\Desktop\folder1\*.xml"') do set Youngest=%%a
xcopy /y "c:\Users\Q423626\Desktop\%Youngest%" "folder2\"

The XML files documented should be moved to folder 2 depending on their modified date. e.g: 4/11/2017 08:00 AM

thanks in advance !


See More: Batch file to move newest XML files to a new folder

Report •

#1
April 12, 2017 at 06:20:58
Post the output of this:

:: todayis.bat 2017-04-12 20:03:30.42
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%i in ('dir/b NEWFILE') do (
echo.%%~Ti %%i
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#2
April 12, 2017 at 20:02:22
Thanks for your help ! It only does shows me the new files without moving them or am I doing something wrong?

Report •

#3
April 12, 2017 at 21:25:32
✔ Best Answer
It seems too simple, or else I'm missing something. Why not just copy and "say no":

:: set destination directory
set destination=target

::------ 1)
for %%a in (*.xml) do echo n | copy /-y %%a %destination%

or:
::----- 2)
for %%a in (*.xml) do if not exist %destination\%%a copy %%a %destination%

I don't see where dates need to have anything to do with the solution, ASSUMING that
"newest" simply means any xml files that haven't already been copied previously. But I've probably "slipped a cog" somewhere in the drivetrain.


Report •

Related Solutions

#4
April 12, 2017 at 21:51:40
@nbrane. That was just the right thing ! Thank you... I was thinking way too much about date etc. Thank you very much !!

Report •

Ask Question