Developing Matt

My Technical Journal

Archive for the ‘Batch’ Category

batch-stamp current time and date

leave a comment »

Alas, I’ve broken my rule to not use batch. 

The way I create a file stamping the current date and time is as follows

@ECHO OFF
FOR /F "tokens=*" %%A IN (‘TIME/T’) DO SET Now=%%A
set strFile=%date:~10,4%%date:~4,2%%date:~7,2%_%Now%
set strFile=%strFile%
set strFile=%strFile::=%
set strFile=%strFile: =%
@ECHO ON

dir >>c:\%strFile%_CUSTOMgetfromsvn.txt

Assistance received from….

http://www.robvanderwoude.com/datetimentexamples.php

http://www.dostips.com/DtTipsStringManipulation.php

Written by matt

August 14, 2012 at 12:24 pm

Posted in Batch

The Case Against the Batch

leave a comment »

The next time someone asks me to write a batch file I’m going to say no.  I end up having to write one every 5 or 6 months and they always give me problems, whether security or switches or a misunderstanding on a command.  This one creates a backup directory, and then copies files over from a source location to a destination.  It sounds easy, but the reality is that this simple request has moved from ‘just copy files’ to ‘copy files, create a full log to show errors, make a backup of all existing files, and let my assistant be able to run it.’  Soon I expect them to ask me to enable it to only copy certain folders and make it shoot fireworks when it’s done.  The point is: for production don’t use batch.  Use a vb app

  • Catching errors isn’t easy..it involves piping out all your cammands in hope that one of them contains an error which they usually don’t. (it seems like there is a way to catch system errors but i can’t find it) 
  • VB app has error handling
  • VB app has debugging
  • Sending emails is easier (i know there are handy dandy tools but there is a comfort level difference here that makes my point for me)
  • Security: anyone with access to the bat file can modify it
  • Extensibility is easier
  • It doesn’t feel like I’m rewriting the wheel. 

There is a bottom line here: program in a technology that you are used to or want to.  Your case can be made simply under the notion that it’s what you know and are motivated to do.  If you don’t know it or aren’t motivated it will take you longer.  Stand up for your self.  With that said, here’s my batch file that runs every day that I wrote because I was asked to and I didn’t talk back.  : )

echo on
REM For testing purposes my production destination is c:\temp
REM this batch file copies existing files to a directory dated today, deletes the existing files, and copies from a source location

REM STEP 1 check for flag before copying
if not exist \\sourcelocation\c$\website\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 c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd% GOTO DELETEFILES
mkdir c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%

echo STEP 3 COPY LIVE CONTENT FILES TO BACKUP FOLDER >c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
xcopy c:\temp\content  c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd% /y /e /c >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

echo STEP 4 DELETE FILES in content files ON PRODUCTION SERVER >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
:DELETEFILES
del c:\temp\content\* /q /s >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

echo step 5 show all the files in the source that are about to be copied
dir \\sourcelocation\c$\website\content /s >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

echo STEP 6 COPY FILES TO NEW DIRECTORY >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
xcopy \\sourcelocation\c$\website\Content c:\temp\content\ /y /e /c >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
echo STEP 7 DELETE COPYCONTENTYES.TXT FILE IN LIVE DIRECTORY >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
del c:\temp\content\CopyContentYes.txt >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

:end

Things I learned while creating this

  •  Del command with any of the /a switches will only delete files with those attributes (not including those files)
  • > will pipe out your results to a log
  • >> will append to an existing log
  • How to create a directory with using a date (this script creates a directory called ContentBackup_2008_04_08)
  • del /q suppresses confirmations and xcopy /q is quite mode (doesn’t show the files being copied)

Written by matt

April 8, 2008 at 8:27 am

Posted in Batch, VB.net

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

Written by matt

December 27, 2007 at 2:54 pm

Posted in Batch