Redirecting output to a temporary file in a Batch file where the filename is held in a variable


In MS DOS Batch files you can redirect output to a file like this:

echo hi > file.txt

However if you wish to run a script several times and create randomly named (or even numericlaly sequential) files it would be good to be able to store the name of the output file in a variable and then redirect the output to that file.

The following batch file was created to make blank floppy disk image for use by Oracle VirtualBox purely using a batch file. The bytes generated are stored in a temporary file that is randomly named, before finally having output placed in a file with a specified name, and then the temporary file is deleted. In this case it would be safe to just name the temporary file with a name that would not be likely to be used, but this method could by used when many tempory files were needed to be genreated in the same directory and there would be very little chance of any name clash. A final check for file existance could also be added to make it even safer.

blank.bat

@echo off

REM Create 1.44MB floppy img file
REM mount floppy output img and Format in VirtualBox VM before using

REM creates 1474560 byte floppy image
REM DOS version of dd if=/dev/zero of=floppy.img bs=1024 count=1440

SET tempfile1=$$tmp$$%random%
SET tempfile2=$$tmp$$%random%

if "%~1" == "" echo Usage: %0 ^<imagename^>
if "%~1" == "" goto exit

if exist "%~1" del "%~1"
if exist $$tmp$$* del $$tmp$$*

for /l %%i in (0,1,31) do echo.012345678901234567890123456789>>%tempfile1%
for /l %%i in (0,1,35) do type %tempfile1% >>%tempfile2%
for /l %%i in (0,1,39) do type %tempfile2% >>"%~1"

del %tempfile1% %tempfile2%

echo "%1" created

:exit

The inspiration for this script came from the following blog.

I have also added a Windows 98 version of this batch file which does not use the %random% number in the filename for comparison with the script above. Windows 98 also lacks a few other features used so the script is slightly different.

blank98.bat

@echo off

REM Create 1.44MB floppy img file
REM Win98se version
REM mount floppy output img and Format in VirtualBox VM before using

REM creates 1474560 byte floppy image
REM DOS version of dd if=/dev/zero of=floppy.img bs=1024 count=1440

SET tempfile1=$$tmp$$1
SET tempfile2=$$tmp$$2

if "%1" == "" echo "Usage: %0 <imagename>"
if "%1" == "" goto exit

if exist "%1" del "%1"
if exist $$tmp$$* del $$tmp$$*

for %%i in (0 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) do echo.012345678901234567890123456789>>$$tmp$$1
for %%i in (0 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) do type $$tmp$$1 >>$$tmp$$2
for %%i in (0 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) do type $$tmp$$2 >>"%1"

del $$tmp$$1
del $$tmp$$2

echo "%1" created

:exit
Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s