将当前日期添加到文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将当前日期添加到文件相关的知识,希望对你有一定的参考价值。

This is a batch file that will add the date to the end of all files in a particular directory located on the user's desktop and place them in a directory called Output_Renamed on the user's desktop.
  1. @echo off
  2. TITLE Add Date
  3.  
  4. set RENAMED="%userprofile%DesktopOutput_Renamed"
  5. echo This batch file will copy all the files from a directory and save them
  6. echo to %RENAMED%. It will also add the current
  7. echo date to the files' name.
  8. echo For questions about this program please contact Corey.
  9. goto START
  10.  
  11. :START
  12. cls
  13. set Loc="%userprofile%DesktopInput_Renamed"
  14. echo Your current working directory is %Loc%
  15. set /p INPUT=Would you like to use this directory? y/n or q for quit:
  16.  
  17. REM Get, cut and set the date variable.
  18. for /f "tokens=2-4 delims=/- " %%a in ('date /t') do set XDate=%%a%%b%%c
  19.  
  20. REM Depending on the user's answer from above do the following.
  21. IF /i %INPUT% == y goto CURRENT
  22. IF /i %INPUT% == n goto OTHER
  23. IF /i %INPUT% == q goto END
  24. IF /i %INPUT% NEQ y goto INVALID
  25. IF /i %INPUT% NEQ n goto INVALID
  26. IF /i %INPUT% NEQ q goto INVALID
  27.  
  28. REM Rename the files in the current working directory.
  29. :CURRENT
  30. IF NOT EXIST %Loc% goto NOINPUTDIR
  31. REM Create the Renamed_Files directory on the user's desktop if it does not exist.
  32. IF NOT EXIST %RENAMED% mkdir %RENAMED%
  33. cd %Loc%
  34. for /f "tokens=*" %%b in ('dir /b') do (
  35. copy "%%b" %RENAMED%
  36. )
  37.  
  38. REM 1st way of renaming. Works if there is only one period.
  39. REM ren *.* ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????_%XDate%*.*
  40.  
  41. REM 2nd way of renaming. Put the date on the front of the filename.
  42. REM for /f "tokens=*" %%b in ('dir /b') do (
  43. REM ren "%%b" "%XDate%
  44.  
  45. REM 3rd way of renaming
  46. cd %RENAMED%
  47. for /f "tokens=*" %%b in ('dir /b') do (
  48. ren "%%b" "%%~nb_%XDate%%%~xb"
  49. )
  50. dir /b
  51. goto END
  52.  
  53. REM Rename the files in another specified directory.
  54. :OTHER
  55. cls
  56. set /p DIRPATH=Enter the full path of the directory or q to quit:
  57. IF /i %DIRPATH% == q goto END
  58. IF /i %DIRPATH% == c: goto WINDOWS
  59. IF /I %DIRPATH% == c: goto WINDOWS
  60. IF /i %DIRPATH% == %WINDIR% goto WINDOWS
  61.  
  62. echo You entered %DIRPATH%
  63. set /p CORPATH=Is this correct? y/n or q for quit:
  64.  
  65. REM Do the following depending on the user's entry.
  66. IF /i %CORPATH% == y goto RENOTHER
  67. IF /i %CORPATH% == n goto OTHER
  68. IF /i %CORPATH% == q goto END
  69. IF /i %CORPATH% NEQ y goto INVALID
  70. IF /i %CORPATH% NEQ n goto INVALID
  71. IF /i %CORPATH% NEQ q goto INVALID
  72.  
  73. REM This part is used to rename files that are located in another directory.
  74. :RENOTHER
  75. IF NOT EXIST %DIRPATH% goto NOTTHERE
  76. IF NOT EXIST %RENAMED% mkdir %RENAMED%
  77. cd %DIRPATH%
  78. for /f "tokens=*" %%b in ('dir /b') do (
  79. copy "%%b" %RENAMED%
  80. )
  81. cd %RENAMED%
  82. for /f "tokens=*" %%b in ('dir /b') do (
  83. ren "%%b" "%%~nb_%XDate%%%~xb"
  84. )
  85. dir /b
  86. goto END
  87.  
  88. :WINDOWS
  89. ECHO This is the Windows directory. Can't rename.
  90. goto OTHER
  91.  
  92. :NOTTHERE
  93. ECHO The directory could not be found!
  94. set /p TRYAGAIN=Would you like to try again? y/n
  95. IF /i %TRYAGAIN% == y goto OTHER
  96. IF /i %TRYAGAIN% == n goto END
  97. IF /i %TRYAGAIN% NEQ y goto INVALID
  98. IF /i %TRYAGAIN% NEQ n goto INVALID
  99.  
  100. :INVALID
  101. echo Your input was not recognized!
  102. goto END
  103.  
  104. :NOINPUTDIR
  105. cls
  106. ECHO I'm sorry but your Input_Renamed directory does not exist.
  107. ECHO You must create Input_Renamed directory on your Desktop and copy the files you
  108. ECHO want to rename to that directory. Otherwise, please specify a different
  109. ECHO directory to use.
  110. set /p TRYAGAIN=Would you like me to create the Input_Renamed directory? y/n or d to specify another directory:
  111. IF /i %TRYAGAIN% == y goto INPUTDIRCREATE
  112. IF /i %TRYAGAIN% == n goto END
  113. IF /i %TRYAGAIN% == d goto OTHER
  114. IF /i %TRYAGAIN% NEQ y goto INVALID
  115. IF /i %TRYAGAIN% NEQ n goto INVALID
  116. IF /i %TRYAGAIN% NEQ d goto INVALID
  117.  
  118. :INPUTDIRCREATE
  119. cls
  120. mkdir %Loc%
  121. ECHO Input_Renamed has been created on your Desktop. Please copy the files to
  122. ECHO that directory or specify another directory.
  123. goto START
  124.  
  125.  
  126. :END
  127. echo Exiting the program, HAVE A NICE DAY!

以上是关于将当前日期添加到文件的主要内容,如果未能解决你的问题,请参考以下文章

将文件添加到存档时不要修改文件的编辑日期

Google Apps 脚本 - 将 n 小时添加到当前时间和日期

如何将 1 天添加到当前日期?

常用python日期日志获取内容循环的代码片段

如何使用R将当前日期添加到Google工作表名称中?

将新片段添加到后台堆栈是不是会暂停当前片段?