GPSPOW
asked on
Add Month End date to COPY command
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%%da te:~7,2%
Thank you
Glen
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%%da
Thank you
Glen
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
~bp
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
Steve
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,3 1") 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_HEA DERS" 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_T emp.csv P:\ACCOUNTING\CHI\MonthEnd ATB\rca_p1 _mt_monthe nd_accts_" $eom.txt
Thanks
Glen
@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,
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_HEA
bcp "select * from livedb.dbo.tbl_RCA_PE_ATB_
copy P:\ACCOUNTING\RCA_HEADERS.
Thanks
Glen
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).
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.
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.
I feel all left out :-)
You are :P - Qlemo
You are :P - Qlemo
ASKER
Thanks
It works perfectly now.
Glen
It works perfectly now.
Glen
Open in new window