Creating files of a predetermined size

In order to test FTP file transfers across a newly installed network connection, I needed to create some files of a predetermined size (e.g. 512Kb). The easiest method I found was to run a command that writes out six characters each time it loops (plus two more bytes – a carriage return and a line feed):

@FOR /L %x IN (1,8,524288) DO @echo xxxxxx>>512kb.txt

This increments a counter by 8 each time it loops starting at 1 and ending at 524288, appending to a file called 512kb.txt. To change the file size, change 524288 to another number (divisible by 8) and the output filename to something more suitable.

Avoiding using hard-coded pathnames in scripts

Another gem gained from my anonymous colleague is the use of the %0 environment variable (which returns the current command name in the same way as %1, %2, etc. return any arguments passed to the command) to avoid using hard coded paths in scripts. For example, %0\..\ refers to the directory in which the file is located, and can be used where a pathname is required, but the drive letter may vary, e.g. %0\..\scripts\ (where the scripts folder could be on any available drive, but always the same drive as the calling command).

Scripting the deletion of registry keys and values

One of my colleagues (who wishes to remain anonymous) gave me a great tip this morning – how to delete registry keys and values using a .REG file.

To delete a value, set its contents to – in the .REG file, e.g.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\MyKey]
"MyValue"=-

Or to delete a key, add a – sign after the leading [ in the .REG file, e.g. [-HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\MyKey]

Apparently this works on all version of Windows from Windows 2000 onwards, although I’ve only tried it with Windows XP Professional.