Copy from previous date folder to current date folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
manasrrp
Posts: 4
Joined: 01 Feb 2018 23:41
Contact:

Copy from previous date folder to current date folder

#1 Post by manasrrp » 02 Feb 2018 03:24

Hi,
I want to copy all files & folders from a parent source folder to destination folder of current date inside a batch script such that as below.
I had make it partially completed to create the destination folder whose name be such like pre-concatenated string "PNSH" + "current date"

xcopy "%source%" "%destination%" /E /Y

For Example
PNSHddmmyyyy format i.e. PNSH03022018

to do this code has written as like this

SET dirname="PNSH%date:~0,2%%date:~3,2%%date:~6,4%"
MKDIR %dirname%

The source folder is as below same as above format
PNSHddmmyyyy format i.e. PNSH02022018

Help me for appropriate batch script.
:)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy from previous date folder to current date folder

#2 Post by aGerman » 02 Feb 2018 10:54

There is no DateTime value type in Batch. You have to use your own algorithms to calculate dates. E.g.

Code: Select all

@echo off &setlocal
for /f "delims=." %%i in ('wmic os get LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
set "yToday=%LocalDateTime:~,4%"
set "mToday=%LocalDateTime:~4,2%"
set "dToday=%LocalDateTime:~6,2%"
call :DaysAdd %yToday% %mToday% %dToday% -1 yYesterday mYesterday dYesterday

echo Today:     %dToday%%mToday%%yToday%
echo Yesterday: %dYesterday%%mYesterday%%yYesterday%
pause

goto :eof


:DaysAdd ByVal_YearIn ByVal_MonthIn ByVal_DayIn ByVal_DeltaIn ByRef_YearOut ByRef_MonthOut ByRef_DayOut
setlocal EnableExtensions
set /a "Year = %~1, Month = 100%~2 %% 100, Day = 100%~3 %% 100"
set /a "a = 14 - Month, a /= 12, b = Year + 4800 - a, c = Month + 12 * a - 3, d = 153 * c + 2"
set /a "d = d / 5 + Day + b * 365 + b / 4 - b / 100 + b / 400 - 1 + %~4"
set /a "e = 4 * d + 3, e /= 146097, f = -e * 146097, f /= 4, f += d"
set /a "g = 4 * f + 3, g /= 1461, h = -1461 * g, h /= 4, h += f, i = 5 * h + 2, i /= 153, Day = 153 * i + 2, Day /= 5"
set /a "Day = -Day + h + 1, Month = -i / 10, Month *= 12, Month += i + 3, Year = e * 100 + g - 4800 + i / 10"
set /a "Month = 10%Month%, Day = 10%Day%"
endlocal &set "%~5=%Year%" &set "%~6=%Month:~-2%" &set "%~7=%Day:~-2%" &goto:eof
Steffen

Post Reply