How do I add the current weeknumber to my foldername

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dieger
Posts: 2
Joined: 29 Apr 2016 07:16

How do I add the current weeknumber to my foldername

#1 Post by dieger » 29 Apr 2016 07:29

Hi, since I am relatively new to batch files and this forum, I will explain what I am trying to do.

I am trying to create a bat script that will create a new folder starting with the current date, some text, followed by the current week number.

I am using the script below to make the new directory with the current date and some text:

Code: Select all

@echo off
echo Get the current date and time in YYYYMMDD format
cls
echo.
SET isodt=%date:~9,4%%date:~6,2%%date:~3,2%
set date=%isodt%
echo.
mkdir "%isodt% Foldername"


This part is working fine. But instead of "20160429 Foldername" I would like to have "20160429 Foldername 17". Where 17 is the current week based on the current date.

I did find the script below which seems to give me the correct week. But how do I incorporate this in the script above so the foldername includes the 17? Or is there an easier way to do this?

Code: Select all

REM @echo off 
REM Set needed options
setlocal ENABLEEXTENSIONS
setlocal ENABLEDELAYEDEXPANSION
REM Extract date components from current date
set /a MM=%DATE:~6,2%
set /a DD=%DATE:~3,2%
set /a YYYY=%DATE:~9,4%
set YY=%DATE:~11,4%
REM Get Weeknumber
call :WeekNumber %MM% %DD% %YYYY%
echo %Week%
pause
goto :Exit
:WeekNumber
REM Calculate an offset based on day of week January 1 fell on
set /a Offset=%3-1900
set /a Offset=((%Offset%+(%Offset%/4))%% 7)+6
REM Find out what day of the year (from January 1) we are on
call :DayNumber %1 %2 %3
REM Add offset and then divide by 7 days per week
set /a Week=(%Day%+%Offset%)/7 -1
goto :Exit
:DayNumber
REM Incrementally build up dy number from month and day
set /a Day=0
goto :DayNumber%1
:DayNumber12
set /a Day+=30
:DayNumber11
set /a Day+=31
:DayNumber10
set /a Day+=30
:DayNumber9
set /a Day+=31
:DayNumber8
set /a Day+=31
:DayNumber7
set /a Day+=30
:DayNumber6
set /a Day+=31
:DayNumber5
set /a Day+=30
:DayNumber4
set /a Day+=31
:DayNumber3
set /a Day+=28
REM Add an extra day on leap years
set /a Leap=%3 %%4
if %Leap% == 0 set /a Day+=1
:DayNumber2
set /a Day+=31
:DayNumber1
set /a Day+=%2
goto :Exit
:Exit

I would appreciate any help.

Kind regards, dieger

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How do I add the current weeknumber to my foldername

#2 Post by aGerman » 30 Apr 2016 07:04

Don't reinvent the wheel.
Ritchie Lawrence wrote a plenty of date and time functions.
http://www.commandline.co.uk/lib/treeview/index.php?contents.php&../Batch%20Function%20Library/Date%20and%20Time%20Functions/DateToWeek.html

Code: Select all

@echo off &setlocal

call :DateToWeek 2016 04 29 yn cw dw
echo %yn% %cw% %dw%

pause
exit /b

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DateToWeek %yy% %mm% %dd% yn cw dw
... etc. ...


Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: How do I add the current weeknumber to my foldername

#3 Post by sambul35 » 30 Apr 2016 07:26

@dieger

Without testing if the script you found to calculate a week number (%week%) actually works, just to show an example, how to incorporate another script into your code as a function (needs testing and editing by YOU :D ):

Code: Select all

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
echo Getting current date and time in YYYYMMDD format, calculating week number
set "isodt=%date:~9,4%%date:~6,2%%date:~3,2%"
call :weekcount
echo. & echo Creating a date stamped folder
mkdir "%isodt% Foldername %week%" & pause
endlocal
exit /b

:weekcount
(script you found goes here)
(goto) 2>nul


You may need to modify the script you found to replace :call statements in it with :goto or such, and use !some_var! delayed expansion variables instead of %some_var% where needed.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: How do I add the current weeknumber to my foldername

#4 Post by Compo » 02 May 2016 07:54

You could always utilise Powershell:
example.cmd

Code: Select all

@Echo Off
SetLocal EnableExtensions
Set "_PSC=PowerShell -ExecutionPolicy Bypass -Command"
For /F "UseBackQ Tokens=*" %%A In (
   `%_PSC% "Get-Date -UFormat '%%Y%%m%%d %~1 %%V'"`) Do Echo=MD "%%A"
Pause
Just run the script with FolderName as the parameter.
when happy with output remove last line and Echo= from the line above it

dieger
Posts: 2
Joined: 29 Apr 2016 07:16

Re: How do I add the current weeknumber to my foldername

#5 Post by dieger » 03 May 2016 01:23

Unfortunately I did not get the scripts to work. Must be my lack of knowledge. I did get the Powershell script to work in my batch script. Thank you Compo!

Post Reply