Copy files modified in last hour batch

February 25, 2010 at 02:00:29
Specs: Windows XP
I wish to create a batch file which will copy image files created in the last hour to another folder. Note I only want to copy files with a .jpg extension.

Can anyone help with this?


See More: Copy files modified in last hour batch

Report •

#1
February 25, 2010 at 03:41:19
It can easily be done in fact, step 1 is to have a tool that can behold the current time and date, into parameters.

Do you have that ?

If not - and this is a question towards the expert - do we here at Computing.net have some kind of single reference for such things ?


Report •

#2
March 1, 2010 at 12:54:02
tvc: do we here at Computing.net have some kind of single reference for such things ?
The general consensus is that date math in CMD sucks. What we do about it depends on the guy answering the question. Some will use debug.exe to write a .COM file; others (me) will suggest VBScript; the rest typically suggest a third party language. If Win7 is involved, POWERSHELL! would technically be a valid alternative.

No one has bothered to write any of this down in a central source.


Report •

#3
March 1, 2010 at 14:32:55
I would agree, apart from the last line. I'm sure somebody would have, but it's a matter of getting that one being recognized, and used.

Threastarter : still alive ?


Report •

Related Solutions

#4
March 1, 2010 at 16:53:01
actually, in this case, since it's time and not date, you could probably just let AT do most of the work. set up the bat to reschedule itself for +1hour after running the copy operation.
time is not as hard to deal with as dates.
@op: if interested in script to add time to current time, let me know, i wrote one for something else awhile back and it's still laying around.

"No one has bothered to write any of this down in a central source."
@Razor, thats a good idear, if 'someone' would set something up like a faq or "how-to" page, but we'll probably just keep improvising and muddle through! :(


Report •

#5
March 2, 2010 at 05:33:40
Time is definatly easier, indeed.

I like the solution you posted Nbrane, but keep in mind that it does have some serious drawbacks, and the biggest one being the fact that the script itself (that what you put into the scheduling) does NOT contain the 1-hour rule.

You are allowing the wrapper (super-script feature, by means of scheduler) take over that task. Now, if that one fails (scheduler down, server down, scheduler blocked, ...) the script itself may behave weird on it (and basically, not take only the files within the 1-hour rule). You must always have any constraint, as close to the core as possible, so that any running of it, uses that constraint.

Dngood, if you want to, I'll write a script to help you with it. But, there's a catch, being that I will rely on other scripts I have (the ones handling the time), so in other words, it'll be a collection of scripts. What I would do, is to write 1 script, that can do the following :

Copy 1 file to another folder, only if the file is less than one hour old. It'll be something like:

xcopytime.cmd C:\source\test.jpg C:\target 60

Which would copy file C:\source\test.jpg to C:\target but only if the file is "less than 60 minutes old". To check on the "age" of a file, I'll need to call other script, which are custom.

Let me know if you want this.


Report •

#6
March 2, 2010 at 06:01:22
That sounds great to me; there's no problem running multiple scripts as long as it's fairly easy to implement!

Dave.


Report •

#7
March 2, 2010 at 06:15:57
dngood: copy image files created in the last hour
What's the scope of this? All image files created anywhere on your PC? All image files created in a specific directory? All image files created in a specific branch of a directory tree?

nbrane: if 'someone' would set something up like a faq or "how-to" page
I ain't stoppin' ya.


Report •

#8
March 2, 2010 at 06:34:36
The files are in all in one directory, there may be different file formats in the directory, i wish to target only the image (jpg) files.

Report •

#9
March 2, 2010 at 11:52:38
@echo off
:: time increment - note: uses environment table var. "nextime" to pass the value
:: increment the current TOD by the input cycle-specification (mmm or hh:mm)
call :norm %1
set addm=%mm%
set addh=%hh%
:echo current TOD: %time%
:echo add-cycle, hours: %hh% minutes: %mm%
call :norm %time%
set /a hh+=addh
set /a mm+=addm
call :nn
set hh=0%hh%
set mm=0%mm%
set nextime=%hh:~-2%:%mm:~-2%
:echo TOD plus add-cycle: %nextime%
:at %nextime% %2
set hh=
set mm=
set addh=
set addm=
echo.%nextime%
goto :eof

:norm
:: normalize input cycle (may be any number of minutes, or hh:mm format)
:: GIGO alert! --- validation NOT performed! --- GIGO alert!
for /f "tokens=1-2 delims=:" %%x in ("%1") do (
set hh=%%x
set mm=%%y
)
if "%mm%" equ "" (
set mm=%hh%
set /a hh=0
) else (
if "%hh:~0,1%" equ "0" if "%hh:~1%" neq "" set hh=%hh:~1%
)
if "%mm:~0,1%" equ "0" if "%mm:~1%" neq "" set mm=%mm:~1%
:nn
set /a hh+=mm/60
set /a mm="mm%%60"
set /a hh="hh%%24"


Report •

#10
March 3, 2010 at 06:51:18
VBScript copies files modified up to an hour ago:
Const srcDir = "c:\images"
Const destDir = "c:\SomeWittyName"
hourAgo = DateAdd("h", -1, Now)
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder(srcDir).Files
  If  LCase(fso.GetExtensionName(f)) = "jpg" _
  And f.DateLastModified > hourAgo Then _
    f.Copy destDir & "\"
Next 'f


Report •

#11
March 5, 2010 at 02:31:45
Working on the xcopytime.cmd thing ...

Report •

#12
March 5, 2010 at 15:34:09
Got it ! ... well, not the full solution, but something that you could need to come to conclusion of the question of this thread. The difference is that the code I used for the below, does not care what format you have setup your Windows in ...
If you need a copy of GETDTF, just PM me, and mention your email address.

D:\Temp>getdtf c:\test.txt

set dtfile_day=05
set dtfile_month=03
set dtfile_year=2010
set dtfile_hours=23
set dtfile_minutes=45
set dtfile_seconds=34
set dtfile_weekday=5
set dtfile_week=09

Delta between now and last modification time, in:
seconds =2636.000000
minutes =43.933333
hours =0.732222
in days =0.030509
in months =0.001002
in years =0.000084

D:\Temp>


Report •

#13
March 10, 2010 at 07:01:33
If you are still interested, got a script to copy files, under condition that the file is less than X minutes old. Scripts are not nicely tuned, but they work. If you need 'em, PM me with your email adress.

D:\Temp>recentfilecopy test.txt c:\ 2 /Y


File test.txt is 15 minutes old.
File test.txt is older than 2 minutes.

NO copy action is performed now.

D:\Temp>recentfilecopy test.txt c:\ 19 /Y


File test.txt is 15 minutes old.
File test.txt is less than 19 minutes old.
xcopy test.txt c:\ /Y
D:test.txt
1 File(s) copied

D:\Temp>


Report •

Ask Question