Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

January 17, 2006 12:00 AM

Naming Temporary or Output Files

Windows IT Pro
InstantDoc ID #48747
Rating: (0)

Developing a naming strategy for temporary files or output files lets you ensure that an instance of a script doesn't unintentionally overwrite another file. One naming approach is for the script operator to specify the filename in the script header. I usually try to avoid this option and instead include code in the script that automatically generates the filenames if for no other reason than to avoid the possibility of typos.

I typically use one of three methods to create unique filenames. The first method is file-path substitution. If your script takes input that includes file paths, you can use the file-path information to create a path-based filename after you strip out the slash (\) and colon (:) characters by using string substitution. Remember, you can't use these characters in a filename. This method is ideal for dealing with output files because it lets you easily identify what path location the report (i.e., output file) relates to. You can use a similar technique to deal with spaces and any other characters that you want to strip or replace in a path. The following sample code changes a path into a filename:

Set Path=\\servera\share37\wilma 
Set Path=%Path::\=-% 
Set Path=%Path:\=-% 
Set Pathfn=%Path%.txt 
Echo %Pathfn% 

The second method is to convert the date and time to a filename. This method is best if you want to name reports for the time of day that the script runs. To create a date-based filename, use this syntax:

For /F "tokens=2,3,4,5,6,7,8
  delims=/ " %%i in ("%DATE%")
  Do Set Dfn=D%%i-%%j-%%k.txt 
Echo %Dfn% 

To generate a combination date-and time-based filename, use this syntax:

For /F "tokens=2,3,4,5,6,7,8 
  delims=/: " %%i in ("%DATE% %TIME%")
  Do Set DTfn= 
  D%%i-%%j-%%k-T%%l-%%m-%%n.txt 
Echo %DTfn% 

The third method works well for temporary files in cases where intuitive filenames aren't important, because only the script will be creating, using, and deleting the files. I used this method in the ReplicationTest script. Generally, the only time you'd need to access such temporary files is for script-debugging purposes, so friendly filenames aren't really necessary.

The code below uses the RANDOM environment variable to create a random filename for the temporary files that ReplicationTest uses:

Set Ranfn=%RANDOM%.txt 
Echo %Ranfn% 

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.