Adjust this script to use date modified

December 20, 2009 at 19:59:57
Specs: Windows Vista
Hi All... I am currently using the batch below
to grab files from a workstation and
copy them over to a unc path on a server
It works very well, but the files are being
duplicated every time it is run regardless
of whether they have changed or not.

The filename if currently pre-pended with the
current system date and time. Is
there a way I can make it use the files date
modified instead. This way I think it
should overwrite the old one rather than
constantly making more copies of the
same file.

Here is the current batch:
@echo off

:: prepare date/time stamp
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t')
do set dt=%%i-%%j-%%k
for /f "Tokens=1" %%i in ('time /t') do set

:: timestamp (yyyymmmdd-hhnnss)
set tm=%tm::=-%
set dtt=%dt%%tm%

::get pc name
Set Hostname=%computername%

:: directories
Set "sourcefolder=C:\TESTFOLDER"
Set "destinationfolder=\\SERVER\TESTING\%hostname%\%dt%"

:: copy files
For /f "delims=" %%a in (
'Dir /a-D /b /s "%sourcefolder%\*.TXT" 2^>nul'
) do If exist "%%a" (
xcopy /s /c /d /i /y "%%a"
"%destinationfolder%\%dtt% %%~na.*"



I have tried using "%%~tI" in place of date /t, but that didn't work.

Any help you can offer would be greatly



See More: Adjust this script to use date modified

Report •

December 20, 2009 at 21:25:28
have you examined xcopy options? it has its own date criteria settable as switches, which would be (imo) more reliable than batch extraction of date from directory.
that said, you can get file date:
for /f %%a in (*.*) do (
set dt=%%~ta
:: format date

there must be a kajillion posts on here about dates. it could be its own subforum under programming!. as you can see, the above code just gives you a system-format date, typically but never invariably mm-dd-yy{yy} hh:mm "am"/"pm" So then ya got to parse it out etc etc. been there, done that So see other posts. Like i said, look into xcopy options, XC handles all the dirty work for you. Also the use of checksum can improve system performance.

oops just realized you'll still need to get the date to create the filename. sorry my bad.
if your filedate format is the (fairly) std setting: mm/dd/yyyy hh:mm xM
::set delimiter (if any) to sep. elements
set dlm=
:: just a sample file-name gatherer for-loop...
for %%z in (*.txt) do (
set dt=%%~tz
::funny, just noticed my system won't let me
:: put the colon next to the endingquote...
for /f "tokens=1-5 delims=-: /" %%a in ("!dt!") do (
set x=%%c
:: select foll. for number of year digits
set x=!x:~2!
set nam=!x!!dlm!%%a!dlm!%%b!
:: add time
set nam=!nam!!dlm!%%d!dlm!%%e

Report •

December 21, 2009 at 16:35:35
Hi nbrane,

Thanks for the detailed response... There certainly are a lot of
date threads on this site. Unfortunately I am having trouble
finding one that relates directly to my problem and I have
difficulty adapting other scripts to my purposes.

Please let me know if I am over simplifying the issue, but is
there a way of just changing the date it looks at from being
the system date - to the date modified of the file?

As it stands now, the file is renamed with the current system
date eg: for the current date of 22/12/2009 (AUS Format) and
time of 10:32am, the file test.txt would be renamed to 22-12-
2009-10-32 test.txt - this file is also saved in a directory as
specified by the set destination folder line in the batch.

All that I want is for it to look for the date/time that the file was
last modified rather than the Current system date and time.

I hope that my problem is now a little more clear.



Report •

December 21, 2009 at 18:37:03
yeah, i got a grasp what you wanted, the code should give you the file's timestamp. here's where to put it:

For /f "delims=" %%a in (
'Dir /a-D /b /s "%sourcefolder%\*.TXT" 2^>nul'
) do If exist "%%a" (
:: insert my code snippet here...
:: this gets the file's timestamp (last modified)
::(or *should*, you indicated you had some problem here?)
set dt=%%~ta
for /f "tokens=1-5 delims=-: /" %%b in ("!dt!") do (
set x=%%d
:: select foll. for number of year digits
::set x=!x:~2! disabled since you use 4-digit year
set dtt=!x!!dlm!%%b!dlm!%%c!
:: add time
:: (i notice you just leave the time with the colon but
:: adjust my code accordingly, i used no separators in
:: my "sample" offering)
set dtt=!dtt!!dlm!%%d:%%e
:: my code is set up for enabled expansion, you will need to
:: adjust it (! to %) or else set your own script to enabled.
xcopy /s /c /d /i /y "%%a"
"%destinationfolder%\%dtt% %%~na.*"

that should do it except for mopping up the small stuff.
(delimiters etc). only thing i can't figure is:
"timestamp (yyyymmmdd-hhnnss)"
accounting for four char.s in month? also, my filetimes don't show seconds, just minutes, so if yours are the same you might have to settle for that.
let me know if any difficulties (usually are with my code!)

Report •

Related Solutions

December 21, 2009 at 19:29:32
Hi nbrane,

So I'm not too sure what the "timestamp (yyyymmmdd-hhnnss)" part means either to be honest. I copied and pasted from somewhere else on this forum and adjusted a little. As it was "::'d" out I never bother to change it.

Sorry to be a pain, but I am having a little trouble following where you suggest I add in your code...

Any chance I could get you to copy my original post with your ammendments? Would be very much appreciated...

Report •

December 22, 2009 at 12:08:06
Mick, don't really understand the real issue, but check that your involved machines (servers and clients) all have the same timezone setup. If not, files may be seen changed, while they are not. Setting to the same date format, may also exclude issues if you do some parsing involving dates as strings. Apart from that, I would say to continue using XCOPY /D as before, should work.

Report •

December 22, 2009 at 15:14:51
Hi tvc,

all I wanted to do was adjust my current script (very top)
which works fine, to use the date/time modified from the file
rather than the current system date/time.

I realise that I'm probably over simplifying the issue and that
problems, particularly involving dates and batch scripts are
not always straight forward, but I was hoping that someone
may be able to assist me on a beginner level.

Thanks for taking the time to respond.


Report •

December 23, 2009 at 01:58:23
That's too convoluted for me to sort out, but a few points:

[1] There *IS NO* general date/time substring solution.

[2] The default datye/time of a file *IS* the modified.

[3] xcopy will copy only newer with /d. And plenty of other options.

Helping others achieve escape felicity


Report •

December 24, 2009 at 00:04:29
FWIW dept. (like i said, can't resist leaving loose ends)
@echo off & setlocal enabledelayedexpansion
Set Hostname=%computername%
set dlm=-
Set "sourcefolder=C:\TESTFOLDER"
Set "destinationfolder=\\SERVER\TESTING\%hostname%

For /f "delims=" %%a in (
'Dir /a-D /b /s "%sourcefolder%\*.TXT" 2^>nul'
) do If exist "%%a" (
::----------- create name from filedate
set fdt=%%~ta
for /f "tokens=1-5 delims=-: /" %%b in ("!fdt!") do (
set x=%%d
:: set x=!x:~2! for two-digit year option
set dtt=!x!!dlm!%%b!dlm!%%c!
set dt=!dtt!
set dtt=!dtt!!dlm!%%e!dlm!%%f
xcopy /s /c /d /i /y "%%a"



Report •

Ask Question