' +----------------------------------------------------------------------------+ ' | Jeffrey M. Hunter | ' | jhunter@idevelopment.info | ' | www.idevelopment.info | ' |----------------------------------------------------------------------------| ' | Copyright (c) 1998-2011 Jeffrey M. Hunter. All rights reserved. | ' |----------------------------------------------------------------------------| ' | FILE : TempFile.vbs | ' | CLASS : Files and Directories | ' | PURPOSE : Create a temporary file. Write to it, then read it back. | ' | PARAMETERS : None | ' | USAGE : cscript TempFile.vbs //NoLogo | ' | NOTE : As with any code, ensure to test this script in a development | ' | environment before attempting to run it in production. | ' +----------------------------------------------------------------------------+ Option Explicit Function Main Const TEMPORARY_FOLDER = 2 Dim objFSO Dim objTempFolder, strTempFileName, strTempFileNameFull, objTempFile Dim strOutput ' +---------------------------------+ ' | CREATE NEW FSO OBJECT | ' +---------------------------------+ Set objFSO = WScript.CreateObject("Scripting.FileSystemObject") ' +---------------------------------+ ' | CREATE / WRITE TEMP FILE | ' +---------------------------------+ Set objTempFolder = objFSO.GetSpecialFolder(TEMPORARY_FOLDER) WScript.Echo "TEMP PATH = " & objTempFolder.Path strTempFileName = objFSO.GetTempName() WScript.Echo "TEMP FILE = " & strTempFileName strTempFileNameFull = objTempFolder.Path & "\" & strTempFileName WScript.Echo "TEMP FILE (full) = " & strTempFileNameFull Set objTempFile = objTempFolder.CreateTextFile(strTempFileName) objTempFile.WriteLine "====================================" objTempFile.WriteLine "TEMP FILE EXAMPLE " objTempFile.WriteLine "====================================" objTempFile.Close() ' +---------------------------------+ ' | READ TEMP FILE | ' +---------------------------------+ If (objFSO.FileExists(strTempFileNameFull)) Then Set objTempFile = objFSO.OpenTextFile(strTempFileNameFull, 1, False) strOutput = "" Do While objTempFile.AtEndOfStream <> True strOutput = objTempFile.ReadLine() WScript.Echo strOutput Loop objTempFile.Close Else WScript.Echo "ERROR: File not found!" End If Set objFSO = Nothing End Function Main WScript.Quit(0)