Solved

How to get the 'Day of the Week' ie) monday, tuesdat etc... into a variable in batch script

Posted on 2007-03-23
3
310 Views
Last Modified: 2013-12-26
Hi,

I need to set a variable to the actual day of the week like Monday, Tuesday etc.... then on the very last line of my code I need to put that variable in place of FRIDAY which is hard coded.  How can I do that?  Here is my code:

for /F "tokens=2-4 delims=/- " %%A in ('date /t') do (
set MM=%%A
SET DD=%%B
SET YYYY=%%C
)

mkdir F:\cognos\backups\%YYYY%%MM%%DD%
cd F:\cognos\backups\%YYYY%%MM%%DD%

REM Erase all the .bak, .xml and .lae files in the F:\cognos\backups\%YYYY%%MM%%DD%. Needed in case backup is run twice.

erase F:\cognos\backups\%YYYY%%MM%%DD%\*.bak /S/Q/F
erase F:\cognos\backups\%YYYY%%MM%%DD%\*.xml /S/Q/F
erase F:\cognos\backups\%YYYY%%MM%%DD%\*.lae /S/Q/F

REM Stop the IWR Service as it cannot be running for next two steps
net stop "Cognos IWR Service Manager (cer3)"

REM First create the iwrdbbackup.bak file with the osbackup command
f:\odi\ostore\bin\osbackup -f f:\cognos\backups\%YYYY%%MM%%DD%\iwrdbbackup.bak -i f:\cognos\backups\%YYYY%%MM%%DD%\inc.lst -I "f:\cognos\backups\import_list.txt"

REM Next create the iwrdbbackup.xml file with the dsdump command
"f:\Program Files\cognos\cer3\bin\dsdump.exe" -db "F:\Program Files\Cognos\cer3\iwr\database\iwr70.db" -fn f:\cognos\backups\%YYYY%%MM%%DD%\iwrdbbackup.xml -v all -l "f:\cognos\backups\logfile.txt"

REM Copy the .db and the .pkg files
xcopy "F:\Program Files\cognos\cer3\iwr\database\"*.db F:\cognos\backups\%YYYY%%MM%%DD%\*.* /Y

REM xcopy F:\Program Files\cognos\cer3\iwr\database\*.pkg F:\cognos\backups\%YYYY%%MM%%DD%\*.* /Y

REM Start the IWR Service
net start "Cognos IWR Service Manager (cer3)"

REM Create the UpfrontStore.bak file. Cognos IWR Service can be running
"f:\Program Files\Cognos\cer3\bin\upfdbadm.exe" -b f:\cognos\backups\%YYYY%%MM%%DD%\UpfrontStore.bak

call setenv.cmd

%costar_cog_home_drive%

cd %costar_cog_home_path%\scripts\Backup Jobs

call "BACKUP_LAE.MCX"
if errorlevel 1 goto err

REM call CleanupDistributedReports.MCX
goto ok

:err
Echo Process Failed at %process_loc%
exit 1

:ok
Echo Process completed Successfully


xcopy F:\cognos\backups\%YYYY%%MM%%DD%\*.* F:\cognos\backups\Cognos7_Daily_Backups\Friday\*.* /Y
0
Comment
Question by:elwayisgod
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 50 total points
ID: 18783115
Here's some code. Please check the spellings on the days of the week.

@echo off

setlocal

call :GETDATEPARTS "%date%"

call :GETDAY %mm% %dd% %yy%

echo Today is: %day%

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:GETDAY

if "%~3"=="" echo GETDAY requires 3 parameters: mm dd yy (4 digit)&goto :EOF

set wmm=%1
set wdd=%2
set wyyyy=%3

if 1%wmm% GTR 100 set /A wmm=1%wmm% - 100
if 1%wdd% GTR 100 set /A wdd=1%wdd% - 100

if %wmm% LSS 3 set /a wmm+=12&set /a wyyyy-=1

set /A dayNum=%wdd% + (2 * %wmm%) + (3 * (%wmm% + 1) / 5) + %wyyyy% + (%wyyyy% / 4) - (%wyyyy% / 100) + (%wyyyy% / 400) + 1
set /A dayNum=%dayNum% %% 7

if %dayNum%==0 set day=Sunday
if %dayNum%==1 set day=Monday
if %dayNum%==2 set day=Tuesday
if %dayNum%==3 set day=Wednesday
if %dayNum%==4 set day=Thursday
if %dayNum%==5 set day=Friday
if %dayNum%==6 set day=Saturday

goto :EOF

Good Luck,
Steve
0
 

Author Comment

by:elwayisgod
ID: 18783526
Is that as short as the code can get?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18783585
yup
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question