Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy file and run script based on current date?

Every day after midnight I have to copy log file from day before.

Log names are in format exYYMMDD.log. So today (22.10.2011) I would have to copy file named ex111021.log into some directory.

Is it possible to do it in batch script? If not I could use powershell but would prefer not as it is not installed on my server.

Edit: With help of Siva Charan I created this (polish win7 - echo %date% prints YYYY-MM-DD)

set /a yest_Day = %date:~8,2%-%var:~-2,1%
copy ex%date:~2,2%%date:~5,2%%yest_Day%.log targetDir
like image 833
Piotr Perak Avatar asked Feb 23 '23 15:02

Piotr Perak


2 Answers

In powershell :

$YesterdayfileName = [string]::format("{0:yyMMdd}", ((Get-Date).adddays(-1)))+".log
# today 22/20/2011 gives 111021.log

Copy-Item $YesterdayfileName c:\temp
like image 174
JPBlanc Avatar answered Mar 03 '23 03:03

JPBlanc


Implement this way in Batch file.

Syntax of Copy Command:

copy sourcefilepath destinationfilepath

Example:

For today's date:

copy ex%date:~12,10%%date:~4,2%%date:~7,2%.log D:\

For yesterday's date:

set /a yest_Day = %date:~7,2%-%var:~-2,1%
copy ex%date:~12,10%%date:~4,2%%yest_Day%.log D:\

You have to provide proper file path before the file name ex111022.log / ex111021.log and destination too.

You can make a schedule for this batch file, which will run on daily basis.

like image 42
Siva Charan Avatar answered Mar 03 '23 05:03

Siva Charan