Developing Matt

My Technical Journal

set date in variable: batch

leave a comment »

I don’t do batch files but once or twice a year, long enough that I forget everything and have to look it all up. (here’s one good site). Anyway, this time around I wanted to use the date to create a directory. I found a lot of different ways to do it. The best is the last one

  1. link: a universal date parsing routine. You call the application and use the variable. (looks like it creates files on the fly so I didn’t end up using it, besides it’s too long and smarter than me so I had to ditch it)
  2. link: this one is crazy. It uses the prompt command to capture the date. The prompt command renames your prompt. Anyway, he calls a new window and then traps it. Pretty neat stuff, but I couldn’t quite get it to work as a variable
  3. link: quick and dirty way to get the date (log_%date:~4,2%-%date:~7,2%-%date:~12,2%)
  4. link: pretty much the same as three, but long and drawn out and explains how it all works.

My complete script is below. It checks for a file. And if the file exists it backs up the existing files from server 1, and deletes these existing files, and then copies the files from server 2 to server 1.

@ echo off
REM STEP 1 check for flag before copying
if not exist \\testserver\Content\CopyContentYes.txt goto end

REM STEP 2 CREATE DIRECTORY WITH DATE
Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%

REM if backup directory exists then GO TO delete files line (else create directory and copy files)
if exist \\productionserver\ContentBackup\ContentBackup_%yyyy%_%mm%_%dd% GOTO DELETEFILES
mkdir \\productionserver\ContentBackup\ContentBackup_%yyyy%_%mm%_%dd%

REM STEP 3 COPY LIVE CONTENT FILES TO BACKUP FOLDER
xcopy \\productionserver\Content \\productionserver\ContentBackup\ContentBackup_%yyyy%_%mm%_%dd% /y /e /c

REM STEP 4 DELETE FILES in content files ON PRODUCTION SERVER
:DELETEFILES
del \\productionserver\Content\* /q /s /ah /ar /as /aa

REM STEP 3 COPY FILES TO NEW DIRECTORY
xcopy \\testserver\Content \\productionserver\Content\ /y /e /c

:end

Advertisements

Written by matt

December 27, 2007 at 2:54 pm

Posted in Batch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: