Adding Beginning and End of Month to a filename in a task script

The following task script has been copied and modified from a past Experts Exchange solution I received a few months back.  I want to add the beginning and End of Month tags to the file name when I create my text file in the target path.  I copied the coding to create the EOM variable from the task I have that has been working.  Then I added the %EOM% to the end of the filename before the file extension.

When I run the task the EOM of month is not showing up in the file name.

Since I am having trouble with the script I thought I would also ask for help in the syntax to get the Beginning of the Previous Month's date.

The format for the resulting filename is as follows:


FilePath\Filename_YYYYMMDD_YYYYMMDD.txt

net use D:\DTS






copy  D:\DTS\MedAssets\MedAssets_CDM.txt  "\\pmcfs\groups\MedAssests\78017_CDM_6390.txt"
copy  D:\DTS\MedAssets\MedAssets_RevUsage.txt  "\\pmcfs\groups\MedAssests\78017_CDM_USAGE_1721_%eom%.txt"



net use D:\DTS /DELETE


goto :eof

:EOM
rem Uses vbscript to get the starting and ending days of the lastmonth.

rem echo dtStarting = dtEnding - Day(dtEnding) + 1 >> %temp%\tmp.vbs
echo dtEnding   = Date - Day(Date) > %temp%\tmp.vbs
rem echo wsh.echo dtStarting >> %temp%\tmp.vbs
echo wsh.echo dtEnding >> %temp%\tmp.vbs

for /f "tokens=1-3 delims=/" %%a in ('cscript.exe //nologo %temp%\tmp.vbs') do (
  set mm=%%a& if !mm! lss 10 set mm=0!mm!
  set dd=%%b& if !dd! lss 10 set dd=0!dd!
  set yr=%%c
  set eom=!yr!!mm!!dd!
)
del %temp%\tmp.vbs
goto :eof 

Open in new window


Thanks

Glen
GPSPOWAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Try adding this line at the top of your script, it will call the :EOM subroutine and calculate the %EOM% variable value.

call :EOM

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
You will also need this at the top of the script:

setlocal enabledelayedexpansion


so, the completed version would be:

@echo off
setlocal enabledelayedexpansion

net use D:\DTS

call :EOM
echo [%EOM%]

copy  D:\DTS\MedAssets\MedAssets_CDM.txt  "\\pmcfs\groups\MedAssests\78017_CDM_6390.txt"
copy  D:\DTS\MedAssets\MedAssets_RevUsage.txt  "\\pmcfs\groups\MedAssests\78017_CDM_USAGE_1721_%eom%.txt"

net use D:\DTS /DELETE

goto :eof

:EOM
rem Uses vbscript to get the starting and ending days of the lastmonth.

rem echo dtStarting = dtEnding - Day(dtEnding) + 1 >> %temp%\tmp.vbs
echo dtEnding   = Date - Day(Date) > %temp%\tmp.vbs
rem echo wsh.echo dtStarting >> %temp%\tmp.vbs
echo wsh.echo dtEnding >> %temp%\tmp.vbs

for /f "tokens=1-3 delims=/" %%a in ('cscript.exe //nologo %temp%\tmp.vbs') do (
  set mm=%%a& if !mm! lss 10 set mm=0!mm!
  set dd=%%b& if !dd! lss 10 set dd=0!dd!
  set yr=%%c
  set eom=!yr!!mm!!dd!
)
del %temp%\tmp.vbs
goto :eof

Open in new window

~bp
0
GPSPOWAuthor Commented:
Bill,

The fix worked in getting the EOM date on the file name.

What do I need to do to get the beginning of the month date  %bom%?

Thanks

Glen
0
Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try, it just builds the BOM date from the EOM, changing the day to 01.

@echo off
setlocal enabledelayedexpansion

net use D:\DTS

call :EOM

copy  D:\DTS\MedAssets\MedAssets_CDM.txt  "\\pmcfs\groups\MedAssests\78017_CDM_6390.txt"
copy  D:\DTS\MedAssets\MedAssets_RevUsage.txt  "\\pmcfs\groups\MedAssests\78017_CDM_USAGE_1721_$bom%_%eom%.txt"

net use D:\DTS /DELETE

goto :eof

:EOM
rem Uses vbscript to get the starting and ending days of the lastmonth.

rem echo dtStarting = dtEnding - Day(dtEnding) + 1 >> %temp%\tmp.vbs
echo dtEnding   = Date - Day(Date) > %temp%\tmp.vbs
rem echo wsh.echo dtStarting >> %temp%\tmp.vbs
echo wsh.echo dtEnding >> %temp%\tmp.vbs

for /f "tokens=1-3 delims=/" %%a in ('cscript.exe //nologo %temp%\tmp.vbs') do (
  set mm=%%a& if !mm! lss 10 set mm=0!mm!
  set dd=%%b& if !dd! lss 10 set dd=0!dd!
  set yr=%%c
  set eom=!yr!!mm!!dd!
  set bom=!yr!!mm!01
)
del %temp%\tmp.vbs
goto :eof 

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GPSPOWAuthor Commented:
Thank you

Worked perfectly.

Glen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.