Arrays and Temporary Files

One of the limitations of command-shell scripting is that it doesn't really let you put items into an array or a list that stays in memory. Some techniques let you create variables that hold multiple items in a horizontal list (e.g., Set List=Fred Wilma Barney). But this approach's weakness is that the number of characters that can be manipulated in a simulated array is limited. For a small list or a handful of items, this limitation is OK, but if you have a large list, items will be dropped from the list because of the character limitation. Also, using a horizontal list requires the processing overhead of setting the list items into the variable and retrieving them from the variable.

A simpler approach is to use a temporary file as your "array." Then, you can simply redirect command output to a text file and you have a list that you can easily manipulate and filter. The general rules for temporary files are:

  1. Don't overuse temporary files. If you can write your code without resorting to using temporary files, do so.
  2. Be sure that you won't have multiple script instances trying to simultaneously utilize the same temporary file path.
  3. Plan to clean up temporary files both before and after running a script. The "before" cleanup is important in case the last run was interrupted before the cleanup phase. Appending data to an already partially created temporary file can give you some strange duplicate or corrupted results. If you have multiple temporary files that you want to clean up, you can name your temporary files with similar names and use a global If Exist and delete statement to delete the files all in one operation.The following statement performs such a global deletion:

    If Exist %temp%\~DFS*.txt Del %temp%\~DFS*.txt

    Remember that if you use *.*, you'll need to use the Del command with the /Q switch.

    The code at callouts B and F in Listing 1 in the main article shows the before and after deletion sections that use the multi-file delete code.
  4. Determine whether you want to write your temporary files to the same location as your script, the %Temp% folder, or an alternative temporary location.
  5. Remember that if you're logged in as a user and run a script that writes to the %Temp% folder, you'll actually be writing to the %USERPROFILE%\Local Settings\Temp location. If you aren't logged in and the script runs as a scheduled task, the %Temp% location will be %SystemRoot%\TEMP.
  6. Be careful when you're trying to delete a file that might be locked by another operation. Note that the blat temporary file %temp%\blatout.txt in the DFSReportBuilder.bat script (which you can download at InstantDoc ID 92798) isn't deleted when the script ends because it can be locked by the blat.exe tool's use of the file. You can choose to not bother with deleting the temporary file immediately, which is what I did, or you can use the Sleep command to delay the attempted deletion of the temporary file by a few seconds. The code at callout A in Listing 1 in the main article shows the single deletion section for the blat source text file.
  7. While you're testing your scripts, you might be tempted to open and view text files while they are being written. Doing so typically won't cause a problem unless the application you use locks the file and prevents a deletion or modification. Also, if you open any text file in Microsoft Excel, you'll notice that the open file is generally locked and won't accept writes or allow deletions until Excel is closed.
Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.