Format date in Windows 7 batch job - date

I'm trying to use the current date in a Windows 7 batch job. The batch job opens multiple files which have today's date appended to them.
Example:
start \\\Directory_Name\Rpts\20130801\0000A060_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\0000P083_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\00007P12_FileName_20130801.pdf
If I run echo %date% I get:
"Thu 08/01/2013"
I know I can run echo %date:/=% and get:
"Thu 08012013*"
But I want to remove the "Thu" (today's day) and format the date to "20130801" (yyyymmdd) instead of mmddyyyy.
So eventually the open file command would look like the following with the correct %date% command inserted: start \\\Directory_Name\Rpts\%date%\00007P12_FileName_%date%.pdf
Anyone know how I can do this?

A robust, region insensitive method:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a"
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%"
set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

This is a bit simpler of a way of doing it with substrings:
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%

Here is a solution, that is independent of local time format:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
and then %datetime:~0,8% will give you your YYYYMMDD

Try this. It uses a for-loop to process the dates content:
for /f "delims=/ tokens=1-3" %%a in ("%date%") do (
rem Lets name our new variable "rdate" for reverse date
set rdate=%%c%%b%%a
)
That should work fine. Just call it as %rdate%.
Hope this helped, Mona

I use it to change the date temporarily.
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~0,2%-%buildDate:~3,2%-%buildDate:~6,4%
net session >nul 2>&1
if %errorLevel% == 0 (
goto check_Permissions
)
echo Permissions Administartor!!!
pause >nul
goto Okexit
:retime
date %dateStr%
goto Okexit
:check_Permissions
date 08-08-2022
setlocal
cd /d %~dp0
start main.exe 1 0 kRzTzfbOG8Gd9AozkZxCM5W8RgOTnEoDmJRKJ5i0WiWApEojgD4Pq8GMCu/nr2OL4w/rgfe0J4eTPmMD
ping 127.0.0.1 -n 3 >nul
goto retime
:Okexit

Related

Using %date% in batch script

I want to have some code like this:
if %date% equ "Mon" echo do this do that
but the cmd window closes after encountering this code, even if I put
pause
after it.
How do I fix this?
Here's a complete cmd file that will give you what you need. The important bit is all in the getDow function and, hopefully, it's commented well enough to understand. First, the test harness:
#echo off
rem Test harness bit - just get current date and compare with getDow.
date /t
call :getDow num long short
echo Day of week is %num%, %long%, %short%
goto :eof
The function itself is:
rem Usage: call :getDow <num> <long> <short>
rem <num> will receive the numeric form (0-6).
rem <long> will receive the long form (e.g., Monday).
rem <short> will receive the short form (e.g., Mon).
:getDow
rem Create local scope to prevent information leakage.
setlocal enableextensions enabledelayedexpansion
rem Create array for translation.
set idx=0
for %%a in (Sunday Monday Tuesday Wednesday Thursday Friday Saturday) do (
set dow[!idx!]=%%a
set /a "idx += 1"
)
rem Get the numeric day of week, mmi command will
rem output 'DayOfWeek=N' and we just extract N.
for /f "tokens=2 delims==" %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek /value ^| findstr "DayOfWeek="') do (
set localNum=%%a
)
set localStr=!dow[%localNum%]!
rem Properly end scope but let selected information leak.
endlocal&&set %1=%localNum%&&set %2=%localStr%&&set %3=%localStr:~0,3%
goto :eof
A sample run of that script gives:
Tue Jun 05
Day of week is 2, Tuesday, Tue
You probably want to use:
IF /I "%DATE:~,3%"=="Mon" (Echo Do this
Echo Do that)
Or possibly:
IF NOT "%DATE:Mon=%"=="%DATE%" (Echo Do this
Echo Do that)
However neither of those are safe or robust methods in anything other than your specific current user environment.
This is how I'd get the day of the week into a variable using a batch file with WMIC:
For /F %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek') Do For %%B In (
Monday.1 Tuesday.2 Wednesday.3 Thursday.4 Friday.5 Saturday.6 Sunday.0
) Do If "%%~xB"==".%%A" Set "WDName=%%~nB"
Line 2 can be optionally adjusted to start with Sunday.0 Monday.1 etc. if necessary or Lunes.1 Martes.2 etc. depending upon your language.
You could then use:
If "%WDName%"=="Monday" (Echo Do this
Echo Do that)
Although (Get-Date).DayOfWeek in PowerShell seems so much simpler.

CMD.exe get first day of the month

Using cmd batch file can i get first day of month and run an action?
For example every first day of the month shutdown the pc.
Thanks in advance!
for german date format: echo 01%date:~2%. For american format: echo %date:~0,3%01%date:~5%
or use a language independent solution:
for /F "delims=" %%i in ('wmic path Win32_LocalTime get month^,year /value^|find "="') doset /a %%i
set DatTim=01.%month%.%year%
REM adapt to your needed format
or in your special case:
for /F "delims=" %%i in ('wmic path Win32_LocalTime get day /value^|find "="') do set /a %%i
if %day%==1 (
rem your commands
)

How to change format for %date% in command prompt?

How do I can change the output format of echo %date% in command prompt? In one system I receive the output as Wed 02/12/2014 but in another system I receive the output as 02/12/2014. Command I am typing on both the systems is echo %date%.
What I basically need to find out is Day of Week. So if can’t change the format then is there any other command in the command-line to get the Day of week?
This solution was posted recently and should work the same on any PC after XP Home.
#echo off
set "daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
for /F "skip=2 tokens=2 delims=," %%a in ('wmic Path Win32_LocalTime Get DayOfWeek /Format:csv') do set "daynumber=%%a"
for /F "tokens=%daynumber% delims=," %%b in ("%daysofweek%") do set "dow=%%b"
echo "%dow%", "%daynumber%"
pause

Command Prompt/Bat file - Create new folder named with today's date

I use the following code to create a new folder that is named with todays date:
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir c:\%date:/=%
Now the format is as follows:
20130619
How do I change the format to?:
2013_06_19
Thank you
%date% depends on your computer settings, and locale. Here is a reliable way to get a date and time stamp. Win XP pro and above.
If you need to use your batch file on unknown machines then this is worth using.
:: time and date stamp YYYYMMDD, HHMMSS and YYYY-MM-DD_HH-MM-SS
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
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%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
Do this:
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1="%%k"_"%%i"_"%%j"
mkdir %yyyymmdd1%
or simply
SET Today=%Date:~10,4%_%Date:~7,2%_%Date:~4,2%
echo %today%
outputs
2013_06_19
Press any key to continue . . .
Then you could easily use the variable today for directory creation e.g.:
mkdir %today%
EDIT: YYYY_MM_DD format

Batch file to rename all files in a folder by adding current date/time to beginning of filename

I can't seem to understand how batch files add yyyy/mo/dd/hh/mm/ss to the beginning of filenames. (Using Windows 7) Accuracy to the second is important.
It doesn't actually have to be a batch file, it just has to be a small program which can be executed by Directory Monitor whenever I add files to a folder: http://brutaldev.com/page/Directory-Monitor.aspx
I only imagine that a batch file would be the simplest and most efficient approach, but any other suggestions are welcome.
I work with many sequentially numbered files with overlapping filenames and I need a quick way to rename them whenever I add them to a folder such that there will never be any file with the same name yet they will still remain in sequential order. This is how I thought of adding the current date and time to the beginning of the filename and why seconds are important, since I can easily add multiple sets to a folder in under a minute but certainly not under a second. It would be ideal if the batch file could ignore file extensions and simply add the current date/time to the beginning of any file added to the folder.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
#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%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
:: this line will rename the files in the current folder which haven't already
:: been renamed by checking for the fullstamp format at the start of the line
:: but it will skip this batch file
for /f "delims=" %%a in ('dir /b /a-d ^|findstr /v "^[0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]*" ') do if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"
pause
#ECHO off
SETLOCAL
IF [%1] NEQ [] goto s_start
:: Author - Simon Sheppard, July 2003
:: Tested for Windows NT, 2K, XP
ECHO STAMPME.cmd
ECHO REName a file with the DATE/Time
ECHO.
ECHO SYNTAX
ECHO STAMPME TestFile.txt
ECHO.
ECHO STAMPME "Test File.txt"
ECHO.
ECHO STAMPME "c:\docs\Test File.txt"
ECHO.
ECHO In a batch file use CALL STAMPME ...
:: To change the filename format just change around the last line below
GOTO :eof
:s_start
SET _file=%~n1%
SET _pathname=%~f1%
SET _ext=%~x1%
::Get the date
:: note ISO 8601 date format would require 4 digit YYYY Year)
FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
SET _mm=%%G
SET _dd=%%H
SET _yy=%%I
)
:: Get the time
FOR /f "tokens=2-4 delims=:." %%G IN ('cmd /c "time<nul"') DO (
SET _hr=%%G
SET _min=%%H
SET _sec=%%I
GOTO :done
)
:done
ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
ECHO The time is: [%_hr%]:[%_min%]:[%_sec%]
REN "%_pathname%" "%_hr%-%_min%-%_sec%#%_file%%_ext%"
This seems to work for me
I'd prefer solutions, that are not dependent to local settings (wmic gives always the same format):
#echo off
setlocal EnableDelayedExpansion
for %%a in (a.*) do (
for /f %%i in ( 'wmic os get localdatetime /value ^|find "Local"' ) do set %%i
set ldt=!LocalDateTime:~0,4!-!LocalDateTime:~4,2!-!LocalDateTime:~6,2!-!LocalDateTime:~8,2!-!LocalDateTime:~10,2!-!LocalDateTime:~12,2!-!LocalDateTime:~15,3!
echo seconds ### ren %%a !LocalDateTime:~0,14!%%a
echo milliseconds ### ren %%a !LocalDateTime:~0,18!%%a
echo with separator ### ren %%a !ldt!-%%a
)