Solved

Add Month End date to COPY command

Posted on 2014-01-11
12
1,663 Views
Last Modified: 2014-01-17
I am trying to add the previous month's end to the title of a file I am creating using the COPY command.

I have this command syntax that appends the current day.  What can I change to make it the previous month's end data?

%date:~10,4%%date:~4,2%%date:~7,2%

Thank you

Glen
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39773659
The easiest way to get the end of last month is to take the first of this month and take one off it.  I would use a little bit of VBScript to help here as it understands dates natively.  Otherwise you would have to take into account how many days in each month and leap years etc.  Do-able but bit OTT in batch.  My article mentioned below shows how to get the different parts of the date etc. if wanted:

@echo off
REM Use VBScript to get date.  This will get the first day of this month and take one off to get last day of last month
REM See http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
(echo eolm=dateserial^(year^(date^),month^(date^),1^)-1
echo wscript.echo year^(eolm^) ^& right^(100 + month^(eolm^),2^) ^& right^(100+day^(eolm^),2^)) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a


echo End of last month to use in filename was %yyyymmdd%

pause

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39774084
Here's a BAT subroutine that should do what you want, and returns a variable with the date required.

@echo off
call :EOM
echo %eom%
exit /b

: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

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39774096
But Bill that will break in 2100 as it isn't a leap year :-) ..... but then it probably does in VBScript too and I don't suppose we'll be using batch then!

Steve
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 56

Accepted Solution

by:
Bill Prew earned 350 total points
ID: 39774106
Well, if you want to be that way Steve, this should fix those non-leap years... ;-)

@echo off
call :EOM
echo %eom%
exit /b

: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%%%400))^|!(!(%yy%%%100)))^&!(%yy%%%4)
  if %mm% == 2 if %leap% == 1 set /A "dd+=1"
  REM Return end of prior moth in eom variable in YYYYMMDD format
  endlocal & set eom=%yy%%mm%%dd%
  exit /b

Open in new window

~bp
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 150 total points
ID: 39774930
Maybe you should start to use PowerShell, if thinking in decades or centuries ;-)
$eom = get-date (get-date).AddDays(-(get-date).Day) -format 'yyMMdd'
copy-item "C:\SourceFolder\File.txt" "D:\TargetFolder\File.$eom.txt"

Open in new window

0
 

Author Comment

by:GPSPOW
ID: 39774965
I have added billprew's solution to my batch and I am not sure if I have it correct.  I am not getting any results to the target folder.  Please review and let me know what I need to do adjust to make it work.

@echo off
call :EOM
echo %eom%
exit /b

: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





bcp "select * from livedb.dbo.tbl_RCA_ATB_HEADERS" queryout "P:\ACCOUNTING\RCA_HEADERS.csv" -T -SPTM-DR01 -w -t^|


bcp "select * from livedb.dbo.tbl_RCA_PE_ATB_MONTHEND" queryout "P:\ACCOUNTING\RCA_ATB_ME_Temp.csv" -T -SPTM-DR01 -w -t^|

copy P:\ACCOUNTING\RCA_HEADERS.csv + P:\ACCOUNTING\RCA_ATB_ME_Temp.csv  P:\ACCOUNTING\CHI\MonthEndATB\rca_p1_mt_monthend_accts_"$eom.txt


Thanks

Glen
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39774969
You'll need to insert your code instead of echo %eom%. The following exit /b stops execution of the batch file, it's the "end of batch file" command.
0
 

Assisted Solution

by:GPSPOW
GPSPOW earned 0 total points
ID: 39775058
One last thing..

The file results in $eom instead of the date as part of the name.

 P:\ACCOUNTING\CHI\MonthEndATB\rca_p1_mt_monthend_accts_"$eom.txt

Please help

Thanks

glen
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39775075
Sorry, didn't see you mixed Bill's and my note. In a cmd batch file (.bat or .cmd) you will have to use %eom%, not $eom. The latter is PowerShell syntax (.ps1 file).
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39775275
No doubt you used Bill's approach, so http#a39774106 should get the lion's share - ok so far.
But did you really wanted to accept http:#a39774930, which is about a PowerShell solution you did not try (presumely)? And your own comment http:#a39775058 doesn't make sense to accept.

My suggestion is close as follows:
   http:#a39774106   400 points
   http:#a39774969   50 p.
   http:#a39775075   50 p.

You can do that yourself by again using "Accept Multiple Solutions", which will override the current pending closure.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39775330
I feel all left out :-)

You are :P - Qlemo
0
 

Author Closing Comment

by:GPSPOW
ID: 39788036
Thanks

It works perfectly now.

Glen
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

615 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