is it possible to make a .bat file that would work like this :
Get current date in dd.mm.yyyy format
Change date to desired one
Wait 60s
Change date back to its original state
exit
I know i can get current date by echo %date% and i get it in format dd.mm.yyyy but I have no idea how change it through one command, "set date" it's not working for me. I also know I could change date by simple command "date" then enter date but don't know how to automatize it. Can anyone write something like this?
To change the date you need admin permissions !
You can do it like echo 05.12.15|date
You can see how you can see how to get the date in a specific format and here you can see few tricks how to wait in batch .
Here's the script - (run it with admin privileges !):
#if (#X)==(#Y) #end /* jscript commend
#echo off
set "desired_date=03.12.15"
:: execute jscript part and get the date in a specific format
for /f "tokens=1-3 delims=- " %%A in ('cscript //E:JScript //nologo "%~f0"') do (
set fdate=%%C.%%B.%%A
)
rem echo %fdate%
::change date
echo %desired_date%|date
::wait 60 seconds
typeperf "\IPv4\Datagrams Received/sec" -si 60 -sc 1 >nul 2>nul
::return date to its original state
echo %fdate%|date
exit /b 0
end of jscript commend*/
function GetCurrentDate() {
// Today date time which will used to set as default date.
var todayDate = new Date();
todayDate = todayDate.getFullYear() + "-" +
("0" + (todayDate.getMonth() + 1)).slice(-2) + "-" +
("0" + todayDate.getDate()).slice(-2) + " " + ("0" + todayDate.getHours()).slice(-2) + ":" +
("0" + todayDate.getMinutes()).slice(-2);
return todayDate;
}
WScript.Echo(GetCurrentDate());
Related
Let me ask how to increment date correctly in PowerShell.
We'd like to get date list in format 'yyyy-mm-dd' in designated period,
to pass them to other programs.
For example, when DateStart='2018-01-05' and DateEnd='2018-01-08', then the date list should be;
2018-01-05
2018-01-06
2018-01-07
2018-01-08
Currently, my script can work as long as the designated period in the same month, like above case, but can't work in different month.
#Get_DateRange.ps1
param(
[String] $DateStart,
[String] $DateEnd
)
# Variables Set
$StartDT = [Datetime]::ParseExact($DateStart,"yyyy-mm-dd",$null).AddDays(-1)
$EndDT = [Datetime]::ParseExact($DateEnd,"yyyy-mm-dd",$null)
# Return Date List during designated period
do
{
$StartDT = $StartDT.AddDays(1)
$StartDT.toString("yyyy-mm-dd")
} While ($StartDT -lt $EndDT)
As wrong case, when DateStart='2018-01-31' and DateEnd='2018-02-05', it only returned '2018-01-31'.
I understand obviously something wrong with my script, but so far I have no idea. Any advice would be appreciated again.
Thank you.
Additional Info:
We'd like to use above script in other batch like this;
FOR /F %%d IN ('%PS_EXE% -ExecutionPolicy ByPass -File "Get_DateRange.ps1" -DateStart %EXT_START_DT% -DateEnd %EXT_END_DT%') DO (
#ECHO %FILE_ID%,%%d,%TARGET_DB%,%TARGET_SCHEMA%,%TARGET_TABLE%,'%%d',gz,%TARGET_DB%,%TARGET_TABLE%>>%EXT_FILE_NAME%
)
OK looks like all that was really wrong with the yyyy-mm-dd should have been yyyy-MM-dd
function Get-Dates(){
param(
[String] $DateStart,
[String] $DateEnd
)
# Variables Set
$StartDT = [Datetime]::ParseExact($DateStart,"yyyy-MM-dd",$null).AddDays(-1)
$EndDT = [Datetime]::ParseExact($DateEnd,"yyyy-MM-dd",$null)
# Return Date List during designated period
While ($StartDT -lt $EndDT){
$StartDT = $StartDT.AddDays(1)
$StartDT.toString("yyyy-MM-dd")
}
}
Get-Dates -DateStart "2018-11-29" -DateEnd "2018-12-03"
returns
2018-11-29
2018-11-30
2018-12-01
2018-12-02
2018-12-03
i see that ArcSet pointed out the error - mm instead of MM.
here's another way to do it. [grin] this grabs the number of days between the two dates and gives the start date [item zero] and one date for each of the remaining days.
$DateStart = '2018-01-31'
$DateEnd = '2018-02-05'
# Variables Set
$StartDT = Get-Date -Date $DateStart
$EndDT = Get-Date -Date $DateEnd
$DayCount = ($EndDT - $StartDT).Days
# Return Date List during designated period
foreach ($DC_Item in 0..$DayCount)
{
$StartDT.AddDays($DC_Item).ToString('yyyy-MM-dd')
}
output ...
2018-01-31
2018-02-01
2018-02-02
2018-02-03
2018-02-04
2018-02-05
This question have the batch-file tag, so here it is a pure Batch file solution that is simple:
#echo off
setlocal EnableDelayedExpansion
set "DateStart=%1" & set "DateEnd=%2"
for /F "tokens=1-6 delims=-" %%a in ("%DateStart%-%DateEnd%") do (
set /A "Days=366*(%%d-%%a)+31*(1%%e-1%%b)+(1%%f-1%%c)+10, MM=1%%b-100"
)
set "D=%DateStart:-=%"
for /L %%# in (1,1,%Days%) do if !D! leq %DateEnd:-=% (
echo !D:~0,4!-!D:~4,2!-!D:~6,2!
set /A "newMM=^!(1!D:~6!-(130+(MM+MM/8)%%2-2*^!(MM-2)+^!(!D:~0,4!%%4))), lastMM=^!(1!D:~4,2!-112), newYYYY=newMM*lastMM"
set /A "MM+=newMM*(1-lastMM*12), D+=newYYYY*(20100-1!D:~4!) + ^!newYYYY*newMM*(100*MM+10000-1!D:~4!) + 1"
)
Example:
C:\Users\Antonio>test 2018-01-31 2018-02-05
2018-01-31
2018-02-01
2018-02-02
2018-02-03
2018-02-04
2018-02-05
If you want to use these results in a Batch file, then it is much simpler that all the code be in a single Batch file. Besides, this code run faster than the PowerShell-Batch combination... ;)
Hi I need help as I'm new to BatchScript.
I need to check if any file/folder within a directory has been modified in the last 15 mins.
Here's my logic :
Find last modified date
Find current date
Find if difference between these two is 15 mins.
I'm able to do the 1st 2 steps.I'm stuck with the third
Please help me to find the time difference between these 2 dates.
Or if there's a better/easier logic.
Here is my code:
#echo off
for /f %a in (' dir "D:\BatchFiles" /od/b/s/t') do set Date1= %~ta
echo The most recently created file is %Date1%
#echo off
for /f "delims=" %i in ('time /t') do set output=%i
#echo off
SET Date2= %DATE:~4,2%/%DATE:~7,2%/%DATE:~10,4% %output%
echo The current date is %Date2%
PAUSE
Here is a pure batch file that returns all files in a given directory of a given age, with the following restrictions:
dates are not resolved correctly, so if a file has been modified in the previous month, it might not be included in the result erroneously; in case the maximum age is to be defined in terms of days, a forfiles solution might be more reasonable;
files that have both characters ) and , in their paths will be missing in the output; this is because of a design flaw of the wmic command, which is used to retrieve locale-independent date/time information of the last modification of files;
To use the script -- let us call it max-aged-files.bat --, provide command line arguments like this:
max-aged-files.bat 15*60 "D:\BatchFiles"
The first argument is the maximal age of a file in terms of seconds; simple arithmetic expressions like 15*60 are understood. The second argument is the location and/or file pattern to apply for searching files; you can state a directory path like "D:\BatchFiles" here, or a file pattern like "*.bat", or a comination like "D:\BatchFiles\*.bat"; it you omit it, the current directory is used.
Here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* Please regard that this script cannot handle dates correctly!
rem so if current date and file date are in different months it fails! */
rem // Retrieve and prepare arguments:
set "MAXAGE=%~1" & rem // (maximum age of files in terms of seconds)
if defined MAXAGE (set /A "MAXAGE=%MAXAGE%+0") else (
>&2 echo ERROR: maximum age not specified! & exit /B 1
)
if %MAXAGE% GEQ 86400 (>&2 echo ERROR: maximum age exceeds range! & exit /B 1)
shift /1
set "LOCATION=%~1"
set "ATTR=%~a1"
set "ATTR=%ATTR:~,1%"
if not defined LOCATION (set "LOCATION=.\*.*") else (
if "%ATTR%"=="d" set "LOCATION=%LOCATION%\*.*"
)
rem /* Gather current date/time in standardised format
rem [like: `YYYYMMDDHHMMSS.UUUUUU+ZZZ`]: */
for /F "delims=" %%I in ('wmic OS GET LocalDateTime') do (
for /F "delims=" %%J in ("%%I") do set "CURRTIME=%%J"
)
rem // Extract `YYYYMMSS` portion from current date/time:
set "CURRDATE=%CURRTIME:~,8%"
rem // Extract `HHMMSS` portion from current date/time only:
for /F "delims=." %%T in ("%CURRTIME:~8%") do (
set "CURRTIME=%%T"
)
rem // Loop through all files at given location:
for %%F in ("%LOCATION%") do (
set "ITEM=%%~fF"
setlocal EnableDelayedExpansion
rem // Gather file date/time in standardised format:
for /F "delims=" %%E in ('
2^> nul wmic DataFile WHERE Name^="!ITEM:\=\\!" GET LastModified ^|^| ^
2^> nul wmic DataFile WHERE ^(Name^="!ITEM:\=\\!"^) GET LastModified
') do (
for /F "delims=" %%F in ("%%E") do set "FILETIME=%%F"
)
rem // Extract `YYYYMMSS` portion from file date/time:
set "FILEDATE=!FILETIME:~,8!"
rem // Extract `HHMMSS` portion from file date/time:
for /F "delims=." %%T in ("!FILETIME:~8!") do (
set "FILETIME=%%T"
)
rem // Compute date difference between file and current date:
set /A "DATEDIFF=CURRDATE-FILEDATE"
rem // Continue processing only if date difference is zero or one:
if !DATEDIFF! GEQ 0 if !DATEDIFF! LEQ 1 (
rem // Convert date difference to seconds:
set /A "DATEDIFF*=240000"
rem // Compute time difference, regarding also date difference:
set /A "TIMEDIFF=DATEDIFF+1!CURRTIME!-1!FILETIME!"
rem // Pad time difference to consist of 6 digits [like `HHMMSS`]:
set "TIMEDIFF=000000!TIMEDIFF!" & set "TIMEDIFF=!TIMEDIFF:~-6!"
rem // Convert time difference to seconds:
set /A "TIMEDIFF=1!TIMEDIFF:~-2!-100+60*(1!TIMEDIFF:~-4,-2!-100+60*(1!TIMEDIFF:~-6,-4!-100))"
rem // Return item if
if !TIMEDIFF! LEQ %MAXAGE% (
echo(!ITEM!
)
)
endlocal
)
endlocal
exit /B
I have used system date from %date% using this code
set d=20%date:~6,2%%date:~0,2%
Its output is say 201309
How can I get the same for date of 30 days back(so I want 201308)? like is there any operation like addition/substraction for dates? I tried
set /a "date2=%date%-30"
But it gives error :"Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021)." as cmd asumes a value is hexadecimal if it starts with 0.
Using this batch file you should get what you need:
The routines can do some extra date math - the batch file is after the first 9 lines and can be self contained.
#echo off
call datebatch today -30
set "var=%day:0,6%"
echo "%var%"
pause
goto :eof
:datebatch
:: Date foward & backward
#echo off
if "%~2"=="" (
echo to get todays date use call "%~n0" today 0
echo to get yesterdays date use call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using today's date
echo call "%~n0" today 0 -
echo.
pause
goto :EOF)
set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%
I have a very simple batch file I am trying to write.
If today is 03/13, I would like my batch file to output:
"I will call you back on 03/15"
So it will take the date and ADD 2 business days. So if it were 03/15:
"I will call you back on 03/19"
Eh, do not worry, it can be done in batch. :) Here is an implementation of mine (~150 lines of code).
Example
https://gist.github.com/DavidRuhmann/4666270
Usage
Do something like this to adjust for the weekends.
if "%Date:~0,3%"=="Thu" call :DaysAhead 4
if "%Date:~0,3%"=="Fri" call :DaysAhead 4
if "%Date:~0,3%"=="Sat" call :DaysAhead 3
if "%Date:~0,3%"=="Sun" call :DaysAhead 2
if "%Date:~0,3%"=="Mon" call :DaysAhead 2
if "%Date:~0,3%"=="Tue" call :DaysAhead 2
if "%Date:~0,3%"=="Wed" call :DaysAhead 2
Use JScript to calculate the date and you're golden.
#if (#X)==(#Y) #end /* (batch + jscript hybrid script init)
:: *** Batch script *****
#echo off
setlocal
for /f %%I in ('cscript /nologo /e:jscript "%~f0"') do (
echo I will call you back on %%I
)
goto :EOF
:: *** JScript script *****/
var dow = new Date().getDay();
var days = (dow > 4) ? 9 - dow : (dow == 4 ? 4 : 2);
var d = new Date(new Date().getTime() + (1000 * 60 * 60 * 24) * days);
WScript.echo((d.getMonth() + 1) + '/' + d.getDate());
If today is Thursday, the script will return the following Monday. Friday through Sunday, the following Tuesday. Monday through Wednesday, two days ahead. And you don't have to worry about locale, leap year, leap frogs, lunar cycles, etc.
The Batch file below do what you want:
#echo off
rem AddBusinessDays.bat date numOfDays
rem Antonio Perez Ayala
rem Convert the date to Julian Day Number + number of days
for /F "tokens=1-3 delims=/" %%a in ("%1") do (
set /A mm=10%%a %% 100, dd=10%%b %% 100, yy=%%c
)
set /A a=mm-14, jd=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075+2+%2, dow=jd%%7
rem Adjust Julian Day Number to avoid weekends
if %dow% lss 2 set /A jd+=2-dow
rem Convert Julian Day Number back to date
set /A l=jd+68569-2,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447,dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l
rem Assemble the result
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%
set newDate=%mm%/%dd%/%yy%
echo %newDate%
Reference: http://www.hermetic.ch/cal_stud/jdn.htm#comp
For example:
>echo %date%
03/13/2013
>AddBusinessDays.bat %date% 2
03/15/2013
>AddBusinessDays.bat %date% 3
03/18/2013
>AddBusinessDays.bat %date% 4
03/18/2013
>AddBusinessDays.bat %date% 5
03/18/2013
>AddBusinessDays.bat %date% 6
03/19/2013
Antonio
PS - Yes, I know that this method will not work for everyone in the world. The good news are that I not wrote this solution for they all, but precisely for the OP although it is very easy to modify it for every computer. For example, previous program does NOT work in my computer because my locale is DD/MM/YYYY, but I can interchange dd and mm in two lines of previous program and I am pretty sure that most people in this world are also capable to do so! ;-)
Hi Antonio,
Just today I found that the code you shared didn't work for more than 7 days ahead, I changed some thing and now it works ever.
#echo off
rem AddBusinessDays.bat date numOfDays
rem adapted by Leonardo Contreras based on Antonio Perez Ayala with handling for more than 7 days
rem to convert Dow mm/dd/yyyy to mm/dd/yy
set mydate=%date:~4,2%/%date:~7,2%/%date:~10,4%
rem Convert the date to Julian Day Number + number of days
for /F "tokens=1-3 delims=/" %%a in ("%mydate%") do (
set /A mm=10%%a %% 100, dd=10%%b %% 100, yy=%%c
)
rem dow_orig is based on a 0-6 days statrting on Monday
set /A a=mm-14, jd_orig = (1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075, dow_orig=jd_orig%%7
rem calculate new julian's day
set /A numw = (dow_orig+%3)/5, njd = jd_orig+%3+numw*2, dow=(njd)%%7
rem Adjust Julian Day Number to avoid weekends
if %dow% gtr 5 (set /A jd+=2 )
rem Convert Julian Day Number back to date
set /A l=njd+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447,dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l
rem Assemble the result
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%
set newDate=%mm%/%dd%/%yy%
echo %newDate%
Hi I cant get the below script ive worked on to pickup the extension of the files, Can any help me out by pointing where I have gone wrong?
dim fileSystem, folder, file
dim path
dim count : count = 0
path = "C:\temp"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) & File.name = "txt" then
count = count + 1
end if
Next
if count < 4 then
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T ERROR /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found please check offsite copy " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 1001 )
Else
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T Information /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found offsite is working fine " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 0 )
End if
File.name is the full name including the extension, to test for the extension;
if ... fileSystem.getExtensionName(file.name) = "txt" then
You also want the logical And not the bitwise concatenating & in their too.
Alex's answer is the one you want, but just for reference if you were working just with vbs and a string filename, without the filesystemobject collection you could achieve the same via:
Right(strFilename, Len(strFilename) - Instrrev(strFilename, "."))
This essentially finds the position of the final "." in the filename, takes this away from the length of your filename, and then gives you however many character's that equals from the right hand side. This could be amended slightly to use the "Mid" command rather than the "Right" but I don't think it matters too much in a case like this.