4

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
2
  • yes it is possible. Refer my answer. +1 for using batch script. Commented Oct 22, 2011 at 10:54
  • @Peri see robvanderwoude.com/datetimentmath.php Commented Oct 22, 2011 at 19:06

3 Answers 3

4

In powershell :

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

Copy-Item $YesterdayfileName c:\temp
Sign up to request clarification or add additional context in comments.

1 Comment

I'll have to go with this solution.
4

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.

5 Comments

Have you tried it? It doesn't work. It doesn't work for today's date and for yesterday's date set doesn't work. I get error saying No argument (message translated from polish)
@Peri: After testing on my machine then only I have posted.
@Peri: Which OS are you using? Your file is saved as ".bat" format?
Ok for today's date it's ex%date:~2,2%%date:~5,2%%date:~8,2%.log on my system. Win7 polish. Based on that %date:~x,y% substrings y characters starting from x character. What echo %date% prints on your system? I see you use substring 12,10 which should substring 10 characters starting at 12th,
As it turns out it doesn't work. It doesn't work for days before 20th of the month but I managed to fix that. But it also doesn't work for first day of the month. It calculates day = 0.
0

are you looking for something like this?

http://www.pcreview.co.uk/forums/do-schedule-automatic-file-copy-t493716.html

1 Comment

No that's not answer to my question. I have no problem scheduling. I don't know how to create name of the file using yesterday's date in batch file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.