• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 118
  • Last Modified:

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
0
GPSPOW
Asked:
GPSPOW
  • 3
  • 2
1 Solution
 
Bill PrewCommented:
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 PrewCommented:
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 PrewCommented:
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
 
GPSPOWAuthor Commented:
Thank you

Worked perfectly.

Glen
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now