CMD.exe get first day of the month - date

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
)

Related

How can I compare two dates in batch?

I have a batch script to get two dates; one of a folder, and the current system date.
I want to specifically compare the two by seeing if the date of the folder is 10 minutes or less older than the current date. This essentially checks if the user has modified this folder at the most 10 minutes ago.
Here's my current code (not complete, but the base):
#echo off
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a/%%b/%%c)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b)
setlocal enabledelayedexpansion
if exist "C:\$Recycle.Bin" (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{recycle-bin-date}=%%a")
for %%a in ("!{recycle-bin-date}!") do (
Set "data=%%~ta"
)
popd
)
set date1=%mydate% %mytime%
set date2=!data!
echo Date 1 (Current): %date1%
echo Date 2 (Recycle): %date2%
pause
::We have the dates above, how do I achieve what I'm trying to do?
If anyone could help me here, I'd really appreciate it.
You CAN do date time math in pure batch but it is quite cumbersome.
(See Ritchie Lawrence batch function library)
I recommend to use PowerShell as a tool for this
PowerShell one liner:
[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes
Wrapped in a batch
#Echo off
For /f "usebackq" %%A in (`
Powershell -NoP -C "[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes"
`) Do Set "AgeMinutes=%%A"
Echo Age in minutes %AgeMinutes%

Batch file for listing all files from a date interval

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

Dated folder batch file does not work on 20th of the month?

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%"

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
)