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
Related
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
)
I need to do a batch file which order all files by a date interval, example:
#echo off
echo Input the date(dd/mm/yyyy):
set /p compDate=
::After that I will compare from the actual day (%date%), example:
set interval = %compDate% - %date%... *Something like that*
::After that I need to list all files from a specific directory, example:
echo Input the directory:
set /p directory=
SET Exit= %UserProfile%\Desktop\test.txt
::After that I might need dir /tc to get the creation date, example:
pushd "%directory%"
dir /s /tc /a-d > %Exit%
::After that I don't know how to get only the lines which are in date interval, example:
Today is 19/08/2014, but I want to search all files created from day 10/07/2014.
So I have to copy all lines which have the date 10/07/2014, 11/07/2014, 12/07/2014 and so on until stop on today created files.
I tried with findstr, but I can't set the date interval, just a specific date to search in the .txt created.
Somebody know how to do that?
If I correctly understood the request, you really don't want files created in a given interval, but files created after a given date. The Batch file below assume that the date used by the system appear in DD/MM/YYYY order:
EDIT: Some modifications as reply to the comments
#echo off
setlocal EnableDelayedExpansion
echo Input the date(dd/mm/yyyy):
set /p compDate=
for /F "tokens=1-3 delims=/" %%a in ("%compDate%") do set compDate=%%c%%b%%a
echo Input the directory:
set /p directory=
SET Exit=%UserProfile%\Desktop\test.txt
pushd "%directory%"
(for /F "tokens=1-5*" %%a in ('dir /s /od /tc /a-d') do (
set "fileDate=%%a"
if "!fileDate:~2,1!!fileDate:~5,1!" equ "//" (
for /F "tokens=1-3 delims=/" %%x in ("!fileDate!") do set fileDate=%%z%%y%%x
if !fileDate! geq %compDate% (
set "fileSize= %%e"
echo %%a %%b %%c %%d !fileSize:~-16! %%f
)
)
)) > %Exit%
popd
A solution that uses WMIC and is independent from time/date settings:
#echo off
setlocal
set /p compDate=Input the date(yyyymmdd):
set /p directory=Full directory path (with no slash at the end):
set exit_file= %UserProfile%\Desktop\test.txt
break>%exit_file%
for /f "tokens=1,2 delims=:" %%a in ("%directory%") do (
set "dir_drive=%%~a:"
set "dir_path=%%~b\"
)
set dir_path=%dir_path:\=\\%
setlocal enableDelayedExpansion
for /f "useback skip=1 tokens=1,2* delims=:" %%f in (`" wmic datafile where (drive='!dir_drive!' and path like '%dir_path%') get CreationDate^,name"`) do (
set creation_date=%%f
set creation_date=!creation_date:~0,8!
set "file_name=%dir_drive%%%~g"
if 1!creation_date! GTR 1%compDate% (
echo !file_name!>>%exit_file%
)
)
exit /b 0
I am new to this and have created a batch file that creates a current dated folder with a user entered title.
Unfortunately it does not create the year for the 20th of the month.
e.g. On the 19th it will create "140119 - Test" but on the 20th "0120 - Test"
Any ideas what is causing it?
It must be the Delims that is causing the issue but I don't know how to get around it.
#echo off
#REM Setups %date variable
#REM First parses day, month, and year into dd , mm, yyyy formats and then combines to be YYMMDD by removing the 20
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET dd=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/20 " %%A IN ('echo %CDATE%') DO SET yy=%%B
SET date=%yy%%mm%%dd%
call :inputbox "Please enter the folder name (Excl. Date):" "Standard Date folder"
exit /b
:InputBox
set input=
set heading=%~2
set message=%~1
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set input=%%a
if "%input%"=="" (exit /b)
mkdir "%date% - %input%"
exit /b
Your fundamental issue is that the delims= phrase estalishes a set of delimiters, not a delimiter string. Any character between the = and " becomes a delimiter.
#ECHO OFF
SETLOCAL
REM Setups %date variable
REM First parses day, month, and year into dd , mm, yyyy formats and then combines to be YYMMDD by removing the 20
FOR /F "TOKENS=1-4 DELIMS=/ " %%A IN ("%date%") DO ECHO A=+%%A+ B=+%%B+ C=+%%C+ D=+%%D+
FOR /F "TOKENS=1-4 DELIMS=/ " %%A IN ("%date%") DO SET dd=%%B&SET mm=%%C&SET yyyy=%%D
SET yy=%yyyy:~-2%
SET xdate=%yy%%mm%%dd%
ECHO xdate=%xdate%
GOTO :EOF
#echo off turns off command-echoing. Having executed that instruction, there is no requirement for further #instruction commands. # simply turns off instruction-echoing for that one instruction.
You don't tell us what your date-format is, but I conclude that it's dayname dd/mm/yyyy. This is fundamental - personally, I use dd/mm/yyyy but many Overflowers would use dayname mm-dd-yyyy. Always a problem area - don't let's get started on time.
%date% is a magic variable, set by the system to potatoes according to the user's selected format-settings. Similarly, %time% is set to carrots. If you set a user-variable of the same name, then the user-variable overrides the system-set variable.
In the above, I've used SET yy=%yyyy:~-2%. To substring in batch, you need SET var=%bigstringname:~start,length% where length is optional and if missing means the remainder of the string afterstart.Startmay be 0 or positive meaningthis number of characters after the start of the string (or character-position, starting a position '0'). If negative, it means this number of characters from the end of the string.
the result depends on your systems locale:
C:\Users\User\TEST>date /t
20/01/2014
C:\Users\User\TEST>for /f "tokens=1-3delims=/ " %a in ('date /t') do #echo(%c%b%a
20140120
C:\Users\User\TEST>for /f "tokens=1-3delims=/ " %a in ('date /t') do #set "mydate=%c%b%a"
C:\Users\User\TEST>echo %mydate%
20140120
C:\Users\User\TEST>set "mydate=%mydate:~2%"
C:\Users\User\TEST>echo %mydate%
140120
Please note the trailing space : 20/01/2014_. You should not use system variables for local variables, ex date.
if this is an option for you then this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher, using WMIC.
#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=%YY%%MM%%DD%"
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
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
)