I have already written here for this problem, we solved it, but now I have to add this "if the file was created in the last 24 hours copy it else don't". I have found several examples online but I just cant seem to be able to figure the Command syntax out. I would really apreciate some help with this ... here is the website I found:
https://github.com/npocmaka/batch.scripts/blob/master/fileUtils/fileModifiedTime.bat
and here is my code:
ECHO OFF
set ReadFolder1="C:\Users\ugrum\Desktop\new"
set Destination="C:\Users\ugrum\Desktop\Zacasnamapa"
set sevenZipDir="C:\Program Files\7-Zip\7zG.exe"
set currentDate=%date%
IF NOT EXIST %Destination% MKDIR %Destination%
FOR /R %ReadFolder1% %%G IN (*_NOT*) DO (
ECHO %%G
FOR %%f IN %%G DO SET filedatetime=%%~tf
IF %currentDate% - %filedatetime:~0, 10% <= 24
XCOPY "%%G" %Destination% /C
)
%sevenZipDir% a -tzip neustrezne.zip %Destination%
RMDIR /Q /S %Destination%
PAUSE
I know the if
is wrong but I have no idea how to get it right.
Here is a batch code for your task:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ReadFolder1=%USERPROFILE%\Desktop\new"
set "Destination=%USERPROFILE%\Desktop\Zacasnamapa"
set "SevenZipApp=%ProgramFiles%\7-Zip\7zG.exe"
if not exist "%Destination%" mkdir "%Destination%"
rem Get seconds since 1970-01-01 for current date and time.
rem From date string only the last 10 characters are passed to GetSeconds
rem which results in passing dd/mm/yyyy or dd.mm.yyyy in expected format
rem to this subroutine independent on date string of environment variable
rem DATE is with or without abbreviated weekday at beginning.
call :GetSeconds "%DATE:~-10% %TIME%"
rem Subtract seconds for 24 hours (24 * 3600 seconds) from seconds value.
set /A "CompareTime=Seconds-24*3600"
for /R "%ReadFolder1%" %%G in (*_NOT*) do (
echo %%G
set "FullFileName=%%G"
for %%H in ("%%G") do (
call :GetSeconds "%%~tH:0"
setlocal EnableDelayedExpansion
if !Seconds! GTR %CompareTime% (
echo Copy file !FullFileName!
%SystemRoot%\System32\xcopy.exe "!FullFileName!" "!Destination!\" /C /I /M /Q /R /Y >nul
)
endlocal
)
)
"%SevenZipApp%" a -tzip neustrezne.zip "%Destination%"
rmdir /Q /S "%Destination%"
endlocal
goto :EOF
rem No validation is made for best performance. So make sure that date
rem and hour in string is in a format supported by the code below like
rem MM/DD/YYYY hh:mm:ss or M/D/YYYY h:m:s for English US date/time.
:GetSeconds
rem If there is " AM" or " PM" in time string because of using 12 hour
rem time format, remove those two strings and in case of " PM" remember
rem that 12 hours must be added to the hour depending on hour value.
set "DateTime=%~1"
set "Add12Hours=0"
if not "%DateTime: AM=%" == "%DateTime%" (
set "DateTime=%DateTime: AM=%"
) else if not "%DateTime: PM=%" == "%DateTime%" (
set "DateTime=%DateTime: PM=%"
set "Add12Hours=1"
)
rem Get year, month, day, hour, minute and second from first parameter.
for /F "tokens=1-6 delims=,-./: " %%A in ("%DateTime%") do (
rem For English US date MM/DD/YYYY or M/D/YYYY
rem set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
rem For German date DD.MM.YYYY or English UK date DD/MM/YYYY
set "Day=%%A" & set "Month=%%B" & set "Year=%%C"
set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
)
rem Remove leading zeros from the date/time values or calculation could be wrong.
if "%Month:~0,1%" == "0" if not "%Month:~1%" == "" set "Month=%Month:~1%"
if "%Day:~0,1%" == "0" if not "%Day:~1%" == "" set "Day=%Day:~1%"
if "%Hour:~0,1%" == "0" if not "%Hour:~1%" == "" set "Hour=%Hour:~1%"
if "%Minute:~0,1%" == "0" if not "%Minute:~1%" == "" set "Minute=%Minute:~1%"
if "%Second:~0,1%" == "0" if not "%Second:~1%" == "" set "Second=%Second:~1%"
rem Add 12 hours for time range 01:00:00 PM to 11:59:59 PM,
rem but keep the hour as is for 12:00:00 PM to 12:59:59 PM.
if %Add12Hours% == 1 if %Hour% LSS 12 set /A Hour+=12
set "DateTime="
set "Add12Hours="
rem Must use two arrays as more than 31 tokens are not supported
rem by command line interpreter cmd.exe respectively command FOR.
set /A "Index1=Year-1979"
set /A "Index2=Index1-30"
if %Index1% LEQ 30 (
rem Get number of days to year for the years 1980 to 2009.
for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
rem Get number of days to year for the years 2010 to 2038.
for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)
rem Add the days to month in year.
if "%LeapYear%" == "N" (
for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)
rem Add the complete days in month of year.
set /A "Days+=Day-1"
rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
rem Exit this subroutine.
goto :EOF
For details on the method used to compare time read my answers on
Note 1:
Format of date and time string returned by %DATE:~-10% %TIME%
and %%~tF:0
depends on Windows region and language settings. The code as written expects for date string dd/mm/yyyy
or dd.mm.yyyy
or dd-mm-yyyy
and for time string hh:mm:ss
with or without leading zeros in case of hour, minute or second is less than 10.
An abbreviated weekday at beginning of date string of environment variable DATE does not matter because of using only last 10 characters if the date string is with leading zero for day or month less than 10 as it is standard on Windows.
For US date format mm/dd/yyyy
the command rem
must be moved from one line to another line in subroutine GetSeconds
as the comment explains.
Note 2:
See my answer on set environment variables with spaces for the reason using set "variable=value with spaces"
and reference value of variable
enclosed in double quotes where necessary instead of using set variable="value with spaces"
which could result in wrong code on execution.
By the way:
Do you have ever thought to spend a few bucks for buying a license of WinRAR as your task to compress all files modified within last 24 hours could be done with WinRAR with a single command line or by using GUI of WinRAR? Think about the time you and we spent to write a batch code for this very easy to solve task using WinRAR and compare it with the little money a single license of WinRAR costs.
The command line using WinRAR would be:
%ProgramFiles%\WinRAR\WinRAR.exe a -ac -afzip -cfg- -ep -ibck -m5 -tn24h -y "%USERPROFILE%\Desktop\neustrezne.zip" "%USERPROFILE%\Desktop\new\*_NOT*"
a is the command which means add to an archive.
-ac
is a switch to clear archive attribute on each file added to archive. This is not really necessary here, but very often helpful.
-afzip
explicitly tells WinRAR to create a ZIP archive. This switch is not really necessary here as archive file name ends with .zip
and therefore WinRAR would automatically use ZIP instead of RAR compression.
-cfg-
disables the standard configuration for creating archives. It is always a good idea to use this switch when using WinRAR from within a batch file executed periodically to make archive creation independent on settings not defined on command line.
-ep
results in adding the files without path to the archive.
-ibck
tells WinRAR to run minimized to system tray as background application. But with the used options a dialog window is displayed if an error occurs on adding the files to the archive.
-m5
use best compression.
-tn24h
this is the nice option WinRAR has which 7-Zip currently does not have to add only files matching the pattern in specified directory modified within last 24 hours, i.e. newer than current date/time minus 24 hours.
-y
assume yes on all user queries.
For more details on WinRAR switches open in WinRAR from menu Help the Help topics, and open on tab Contents item Command line mode and subitem Switches.