community.spiceworks.com
Open in
urlscan Pro
2a02:e980:b3::d4
Public Scan
Submitted URL: http://community.spiceworks.com/t/properly-format-this-date-time-stamp-in-win-batch-script/584820
Effective URL: https://community.spiceworks.com/t/properly-format-this-date-time-stamp-in-win-batch-script/584820
Submission: On April 10 via manual from IN — Scanned from DE
Effective URL: https://community.spiceworks.com/t/properly-format-this-date-time-stamp-in-win-batch-script/584820
Submission: On April 10 via manual from IN — Scanned from DE
Form analysis
1 forms found in the DOMPOST /login
<form id="hidden-login-form" method="post" action="/login" style="display: none;">
<input name="username" type="text" id="signin_username">
<input name="password" type="password" id="signin_password">
<input name="redirect" type="hidden">
<input type="submit" id="signin-button" value="Log In">
</form>
Text Content
Skip to main content * News & Insights * News & Insights Home -------------------------------------------------------------------------------- * Artificial Intelligence * Innovation * IT Careers & Skills * Cloud * Cybersecurity * Future of Work * IT Research * All Categories * Community * Community Home -------------------------------------------------------------------------------- * Cloud * Collaboration * Hardware * Networking * Programming * Security * Software * Storage * Vendors * Virtualization * Windows * All Categories * Events * IT Tools * Cloud Help Desk * Inventory Online * Contracts * Connectivity Dashboard * Pricing * All IT Tools * State of IT * 2024 Report * IT Spend * Newsletters * Reviews Log In | Join * * My Feed * Unanswered Questions * My Posts * Community Guidelines * Events * More Categories * Cloud Computing & SaaS * Collaboration * Data Storage, Backup & Recovery * Hardware * Networking * Programming & Development * Security * Software * Vendors * Virtualization * Windows * All categories Tags * active-directory-gpo * dns * firewalls * microsoft-office-365 * powershell * printers-copiers-scanners-faxes * sap * vmware * water-cooler * windows-server * All tags * Spiceworks Community * Programming & Development PROPERLY FORMAT THIS DATE-TIME STAMP IN WIN BATCH SCRIPT? Programming & Development questionit-programming You have selected 0 posts. select all cancel selecting Jun 2017 1 / 5 Jun 2017 Jun 2017 David Alliedavidallie2562Poblano Jun 2017 Hello. I’ve got a batch file that runs a robocopy job, saving the output to a log file, that then gets renamed, appending the date-time to in the following format: logfilename_YYYYMMDD_HHmmss As I have several different robocopy jobs running sequentially in this batch file, I need to use a function to return the correctly formatted date-time stamp. I’ve almost got it, but what I’m getting is this: Success: 201762_094213 What I want to see is: Success: 20170602_094213 Here is my test batch script: :: getDateTimeStamp.bat :: :: Get the current date-and-time, returning it in the format: YYYYmmdd_HHmmss :: @echo off set myDate="" call:getDateTime myDate echo.Success: %myDate% goto:eof ::--------------- ::-- FUNCTIONS start below ::--------------- :getDateTime :: returns a unique string based on a date-time format: YYYYmmddHHmmss SETLOCAL for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do ( for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do ( set %%a=%%A&set %%b=%%B&set %%c=%%C)) set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100" for /f "tokens=1-4 delims=:. " %%A in ("%time: =0%") do ( set myDate=%yy%%mm%%dd%_%%A%%B%%C ) ENDLOCAL & IF "%~1" NEQ "" (set %~1=%myDate%) ELSE echo. Failure: %myDate% exit /b :eof My result is correctly padding the time, but is not padding the month and day so they’re shown as two digits. Can someone point out how I need to correct my code? Best Answer by acesuvon in post #3 > I realize that since you’re invested in the batch script method, one line of > PowerShell code may not do you any good. Here’s how this would work in > PowerShell: Clear-Host $DesktopPath = "$($env:USERPROFILE)\Desktop" $TimeStamp > = Get-Date -Format 'yyyyMMdd@HH.mm.ss' $FromPath = Read-Host "Enter so… 4 * CREATED Jun 2017 * LAST REPLY Jun 2017 * 4 REPLIES * 328 VIEWS * 2 USERS * 9 SPICE UPS * 3 2 acesuvonJalapeno Jun 2017 Whoa. So much confusion over a simple date-time. In PowerShell: Get-Date -Format "yyyyMMdd_hhmmss" Boom. Done. Just use that and then call robocopy from PowerShell. 2 acesuvonJalapeno Jun 2017 I realize that since you’re invested in the batch script method, one line of PowerShell code may not do you any good. Here’s how this would work in PowerShell: Clear-Host $DesktopPath = "$($env:USERPROFILE)\Desktop" $TimeStamp = Get-Date -Format 'yyyyMMdd@HH.mm.ss' $FromPath = Read-Host "Enter source path" $ToPath = Read-Host "Enter destination path" robocopy $FromPath $ToPath /COPYALL /B /SEC /MIR /R:0 /W:0 /NFL /NDL /log:$DesktopPath\logfilename_$TimeStamp.txt /tee Invoke-Item "$DesktopPath\logfilename_$TimeStamp.txt" Read-Host "`nPress ENTER to exit" You will get nonsensical errors if you don’t run as Administrator, but that’s actually Robocopy’s error output, not PowerShell, so you’d get that in cmd too. I also chose to add periods in between the hours/minutes/seconds cause I think it looks neater, and you don’t have to tee if you don’t want to see the output in the shell either. Best Answer 2 David Alliedavidallie2562Poblano Jun 2017 Thanks @acesuvon, I also just found a combination .bat/.vbs set of scripts on ss64.com that did just what I wanted in the native command prompt. The getDate.bat script: :: getDate.bat :: :: Get the current date-and-time, returning it in the format: YYYYMMDD_HHmmss :: @echo off set myDateTime="" call:getDateTime myDateTime echo.Success: %myDateTime% goto:eof ::--------------- ::-- FUNCTIONS start below ::--------------- :getDateTime :: returns a unique string based on a date-time-stamp, YYYYMMDD_HHmm SETLOCAL for /f %%G in ('cscript /nologo getdate.vbs') do set _dtm=%%G set _yyyy=%_dtm:~0,4% set _mm=%_dtm:~4,2% set _dd=%_dtm:~6,2% set _hh=%_dtm:~8,2% set _nn=%_dtm:~10,2% set myDateTime=%_yyyy%%_mm%%_dd%_%_hh%%_nn% echo %myDateTime% ENDLOCAL & set %~1=%myDateTime% exit /b :eof and the accompanying getDate.vbs script: ' Syntax: ' CSCRIPT /nologo getdate.vbs Dim dt dt=now 'output format: yyyymmddHHnn wscript.echo ((year(dt)*100 + month(dt))*100 + day(dt))*10000 + hour(dt)*100 + minute(dt) This combo, merged into my backup script works fine. THAT SAID, I thank you! for taking the time to send me the Powershell equivalent. I am going to migrate these scripts over to Powershell once I’ve got at least two solid sets of backups for each set of folders (being backed up)—am going to use your code snippet for the date-time piece. Thank you, again! – David 1 acesuvonJalapeno Jun 2017 No probs. PowerShell is all I know, I’m just glad it was already around when I started doing Windows admin stuff. To me batch scripts look like what the printer spits out when it has the wrong driver! Reply NEW & UNREAD TOPICS Topic Replies Spice Views Activity What port is Invoke-Sqlcmd uses? spiceuser-lny6v Apr 26, 2023 Programming & Development questionpowershell 21 11 528 Apr 2023 Created folders from CSV lElOUCHE_79 lelouche79 May 14, 2023 Programming & Development questionpowershell 13 11 77 May 2023 Trying to extract data from a specific cell in Excel using Powershell JMcNaug jmcnaug Jun 9, 2023 Programming & Development questionpowershell 5 10 505 Jun 2023 how to make the “mark done” disappear depending on a condition in Odoo 14 it11168 Aug 6, 2023 Programming & Development questionit-programming 0 7 12 Aug 2023 PDQ Deploy copy database Nuno85 nmarques12 Apr 24, 2023 Programming & Development questionpowershellit-programming 3 6 28 Apr 2023 WANT TO READ MORE? BROWSE OTHER TOPICS IN PROGRAMMING & DEVELOPMENT OR VIEW LATEST TOPICS. CONNECT * Tech Vendors * Live Events * SpiceCorps Meetups * SpiceWorld NEED HELP? * FAQs * Support Forum * Community Guidelines RESOURCES * All Categories * Tech How-Tos * Scripts * IT Research * Product Reviews FOR TECH MARKETERS * Marketing Services * Demand Generation * Account Based Marketing * Data Capabilities * Contact Sales SPICEWORKS * About Us * Contact Us * News & Insights * Community * Tools & Apps * Aberdeen * Careers * Press/Media * * * * * Sitemap * Privacy Policy * Terms of Use * Cookie Policy * Accessibility Statement * Do Not Sell My Personal Information Copyright © 2006 — 2024 Spiceworks Inc. Invalid date Invalid date WE CARE ABOUT YOUR PRIVACY If you consent, we and our 816 partners can store and access personal information on your device to provide a more personalised browsing experience. This is accomplished through processing personal data collected from browsing data stored in cookies. You can provide/withdraw consent and object to processing based on a legitimate interest at any time by clicking on the ‘Manage Preferences’ button.Our Privacy Policy WE AND OUR PARTNERS PROCESS DATA TO PROVIDE: Store and/or access information on a device. Personalised advertising and content, advertising and content measurement, audience research and services development. List of Partners (vendors) Reject All I Accept Show Purposes