Solved

Appending the EOM date to a file name dependent on the current date

Posted on 2015-02-15
7
149 Views
Last Modified: 2015-02-16
In the past I was given the syntax to append the today's date to a file name:

"filename_"%date:~10,4%%date:~4,2%%date:~7,2%".txt"

Can I get the syntax for the previous month's  end date in a yyyymmdd format?

Example:

If today is 3/2/2015, the output would be 20150228.

Thanks

Glen
0
Comment
Question by:GPSPOW
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 40611275
If using VBScript is ok w/ you, this should give your main need:
@echo off
setlocal ENABLEDELAYEDEXPANSION
echo dtEnding   = Date - Day(Date) > %temp%\tmp.vbs
echo wsh.echo dtEnding >> %temp%\tmp.vbs
echo wsh.echo dtStarting >> %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
  echo !yr!!mm!!dd!
)
del %temp%\tmp.vbs
goto :eof

Open in new window

0
 

Author Comment

by:GPSPOW
ID: 40612606
Here is a batch file I have copied from one that works for the EOM date.  However, for some reason it is not copying the data.

I have double checked the paths and they are correct.

Can someone look and see if there is a syntax error?
net use D:\DTS






echo off
call :EOM
 echo %eom%
 
 :EOM
   setlocal
   REM Get todays date (into mm, dd, yy variables)
   for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
     for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (
       set "%%a=%%A" 
       set "%%b=%%B" 
       set "%%c=%%C"
     )
   )
   REM Convert month to number and decrease by 1
   set /A "mm=1%mm% - 101"
   REM If this is January, then decrease year and make month December
   if %mm% == 0 (
     set mm=12
     set /a yy-=1
   )
   REM Get the normal last day for this month
   for /F "tokens=%mm% delims=," %%A in ("31,28,31,30,31,30,31,31,30,31,30,31") do set dd=%%A
   REM See if this is a leap year, if so and it's February, add one to last day of month
   set /A "leap=%yy% %% 4"
   if %mm% == 2 if %leap% == 0 set /A "dd+=1"
   REM Return end of prior moth in eom variable in YYYYMMDD format
   endlocal & set eom=%yy%%mm%%dd%
   exit /b 





 

 copy D:\DTS\MonthlyConiferData\ConiferData_SQL.txt  "\\pmcfs\groups\Accounting\ConiferHealth\ConiferData_"%eom%".txt"





net use D:\DTS /DELETE

Open in new window


Thanks

glen
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40612620
Does your code return the last day of the prior month, which is your original question? If not, you can use my solution for that.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:GPSPOW
ID: 40612629
I figured out why it was not running.  The "exit /b" command stopped the batch.

I am getting the last day of the month.  

However if the numeric month is Jan - Sept I only have one digit instead of a leading zero.  So Jan shows up as 2015131.  It needs to be 20150131.

If I use your code, what do I replace in the %eom% for date variable in the destination file name?  Or if I stick with my solution, what can I add to determine when I need the leading zero?

thanks

Glen
0
 
LVL 23

Accepted Solution

by:
NVIT earned 500 total points
ID: 40612660
Try this:

Note: I included the dtStarting (2 disabled lines) in case you need the beginning-of-month one fine day.

net use D:\DTS

echo off
setlocal ENABLEDELAYEDEXPANSION

call :EOM
copy D:\DTS\MonthlyConiferData\ConiferData_SQL.txt  "\\pmcfs\groups\Accounting\ConiferHealth\ConiferData_%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

0
 

Author Closing Comment

by:GPSPOW
ID: 40612691
Thanks for the help

Glen
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40612697
You're welcome. Glad to help...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 210
How does Google get such fast response times? 5 152
eclipse package explorer vs project explorer view 2 84
simplest php form 3 63
A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now