Some DOS batch tips

Create a date string for a filename (thanks go to StackOverflow)

The below is one long line:

for /F “usebackq tokens=1,2 delims=” %%i in (`wmic os get LocalDateTime /VALUE 2^>NULL`) do if ‘.%%i.’==’.LocalDateTime.’ set ldt=%%j

You can shorten to yyyymmdd like so:

set ldt=%ldt:~0,8%

To zip an entire folder structure into a file (with the date in the name)

The following is one line long.  It should not wrap

c:\tools\7za\7za a \\target\folder\file-%ldt%.zip c:\toBackup\folder\*

Invoking VSS (disk shadow) and then working with files from the shadow

Disk shadowing involves setting up the shadow, and then executing commands against the shadow.  Unfortunately sometimes things go awry while executing the commands, and then the shadow hangs around. So I always start the script with a shadow removal.  Here’s what my batchfile looks like:

diskshadow –s cleanup.cmds
diskshadow –s diskshadow.cmds

The cleanup.cmds contains the following:

UNEXPOSE p:

That’s all.  It removes the temporary drive p: which was set up as the volume shadow drive.
Next we look at diskshadow.cmds:

SET CONTEXT PERSISTENT NOWRITERS
SET METADATA example.cab
SET VERBOSE ON
BEGIN BACKUP
ADD VOLUME C: ALIAS systemVolumeShadow
CREATE
EXPOSE %systemVolumeShadow% p:
EXEC c:\scripts\backupfiles.cmd
UNEXPOSE p:
END BACKUP
RESET

In a nutshell, using the file example.cab to hold some metadata, it starts a volume shadow operation on C: and exposes the shadow as p:  It then runs whatever commands you want which are stored in backupfiles.cmd, typically copy operations from p: to a backup.

Print | posted on Thursday, April 07, 2016 12:11 PM

Feedback

No comments posted yet.
Title  
Name
Email (never displayed)
Url
Comments   
Please add 2 and 5 and type the answer here: