Script to add day not date to filename - powershell

I need to create a script batch, PowerShell or VB to add the day of the week to the filename.
For example, there are 4 files and all need to have MON apended to the front on Mondays, TUE on Tuesdays, WED on Wednesdays, etc.
Can anyone assist with this please?

$dow = (Get-Date -f ddd).ToUpper()
$fileName = "${dow}_your_file_name.txt "
THU_your_file_name.txt

Use the VBScript Docs or Google for details on the Weekday() and WeekdayName() functions used in:
Today = Date()
DayNum = Weekday(Today)
DayName = WeekdayName(DayNum, True)
WScript.Echo UCase(DayName) & "_" & "somefile.txt"
THU_somefile.txt
PS:
Start here: Functions (VBScript)

Well, there's an answer for powershell and one for VBScript. Here's one for Windows cmd batch.
#echo off
setlocal
for /f "tokens=5" %%I in ('find "" "%date:~0,3%" 2^>^&1') do set day=%%I
ren "oldfile.txt" "%day%_oldfile.txt"
Explanation
:: DIRECTIVE RESULT
:: --------------------------------------------------------
:: %date% Thu 02/07/2013
:: %date:~0,3% Thu
:: find "" "Thu" error stating "File not found - THU"
:: --------------------------------------------------------
Then all that remains is to redirect the error from stderr to stdout and scrape the fifth token.
(source of idea to use find to convert to upper case)

if you choose VBScript, be careful with WeekDayName function as it related to regional language settings. For example, Friday is Петък (in Bulgarian), so in my system WeekDayName with abbreviate set to True will return Пт, not Fri.
WScript.Echo WeekDayAbbrENG()
Function WeekDayAbbrENG()
Dim WDs
WDs = Split("SUN MON TUE WED THU FRI SAT")
WeekDayAbbrENG = WDs(Weekday(Now) - 1)
End Function
[EDIT] Actually, the same problem appear with Get-Date in PowerShell.

Related

Batch - Get the date from 8 days from now [duplicate]

This question already has answers here:
Subtract days in batch file
(3 answers)
How to get 3 days past date from current date Using Batch file
(2 answers)
Date arithmetic in cmd scripting
(6 answers)
Windows console %DATE% Math
(6 answers)
Closed 2 years ago.
I'm trying to get the date from 8 days from now to navigate in a tree structure.
I've use this but I got a problem at the start of the new month (I got a negative number and thats logical) :
set annee=%date:~6,4%
set mois=%date:~3,2%
set jour=%date:~0,2%
set /a j8=%jour%-8
cd %annee%\%mois%\%j8%
Got you a solution to resolve my problem ?
Thanks in advance.
The easiest way will be with a polyglot script that embeds jscript.
Here's the dayAdder.bat that accepts only one argument - the days you want to add to the current date and prints the result:
#if (#X) == (#Y) #end /* JScript comment
#echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
var days=parseInt(WScript.Arguments.Item(0));
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = new Date();
WScript.Echo(date.addDays(5));
WScript.Echo("Year: " + date.getFullYear());
WScript.Echo("Month: " + date.getMonth());
WScript.Echo("DayOfTeWEek: " + date.getDay());
examaple:
E:\scripts>dayAdder.bat 7
Sun Nov 8 16:27:48 UTC+0200 2020
Year: 2020
Month: 10
DayOfTeWEek: 2
DayOfTheMonth: 3
You can modify it in way that will be suitable for you.

How to increment date ('yyyy-mm-dd') in different months period correctly in Power Shell

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... ;)

Simple time changing .bat

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());

Gnuplot prints a strange year, 30 years later

I have the next problem with gnuplot, when I print the time gnuplot
prints de time+30years.
This is a part of my data:
1411336800,1390,0,0,0,10,1411,0,10,0,0,0,0,0,1411
1411340400,1506,0,0,0,10,969,0,10,0,0,0,0,0,969
1411344000,1115,0,0,0,10,1108,0,10,0,0,0,0,0,1108
1411347600,719,0,0,0,10,712,0,10,0,0,0,0,0,712
A part of the script is:
set timefmt "%s"
stats "<tail -1 uur.txt " using 1:2 nooutput
tijd = strftime("%d %B %Y %H:%M", STATS_max_x)
print tijd
And then gnuplot prints: 21 September 2044 01:00. 44 ?
Has some one a clue?
I tried several formats but nothing helped.
Until version 4.6, internally gnuplot uses the 1. January 2000 as reference for its date and time functions (in version 5.0 the standard Unix timestamp is used).
You shouldn't have any problems with set timefmt "%s" if you plot the data. But when using strftime it makes a difference. Since you're using tail anyway, you can simply use
tijd = system('date -d #$(tail -1 uur.txt | cut -d, -f1) +"%d %B %Y %H:%M"')
print tijd

Calculate business days using a batch file

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%