2

You'll have to forgive me as I am a beginner when it comes to batch file and Powershell scripting.

We have a backup solution with SQL that is currently being tested, which will perform Full backups once a week and then Differental backups for the reminder of the weekdays. As part of the restore process, we have a Windows batch script file in place which will rename the Full backup file before compressing it with 7Zip. The rename portion of the script looks like this:

cd C:\SQL Backups\Production Backups\eStart_Good
set date=%date:~-4%_%date:~3,2%_%date:~0,2%
ren *%date%* eStart_Good_FullBackup.bak

As this command will get run once a week on the night of a Full backup, it looks for today's date in the form of eg. 2016_03_02 because that's how the naming convention is set when backing up from SQL. This works as it should do.

However, what I want to happen after a Full backup from SQL as occurred is rename the previous eStart_Good_FullBackup file by taking out the 'FullBackup' portion of the name and replace with the date 7 days previous to today's date. So if the next Full backup is set to run on 04/03/2016, I want it to show up in the file name as 2016_02_26 for example.

I know with the rename command I need to change it to:

ren FullBackup eStart_Good_%date%.bak (FullBackup being a wild card.)

But, I've tried a number of combinations to change the set date variable and I keep getting the wrong numbers or pieces of code when echoing the variable.

set date=%date:~-4%_%date:~3,2%_%date:~0,2%

Does anyone have any advice they can give me please?

Jamie Eltringham
  • 810
  • 3
  • 16
  • 25
Sonic23
  • 21
  • 2

1 Answers1

0

Working with dates in batch scripts is... complicated. Powershell might be much simpler way for you to work with this. However, sometimes Powershell isn't an option and you just need to do it with a batch script. Here's a script I wrote for a how to figure out 'yesterday': How do I find last Sunday's date and save it in a variable in a Windows batch file. You can make it loop seven times to get seven days ago.

Hope this helps.

Community
  • 1
  • 1
Wes Larson
  • 1,042
  • 8
  • 17