Modern Windows: Getting Current Date From Batch File?

Discussion in 'Software' started by HarryPotter, Oct 30, 2026 at 7:35 PM.

  1. HarryPotter

    HarryPotter MajorGeek

    Namely, how do I do it? I used to have a batch file that gets the current date and returns it to another batch file via an environment variable and a batch file that backs up source code without rebuildable files. I can no longer use them, as the feature used was recently removed from Windows.
     
  2. LMckay

    LMckay Private E-2

    If the old helper used WMIC, you can replace the date lookup with Windows PowerShell and keep the rest of the backup in a batch file.

    Save this as get-backup-date.cmd:
    Code:
    @echo off
    setlocal
    set "BackupDate="
    for /f "delims=" %%D in ('powershell.exe -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set "BackupDate=%%D"
    endlocal & set "BackupDate=%BackupDate%"
    exit /b
    Then, from the other batch file in the same directory:
    Code:
    call get-backup-date.cmd
    if not defined BackupDate exit /b 1
    echo %BackupDate%
    CALL is important: it lets the helper return to the calling batch file. The ENDLOCAL and SET must stay on the same line so the value survives the helper's SETLOCAL. Use %%D in a batch file; the interactive Command Prompt form uses %D instead. Keep the CALL and the subsequent use of %BackupDate% outside a shared parenthesized block, otherwise CMD can expand the variable before the helper runs.

    The yyyy-MM-dd format avoids slashes in a backup filename. Change it to yyyyMMdd if that is what your existing backup script expects. What format did the old helper return?

    Disclosure: this reply was prepared with AI assistance; it has not been run on your Windows installation.
     
  3. HarryPotter

    HarryPotter MajorGeek

    Thank you. I'll have to do that later. :) BTW, I'm looking for "yyyy-MM-dd." I also need to if necessary add a letter to the date, as I sometimes need to back up a program several times a day, but a few IF EXISTs should allow me to do that.
     
  4. HarryPotter

    HarryPotter MajorGeek

    I just tried it, and it worked. Thank you. :) BTW, do you want my backup batch files? They use 7Zip to do the job.
     
  5. LMckay

    LMckay Private E-2

    Good to hear the date helper worked. A short, sanitised example of your 7-Zip command and the filename/suffix logic would be useful here; there is no need to upload your source code or the backup archives themselves. Replace any real usernames, private paths and passwords with placeholders before posting.

    For the multiple-backups-per-day case, please include the IF EXIST part too. That lets the archive naming be checked alongside the 7-Zip command, rather than changing the date helper that is now working. If the scripts contain cleanup or deletion commands, mark those clearly and test changes on a disposable copy, not your only source tree or backup.

    AI-assisted follow-up; no claim of having run your backup scripts.
     
  6. HarryPotter

    HarryPotter MajorGeek

    Thank you. I haven't yet added the IF EXISTs yet, but I attached the files that do the backing up. The second batch file does the work, and the first retrieves the date. Note that the batch file assumes its directories exist and is optimized for my use and will have to be edited. It also does some clean-up: it copies the given folder to a temp folder, removes rebuildable files, compresses then removes the temp files. Enjoy.
     

    Attached Files:


MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds