Categories
DOS & DOS Scripting

Batch Parameter Extensions

These examples use modifiers to modify parameter %1

  • %~f1 Expand %1 to Fully qualified path name
  • %~d1 Expand %1 to Drive letter
  • %~p1 Expand %1 to Path only e.g. \utils\ (ncludes a trailing \) Some commands will interpret this as an escape character.
  • %~n1 Expand %1 to a file Name without file extension C:\utils\MyFile or if only a path is present (with no trailing backslash\) – the last folder in that path.
  • %~x1 Expand %1 to a file eXtension only – .txt
  • %~s1 Change the meaning of f, n, s and x to reference the Short 8.3 name (if it exists.)
  • %~1 Expand %1 removing any surrounding quotes (")
  • %~a1 Display the file attributes of %1
  • %~t1 Display the date/time of %1
  • %~z1 Display the file size of %1
  • %~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

You can also combine these as shown below:

  • %~dp1 Expand %1 to a drive letter and path
  • %~sp1 Expand %1 to a path shortened to 8.3 characters
  • %~nx1 Expand %2 to a file name and extension only
  • %~dp0 The folder from which the script is being run

Reference:

  • https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
  • https://ss64.com/nt/syntax-args.html
  • http://davidthewizard.blogspot.ca/2010/03/better-batch-files-through-command.html
  • http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work
Categories
DOS & DOS Scripting Operating Systems

Robocopy to Copy Folders

There are numerous switches and options for Robocopy that are hard to get right if you're rushing into a task.

Here is a useful example:

robocopy src dest /MIR /R:3 /W:15 /REG /LOG:c:\temp\robocopy.log /NP /TEE /DCOPY:T /MT

/MIR = Copy the mirror structure

/R= Retry only # times.  This is important, the default is one million which effectively means that if there's one bad file in a huge batch, it will never complete.

/W= Wait time between retries.

/REG writes the the R and W settings to the registry as defaults

(Effectively, with the R, W and REG options I"m saying "If there's a problem copying a file it's probably a lost cause.  Try again if you must, but don't waste a lot of time on it.  And don't ask again about these options"'. )

/LOG = Write the results to a log file, I'll miss info if it scrolls out of the buffer.

/NP = don't display the percentage.  Benefits: it gives a visual indicator of progress on screen. Drawbacks: It clogs up the log file with a line for every report of progress, at 1 per every 0.1%.  Over a thousand log lines for every file is a bit much.

/TEE = Show the output on the screen as well as sending it to the log file.

/DCOPY:T = Copy folder timestamps

/MT[:n] = Multi-Threaded mode, where n indicates the number of threads to be used. By default, n is 8 when you use the /MT option, and can be any value from 1 to 128.

 

Categories
DOS & DOS Scripting

Creating a folder named for the current date

When automating a backup script, you may want to be able to create a folder named with the current date.

This quick line will accomplish that;

1
2
@echo off
md %date:~-4,4%-%date:~-10,2%-%date:~-7,2%
Categories
DOS & DOS Scripting

Backup script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@echo off
rem :-----------------------------------------------------------------------------
rem : BACKUP SCRIPT
rem : This script was prepared for backing up a Windows XP computer
rem :   to an external hard drive.
rem : It will create a new folder named for the current date in the \backups folder of
rem :   the external drive, then copy the contents of the data folder on the computer.
rem : If the external drive ever fills up - unlikely - simply delete the oldest
rem :   folders from the backup folder to free up room.
rem : The script assumes that the external drive is drive letter K: and the
rem :   data folder on the source computer is "C:\Documents and Settings".
rem :-----------------------------------------------------------------------------

rem Set configuration - change these values if necessary
set backup_drive=K:
set source_folder=C:\Documents and Settings
 
title Data Backup

rem Remind the user to close all applications - open files may interfere
echo.
echo Preparing to backup computer data to external hard drive.
echo.
echo  - This backup may take several hours. It may best be done overnight.
echo  - All applications, including email, should be closed first.
echo.
echo Are you ready to proceed?
 
:loop
set Choice=
set /P Choice=Type [Y]es or [N]o and press Enter:
:: The syntax in the next line extracts the substring
:: starting at 0 (the beginning) and 1 character long
if not '%Choice%'=='' set Choice=%Choice:~0,1%
echo.
:: /I makes the IF comparison case-insensitive
if /I '%Choice%'=='y' goto proceed
if /I '%Choice%'=='n' goto skip
echo "%Choice%" is not valid. Please try again.
goto loop
 
:proceed
 
echo.
echo Proceeding with backup...

rem Change to backup drive and folder
cd /d %backup_drive%\backups

rem Create a folder for the current date and change to that directory
md %date:~-4,4%-%date:~-10,2%-%date:~-7,2%
cd %date:~-4,4%-%date:~-10,2%-%date:~-7,2%

rem Start the file copy
echo start > start.txt
start /wait xcopy "%source_folder%" /E /C /H /R /Y
echo finish > finish.txt

rem Advise user that backup is completed.
echo.
echo Backup is completed.
echo Files are stored at %backup_drive%\backup\%date:~-4,4%-%date:~-10,2%-%date:~-7,2%\
 
echo.
color 2f
 
goto end
 
:skip
echo.
echo All programs should be closed before this starts.
echo Please close all programs and try again.
goto end
 
:end
echo.
pause