Cynosure.X International LLC

:

Add Comment | Related Links | TrackBack
Related Content

GetTempPathName.bat (GPL)

GetTempPathName.bat is a Windows batch file that provides your batch programs with a guaranteed unique temporary file name. Just call this script with the prefix you want for your temporary file, and this script will return an unique temporary name. Two optional parameters allows you to specify suffix for the temporary file and the directory to locate the temporary file.

After calling this script, the temporary file is created to prevent another process from using the same file name. Your application should delete the temporary file after using it.

GetTempPathName.bat is distributed as executable source code under the GNU General Public License. Please see the license agreement elsewhere on this site.

Usage

  GetTempPathName.bat "prefix" ["suffix"] ["directory"]

Examples

  > GetTempPathName.bat foo
"C:\DOCUME~1\JuJu\LOCALS~1\Temp\foo2164.tmp"

> GetTempPathName.bat foo .txt
"C:\DOCUME~1\JuJu\LOCALS~1\Temp\foo13500.txt"

> GetTempPathName.bat foo .txt .
".\foo16875.txt"

Example Batch Code

  call SetVariableToOutput.bat GetTempPathName.bat %~n0
set tempFile=%OutputVariable%

Attached File: GetTempPathName.bat (744 B)

Chieh Cheng
Fri, 18 Jul 2008 18:37:17 +0000

While implementing the "Random Sound Board" software, I have found a bug in this GetTempPathName.bat script. The bug is that the temporary file it creates is not empty. It contains a single line. In most cases, where you are overwriting the temporary file, it doesn't cause any problem. But if you are expecting an empty file to write to and read, then you are in for a big surprise. I've fixed the bug in the newer version of GetTempPathName.bat, which is attached below.

Attached File: 1 - GetTempPathName.bat (748 B)

Chieh Cheng
Sun, 26 Oct 2008 01:36:40 +0000

This script outputs the temporary file path with double-quotes. If you can to get rid of the double quotes you can use the sub-string functionality of "Set". The following is an example to strip the double-quotes and assign it to a variable.

  call SetVariableToOutput.bat GetTempPathName.bat %~n0
set tempFile=%OutputVariable:~1,-1%

Chieh Cheng
Thu, 19 Mar 2009 09:44:27 +0000

I made slight changes to this "GetTempPathName.bat" script. I've fixed some of the double-quote inconsistencies. Now it outputs the temporary file names without the surround double-quote. This fix made this script more robust and easier to use in your custom scripts.

Attached File: 2 - GetTempPathName.bat (802 B)

Chieh Cheng
Wed, 23 Dec 2009 02:24:49 +0000

It turns out, on some Windows user accounts, the temporary directory variable is not set, causing GetTempPathName.bat to fail. I have made a slight change to this version of GetTempPathName.bat to make it more robust and resolve that issue. The new version is attached below.

Attached File: 3 - GetTempPathName.bat (805 B)

Chieh Cheng
Wed, 10 Feb 2010 21:03:30 +0000

Remember to delete the temporary file after your script is done with it. Here is an example:

  call SetVariableToOutput.bat GetTempPathName.bat "%~n0"
set tempFile=%OutputVariable%

. . . your code . . .

del "%tempFile%"

Chieh Cheng
Sun, 28 Mar 2010 02:47:31 +0000

Here is an example of how to use it within an if-statement with delayed expansion:

  SetLocal EnableDelayedExpansion

if %i% equ 1 (
call SetVariableToOutput.bat GetTempPathName.bat "%~n0"
set tempFile=!OutputVariable!

. . . your code . . .

del "!tempFile!"
)

Chieh Cheng
Fri, 19 Dec 2014 08:43:59 +0300

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

Add Comment

Spam Control | * indicates required field
Your Name: *
E-mail:
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 7 + 0?
Click button only once, please!

TrackBack

TrackBack only accepted from WebSite-X Suite web sites. Do not submit TrackBacks from other sites.

Send Ping | TrackBack URL | Spam Control

No TrackBacks yet. TrackBack can be used to link this thread to your weblog, or link your weblog to this thread. In addition, TrackBack can be used as a form of remote commenting. Rather than posting the comment directly on this thread, you can posts it on your own weblog. Then have your weblog sends a TrackBack ping to the TrackBack URL, so that your post would show up here.

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

52 Users Online

Hacking Digital Cameras
Fun for Photographers



Amazon Associate

Copyright © 1996 - 2024. All Rights Reserved.