Batch command - adding date at the end of folder

对着背影说爱祢 提交于 2020-01-06 19:42:33

问题


I currently run a batch command to create a folder 1 day in advanced and label it as MMDDYY. Everything is working as intended except single digit days. Currently it named the next day folder has 12214, is it possible to have it name it as 120214?

@echo off

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" 
set "YY=%dt:~2,2%" 
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

:loop
  set /a DD+=1

  if %DD% gtr 31 (
    set DD=1
    set /a MM+=1

    if %MM% gtr 12 (
      set MM=1
      set /a YY+=1
      set /a YYYY+=1
    )
  )
xcopy /d:%MM%-%DD%-%YYYY% /l . .. >nul 2>&1 || goto loop

echo %DD%/%MM%/%YYYY%
mkdir "C:\Users\Name\Desktop\%mm%%dd%%yy%\"

pause

回答1:


You need to pad again the data once the operations have been done. Also you will need some more logic to handle the month change

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Retrieve data
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" 
    set "YY=%dt:~2,2%" 
    set "YYYY=%dt:~0,4%"
    set "MM=%dt:~4,2%"
    set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%"
    set "Min=%dt:~10,2%"
    set "Sec=%dt:~12,2%"

    rem Remove padding from date elements and increase day
    set /a "y=%YYYY%", "m=100%MM% %% 100", "d=(100%DD% %% 100)+1" 
    rem Calculate month length
    set /a "ml=30+((m+m/8) %% 2)" & if %m% equ 2 set /a "ml=ml-2+(3-y %% 4)/3-(99-y %% 100)/99+(399-y %% 400)/399"
    rem Adjust day / month / year for tomorrow date
    if %d% gtr %ml% set /a "d=1", "m=(m %% 12)+1", "y+=(%m%/12)"

    rem Pad date elements and translate again to original variables
    set /a "m+=100", "d+=100"
    set "YYYY=%y%"
    set "YY=%y:~-2%"
    set "MM=%m:~-2%"
    set "DD=%d:~-2%"

    echo Tomorrow: %YYYY% / %MM% / %DD%

Just add the folder creation in the required format




回答2:


Batch is cumbersome with date math. Leap years, month / year changes / etc can be a pain to deal with. I suggest using a JScript Date() object, where all such conversions are handled automatically.

As follows is a batch / JScript hybrid script. Save it with a .bat extension and run it as you are used to running your typical batch scripts.

@if (@a==@b) @end   /* JScript ignores this multiline comment

:: batch portion

@echo off
setlocal

for /f "tokens=1-3" %%I in ('cscript /nologo /e:JScript "%~f0"') do (
    set "MM=%%I"
    set "DD=%%J"
    set "YYYY=%%K"
)

xcopy /d:%MM%-%DD%-%YYYY% /l . .. >nul 2>&1 || goto loop

echo %MM%/%DD%/%YYYY%
mkdir "%userprofile%\Desktop\%MM%%DD%%YYYY:~-2%\"

pause

goto :EOF

:: end batch portion / begin JScript */

function zeroPad(what) { return (what+'').length < 2 ? '0'+what : what; }

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

WSH.Echo([
    zeroPad(tomorrow.getMonth() + 1),
    zeroPad(tomorrow.getDate()),
    tomorrow.getFullYear()
].join(' '));


来源:https://stackoverflow.com/questions/27254636/batch-command-adding-date-at-the-end-of-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!