Temp file name in a .bat file?

  • Thread starter Thread starter Angel Tsankov
  • Start date Start date
Angel said:
How can I get a name for a temporary file in a .bat file?

OK, I'll bite.

Can you elaborate a bit? Give some more detail about what you
want to do?

What do you mean by 'get'?

What is a 'temporary file'?
 
Phil Robyn said:
OK, I'll bite.

Can you elaborate a bit? Give some more detail about what you
want to do?

What do you mean by 'get'?

What is a 'temporary file'?

In other words, is the temp file created by some program or are you trying
to generate a random filename to use for your own temp files?
 
Todd said:
In other words, is the temp file created by some program or are you trying
to generate a random filename to use for your own temp files?

Oh, Todd, you have such a way with words!
 
"Angel Tsankov" wrote in message
How can I get a name for a temporary file in a .bat file?

In WSH (Windows Script Host), the GetTempName method
is provided for exactly this purpose. For example, the following
line of VBS code will display the result of GetTempName:

WScript.Echo CreateObject("Scripting.FileSystemObject").GetTempName

However, to run this command and retrieve the result in a variable
itself needs a workfile (containing the VBS code), which begs the
question.

As an alternative approach, which doesn't require a workfile,
is to use the RANDOM variable to generate a workfile name.

The following code shows how to create a temporary workfile
name of format WRKnnnnn.TMP that doesn't conflict with any
existing file, and loads it into the variable WorkFile:

Lines that aren't prefixed with two spaces have wrapped in transmission
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
:LOOP
SET /a WorkFile=%RANDOM%+100000
SET WorkFile=WRK%WorkFile:~-5%.TMP
IF EXIST %WorkFile% GOTO LOOP

ECHO. Your temporary workfile name is %WorkFile%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

The following screen capture assumes the above is pasted into DEMO.BAT

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
Your temporary workfile name is WRK12947.TMP

C:\WORK>
============End screen capture
 
Phil Robyn said:
Oh, Todd, you have such a way with words!

Sorry. First thing that came to mind was, either we would see a textbook
response to your final question, or an extremely long response unrelated to
the actual task. Oh well.
 
William said:
"Angel Tsankov" wrote in message



In WSH (Windows Script Host), the GetTempName method
is provided for exactly this purpose. For example, the following
line of VBS code will display the result of GetTempName:

WScript.Echo CreateObject("Scripting.FileSystemObject").GetTempName

However, to run this command and retrieve the result in a variable
itself needs a workfile (containing the VBS code), which begs the
question.

As an alternative approach, which doesn't require a workfile,
is to use the RANDOM variable to generate a workfile name.

The following code shows how to create a temporary workfile
name of format WRKnnnnn.TMP that doesn't conflict with any
existing file, and loads it into the variable WorkFile:

Lines that aren't prefixed with two spaces have wrapped in transmission
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
:LOOP
SET /a WorkFile=%RANDOM%+100000
SET WorkFile=WRK%WorkFile:~-5%.TMP
IF EXIST %WorkFile% GOTO LOOP

ECHO. Your temporary workfile name is %WorkFile%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

The following screen capture assumes the above is pasted into DEMO.BAT

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
Your temporary workfile name is WRK12947.TMP

C:\WORK>
============End screen capture

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/

Assuming that this is what the OP is asking for, then how about this?

==========begin file C:\cmd\demo\GetTempFile.cmd ==========
01. @echo off
02. setlocal
03. set tempfile=%date:~4%%time::=%
04. set tempfile=%tempfile:/=%
05. set tempfile=%tempfile:.=%
06. set tempfile
==========end file C:\cmd\demo\GetTempFile.cmd ==========
 
Todd said:
Sorry. First thing that came to mind was, either we would see a textbook
response to your final question, or an extremely long response unrelated to
the actual task. Oh well.

Hey, don't apologize! You hit the nail on the HEAD!

I'm kinda disappointed, 'cause I was also curious about what kind of
response we would get...
 
Back
Top