Help creating a program to copy data from last 24 hours

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lopiop
Posts: 5
Joined: 23 Jan 2015 23:25

Help creating a program to copy data from last 24 hours

#1 Post by lopiop » 23 Jan 2015 23:36

Hi all, newbie here, I need help to create a batch file that will copy data (log files) from one folder to another from the last 24 hours to the current time the batch file was run/open.

Previously, I have used this code and it worked, however this code is deficient as it only copy the log files based on the current date.
Lets say if somebody is seeking for data from the last 24 hours, and he/she runs the program on 24 Jan, 12.01 am, thus all the data from the previous day wont be copied.

Below is the code I have used previously.

Code: Select all

for /F "tokens=1* delims= " %%A in ('DATE/T') do set SYSDATE=%%B
set DATE_YYYY=%SYSDATE:~6,4%
set DATE_MM=%SYSDATE:~3,2%
set DATE_DD=%SYSDATE:~0,2%
set DATE_MM_DD_YYYY=%DATE_MM%-%DATE_DD%-%DATE_YYYY%
echo %DATE_MM_DD_YYYY%
xcopy "w:\documents\*.doc" c:\dest /E /Y /D:%DATE_MM_DD_YYYY%

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help creating a program to copy data from last 24 hours

#2 Post by Squashman » 23 Jan 2015 23:55

If you can use third party programs, XXcopy has relative time options. You can do minutes and hours with it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a program to copy data from last 24 hours

#3 Post by foxidrive » 24 Jan 2015 01:07

I played with Powershell a while back - here's a script that will list the files from the last 24 hours.

Some configuration of Powershell may be needed if it complains.

Code: Select all

@echo off
:: lists files in the last 24 hours

set "file=%temp%\psdaterange.ps1"

cd /d "d:\folder\logs"

(
echo( $date1 = Get-Date
echo( $date2 = $date1.addhours(-24^)
echo(
echo( foreach( $item in (Get-ChildItem -file ^) ^)
echo( {
echo( if( $item.LastWriteTime -ge $date2 -and $item.LastWriteTime -le $date1 ^) {Write-Host $item}
echo( }
) >"%file%"

for /f "delims=" %%a in ('powershell "%file%"') do echo "%cd%\%%a"
del "%file%"

pause

lopiop
Posts: 5
Joined: 23 Jan 2015 23:25

Re: Help creating a program to copy data from last 24 hours

#4 Post by lopiop » 24 Jan 2015 03:25

@foxidrive what should I do after listing the files?
does this work with xp?
sorry really a noob in this

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a program to copy data from last 24 hours

#5 Post by foxidrive » 24 Jan 2015 04:09

Does your XP have Powershell installed?
Test this change to see if it works.

Tell me if there is an error message


Change this:

Code: Select all

) do echo "%cd%\%%a"


to this

Code: Select all

) do echo "%cd%\%%a" & xcopy "%cd%\%%a" "d:\dest\"

lopiop
Posts: 5
Joined: 23 Jan 2015 23:25

Re: Help creating a program to copy data from last 24 hours

#6 Post by lopiop » 24 Jan 2015 05:33

@foxidrive hi, thx for the reply, i tried the script on my laptop, not on my company pc, and the script didnt work, at the first line it said
C:\Test\data\Get child item: A parameter cannot be found that matches parameter name 'file'

Code: Select all

@echo off
:: lists files in the last 24 hours

set "file=%temp%\psdaterange.ps1"

cd /d "C:\Test\data"

(
echo( $date1 = Get-Date
echo( $date2 = $date1.addhours(-24^)
echo(
echo( foreach( $item in (Get-ChildItem -file ^) ^)
echo( {
echo( if( $item.LastWriteTime -ge $date2 -and $item.LastWriteTime -le $date1 ^) {Write-Host $item}
echo( }
) >"%file%"

for /f "delims=" %%a in ('powershell "%file%"') do echo "%cd%\%%a" & xcopy "%cd%\%%a" "C:\Test\transfer"
del "%file%"

pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a program to copy data from last 24 hours

#7 Post by foxidrive » 24 Jan 2015 06:25

It fails in XP powershell here too.

Which OS does your company machine use?

lopiop
Posts: 5
Joined: 23 Jan 2015 23:25

Re: Help creating a program to copy data from last 24 hours

#8 Post by lopiop » 24 Jan 2015 06:38

foxidrive wrote:It fails in XP powershell here too.

Which OS does your company machine use?

windows xp, im trying it out on my windows 7 laptop right now, any idea how to fix this?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help creating a program to copy data from last 24 hours

#9 Post by Squashman » 24 Jan 2015 08:54

Squashman wrote:If you can use third party programs, XXcopy has relative time options. You can do minutes and hours with it.

Since you did not comment about my suggestion I assume this means no?

lopiop
Posts: 5
Joined: 23 Jan 2015 23:25

Re: Help creating a program to copy data from last 24 hours

#10 Post by lopiop » 24 Jan 2015 09:45

Squashman wrote:
Squashman wrote:If you can use third party programs, XXcopy has relative time options. You can do minutes and hours with it.

Since you did not comment about my suggestion I assume this means no?

Hi, thx for the suggestion, preferably i would want to use a batch file, as I cant access the web through the company pc. could you elaborate on how to use the program? thx

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help creating a program to copy data from last 24 hours

#11 Post by Squashman » 24 Jan 2015 11:18

XXcopy is no different then using xcopy in a batch file.
Read the section about relative time specifiers. Shows a perfect example for you.
http://www.xxcopy.com/xxcopy17.htm

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a program to copy data from last 24 hours

#12 Post by foxidrive » 25 Jan 2015 05:24

lopiop wrote:
foxidrive wrote:It fails in XP powershell here too.

Which OS does your company machine use?

windows xp, im trying it out on my windows 7 laptop right now, any idea how to fix this?


No, I'm not very experienced in Powershell, and the differences as it was upgraded.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Help creating a program to copy data from last 24 hours

#13 Post by Samir » 02 Feb 2015 11:51

I like xxcopy for things like this. Being able to use it on various OSs without modification of the batch is nice too.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help creating a program to copy data from last 24 hours

#14 Post by Squashman » 02 Feb 2015 13:59

Samir wrote:I like xxcopy for things like this. Being able to use it on various OSs without modification of the batch is nice too.


Well you could do this without any third party utilities but in this case it is the perfect tool for the job.

You could use a function to convert the current date and the files last modified time to the number of hours since a specified date then do the subtraction. There are all kinds of native batch functions for date and time conversions.

Dave's hybrid GetTimeStamp.bat could easily do the conversion to hours as well.

Post Reply