Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.9 KiB

  1. @echo off
  2. rem Enabling verbose or _echo in this script will cause
  3. rem errors in the calling script.
  4. setlocal enableextensions
  5. REM Create a unique file name based on the full path file name
  6. REM passed in.
  7. for %%a in (./ .- .) do if ".%1." == "%%a?." goto usage
  8. if "%1"=="" echo No input file specified & goto end
  9. set infile=%1
  10. for %%i in (%infile%) do (
  11. set fullpath=%%~fi
  12. set drvonly=%%~di
  13. set pathonly=%%~pi
  14. set baseonly=%%~ni
  15. set extonly=%%~xi
  16. )
  17. REM
  18. REM If the filename passed in does not already exist, return that name
  19. REM
  20. if not exist %fullpath% (
  21. echo %fullpath%
  22. goto end
  23. )
  24. REM
  25. REM Otherwise, derive a filename based on the one passed in that doesn't
  26. REM already exist.
  27. REM IN: filename.ext
  28. REM OUT: filename.1.ext
  29. REM
  30. set i=
  31. :loop
  32. set /a i+=1
  33. set newname=%drvonly%%pathonly%%baseonly%.%i%%extonly%
  34. if exist %newname% goto :loop
  35. echo %newname%
  36. REM
  37. REM Create %newname%'s directory if it doesn't exist
  38. REM
  39. if not exist %drvonly%%pathonly% md %drvonly%%pathonly%
  40. goto end
  41. :usage
  42. echo Returns a unique nonexistent full path file name based on the name passed in.
  43. echo.
  44. echo Usage: %~n0 ^<filename^>
  45. echo.
  46. echo ex: %~n0 filename.ext
  47. echo -^> ^<cwd^>\filename.ext ^|
  48. echo -^> ^<cwd^>\filename.1.ext ^| ...
  49. echo -^> ^<cwd^>\filename.2.ext ^| ...
  50. echo.
  51. echo ex: %~n0 %%tmp%%\mylogfile.log
  52. echo -^> c:\tmp\mylogfile.log ^|
  53. echo -^> c:\tmp\mylogfile.1.log ^| ...
  54. echo.
  55. REM DOUBLE UP '%' IN THE FOLLOWING CMD EXAMPLE TO GET CORRECT OUTPUT
  56. echo Typical call sequence from a CMD script:
  57. echo set script_name=%%~n0
  58. echo ...
  59. echo for /f %%%%i in ('%~n0 %%tmp%%\%%script_name%%.log') do set logfile=%%%%i
  60. echo call logmsg.cmd /t "Start %%cmdline%%"
  61. echo.
  62. echo NOTE: We write to the file, so other %~n0 calls will see it. Otherwise,
  63. echo multiple %~n0 calls will return the same unique file name.
  64. echo.
  65. :end
  66. @if defined _echo echo on
  67. @if defined verbose echo on
  68. @endlocal