Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for maketemp.bat

(view source code of maketemp.bat as plain text)

  1. @ECHO OFF
  2. :: Not for Windows 9* or MS-DOS
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: Only one argument allowed
  5. IF NOT "%~2"=="" GOTO Syntax
  6. :: Check if FINDSTR is available
  7. FINDSTR /? >NUL 2>&1 || GOTO Syntax
  8. :: Allowed argument is a variable name, check for invalid characters
  9. ECHO.%1| FINDSTR /R /I /C:"[^0-9a-z\._-]" >NUL && GOTO Syntax
  10.  
  11. :: Use local variable
  12. SETLOCAL ENABLEDELAYEDEXPANSION
  13.  
  14. :: Use the default if no variable name was specified
  15. IF NOT "%~1"=="" (SET VarName=%~1) ELSE (SET VarName=TempFile)
  16.  
  17. :Again
  18.  
  19. :: Use creation time as prefix
  20. :: Note: spaces are replaced by zeroes, a bugfix by Michael Krailo
  21. SET TempFile=~~%Time: =0%
  22. :: Remove time delimiters
  23. SET TempFile=%TempFile::=%
  24. SET TempFile=%TempFile:.=%
  25. SET TempFile=%TempFile:,=%
  26.  
  27. :: Create a really large random number and append it to the prefix
  28. FOR /L %%A IN (0,1,9) DO SET TempFile=!TempFile!!Random!
  29.  
  30. :: Append .tmp extension
  31. SET TempFile=%TempFile%.tmp
  32.  
  33. :: If temp file with this name already exists, try again, otherwise create it now
  34. IF EXIST "%Temp%.\%TempFile%" (
  35. 	GOTO Again
  36. ) ELSE (
  37. 	TYPE NUL > "%Temp%.\%TempFile%" || SET TempFile=
  38. )
  39.  
  40. :: Retrieve the fully qualified path of the new temp file
  41. FOR %%A IN ("%Temp%.\%TempFile%") DO SET TempFile=%%~fA
  42.  
  43. :: Display the fully qualified path of the new temp file
  44. SET TempFile
  45.  
  46. :: Return the fully qualified path of the new temp file
  47. ENDLOCAL & SET %VarName%=%TempFile%
  48.  
  49. :: Done
  50. GOTO:EOF
  51.  
  52.  
  53. :Syntax
  54. ECHO.
  55. ECHO MakeTemp.bat,  Version 2.10 for Windows 2000 and later
  56. ECHO Create a new temporary file with a unique name, and return
  57. ECHO its fully qualified path in an environment variable
  58. ECHO.
  59. ECHO Usage:  MAKETEMP  [ variable_name ]
  60. ECHO.
  61. ECHO Where:  variable_name  is the optional name of the environment
  62. ECHO                        variable that will contain the returned
  63. ECHO                        path (default: "TempFile")
  64. ECHO.
  65. ECHO Notes:  The fully qualified path of the temporary file will be
  66. ECHO         stored in an environment variable named as specified on
  67. ECHO         the command line, or named "TempFile" if not specified.
  68. ECHO         If a temporary file could not be created, the variable
  69. ECHO         will be empty (undefined).
  70. ECHO         This batch file has been tested in Windows XP only; it
  71. ECHO         will probably work in Windows 2000 too; to make it work
  72. ECHO         in Windows NT 4, the Resource Kit utility FINDSTR must
  73. ECHO         be installed and in the PATH.
  74. ECHO.
  75. ECHO Written by Rob van der Woude
  76. ECHO http://www.robvanderwoude.com
  77.  
  78. IF "%OS%"=="Windows_NT" ENDLOCAL
  79. IF "%OS%"=="Windows_NT" COLOR 00
  80.  

page last modified: 2024-02-26; loaded in 0.0214 seconds