Page 1 of 2

get last month, set it as a variable and insert in script

Posted: 20 Nov 2012 10:42
by Bowlardo
my script is intended to run every month on the 1st day of the month and get all of last month's file containing a specific text.
It is doing this perfectly at the moment.
The only issue is that I will have to edit the month number highlighlighted below every month.
:arrow: need to get today's date
:arrow: extract month number
:arrow: when the script runs it insert the months number where 11 is highlighted below

Any help would be greatly appreciated

REM******Monthly Files***************
echo on
pushd "\\mfaedi01\gensrvnt\IntOut\#2012\11\"
for /f "delims=" %%a in ('findstr /s /m "YOPLMUCH" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Monthly
)
popd

COPY \\mfbedi02\Gensrvnt\workdir\Monthly\*.* \\mfbedi02\Gensrvnt\InterchangesIn\

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 10:54
by Aacini
Do you want to insert last month number, right? For current month, just delete SET /A and IF lines...

Code: Select all

REM******Monthly Files***************
echo on
for /F "delims=/" %%a in ("%date%") do set /A month=1%%a %% 100
set /A month-=1
if %month% equ 0 set month=12
if %month% lss 10 set month=0%month%
pushd "\\mfaedi01\gensrvnt\IntOut\#2012\%month%\"
for /f "delims=" %%a in ('findstr /s /m "YOPLMUCH" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Monthly
)
popd

COPY \\mfbedi02\Gensrvnt\workdir\Monthly\*.* \\mfbedi02\Gensrvnt\InterchangesIn\


EDIT: Code modified to get month=12 when current month=1

EDIT: Note that the program assume that date format is MM/DD/YYYY. If the day appear first (DD/MM/YYYY), change "delims=/" part by "tokens=2 delims=/". Also, copy the code again...

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 12:48
by Squashman
What happens on January 1, 2013?

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 13:10
by RogerSmith

Code: Select all

REM******Monthly Files***************
echo on
set aa=%date:~3,-5%
set /a bb=%aa%-1
if %bb%==01 set bb=1
if %bb%==02 set bb=2
if %bb%==03 set bb=3
if %bb%==04 set bb=4
if %bb%==05 set bb=5
if %bb%==06 set bb=6
if %bb%==07 set bb=7
if %bb%==08 set bb=8
if %bb%==09 set bb=9
if %bb%==0 set bb=12
pushd "\\mfaedi01\gensrvnt\IntOut\#2012\%bb%\"
for /f "delims=" %%a in ('findstr /s /m "YOPLMUCH" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Monthly
)
popd
COPY \\mfbedi02\Gensrvnt\workdir\Monthly\*.* \\mfbedi02\Gensrvnt\InterchangesIn\


now your code will automactly get the last month

no bugs found.

regards

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 14:23
by Aacini
Squashman wrote:What happens on January 1, 2013?


Oops, my mistake! I fixed it...

Antonio

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 16:34
by dbenham
What about Feb 2013 :?:

I assume #2012 refers to the year, so shouldn't that also get incremented at some point :?:

I know Bowlardo didn't specify the requirement, but it seems like a problem to me. (easily fixed, I know)


Dave Benham

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 16:51
by RogerSmith
he can change that manualy one time per year...

or....

Code: Select all

REM******Monthly Files***************
echo on
set ff=%date:~6%
set aa=%date:~3,-5%
set /a bb=%aa%-1
if %bb%==01 set bb=1
if %bb%==02 set bb=2
if %bb%==03 set bb=3
if %bb%==04 set bb=4
if %bb%==05 set bb=5
if %bb%==06 set bb=6
if %bb%==07 set bb=7
if %bb%==08 set bb=8
if %bb%==09 set bb=9
if %bb%==0 set bb=12
pushd "\\mfaedi01\gensrvnt\IntOut\#%ff%\%bb%\"
for /f "delims=" %%a in ('findstr /s /m "YOPLMUCH" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Monthly
)
popd
COPY \\mfbedi02\Gensrvnt\workdir\Monthly\*.* \\mfbedi02\Gensrvnt\InterchangesIn\


edit by squashman to add code tags.

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 16:58
by Squashman
RogerSmith wrote:he can change that manualy one time per year...

or....

Code: Select all

REM******Monthly Files***************
echo on
set ff=%date:~6%
set aa=%date:~3,-5%
set /a bb=%aa%-1
if %bb%==01 set bb=1
if %bb%==02 set bb=2
if %bb%==03 set bb=3
if %bb%==04 set bb=4
if %bb%==05 set bb=5
if %bb%==06 set bb=6
if %bb%==07 set bb=7
if %bb%==08 set bb=8
if %bb%==09 set bb=9
if %bb%==0 set bb=12
pushd "\\mfaedi01\gensrvnt\IntOut\#%ff%\%bb%\"
for /f "delims=" %%a in ('findstr /s /m "YOPLMUCH" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Monthly
)
popd
COPY \\mfbedi02\Gensrvnt\workdir\Monthly\*.* \\mfbedi02\Gensrvnt\InterchangesIn\


edit by squashman to add code tags.

But now you have the same flaw I pointed out to Aacini but with the year. What happens on January 1, 2013.

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 17:34
by RogerSmith
hehe thanks for point it out, mind blowing.

REM******Monthly Files***************
echo on
set ff=%date:~6%
set aa=%date:~3,-5%
if %date:~3,-5%==01 set /a ff=%date:~6%-1
set /a bb=%aa%-1
if %bb%==01 set bb=1
if %bb%==02 set bb=2
if %bb%==03 set bb=3
if %bb%==04 set bb=4
if %bb%==05 set bb=5
if %bb%==06 set bb=6
if %bb%==07 set bb=7
if %bb%==08 set bb=8
if %bb%==09 set bb=9
if %bb%==0 set bb=12
pushd "\\mfaedi01\gensrvnt\IntOut\#%ff%\%bb%\"
for /f "delims=" %%a in ('findstr /s /m "YOPLMUCH" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Monthly
)
popd
COPY \\mfbedi02\Gensrvnt\workdir\Monthly\*.* \\mfbedi02\Gensrvnt\InterchangesIn\

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 17:39
by Squashman
You already have aa and ff defined as variables why not use them to your advantage.

Code: Select all

if %aa%==01 set /a ff-=1

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 17:43
by RogerSmith
hehehe you are 100% right

nice eye!

Re: get last month, set it as a variable and insert in scrip

Posted: 20 Nov 2012 18:28
by Aacini
There are a couple details in my code above. If date format is not MM/DD/YYYY but DD/MM/YYYY a small mod is needed. Refer to my first post above...

Re: get last month, set it as a variable and insert in scrip

Posted: 21 Nov 2012 08:24
by Bowlardo
Thank Dave

I neglected to factor in the year
Much appreciated


dbenham wrote:What about Feb 2013 :?:

I assume #2012 refers to the year, so shouldn't that also get incremented at some point :?:

I know Bowlardo didn't specify the requirement, but it seems like a problem to me. (easily fixed, I know)


Dave Benham

Re: get last month, set it as a variable and insert in scrip

Posted: 21 Nov 2012 08:25
by Bowlardo
Thanks Everyone that is working perfectly now :D

Really impressed.

Re: get last month, set it as a variable and insert in scrip

Posted: 07 Aug 2013 03:27
by Bowlardo
The script is in bold and has worked for several months. For some reason that I do not understand it is not running this month
echo on
set ff=%date:~6%
set aa=%date:~3,-5%
if %date:~3,-5%==01 set /a ff=%date:~6%-1
set /a bb=%aa%-1
if %bb%==1 set bb=01
if %bb%==2 set bb=02
if %bb%==3 set bb=03
if %bb%==4 set bb=04
if %bb%==5 set bb=05
if %bb%==6 set bb=06
if %bb%==7 set bb=07
if %bb%==8 set bb=08
if %bb%==9 set bb=09
if %bb%==0 set bb=12
pushd "\\mfaedi01\gensrvnt\IntOut\#%ff%\%bb%\"
for /f "delims=" %%a in ('findstr /s /m "YPLTDNST" *.*') do (
echo copying %%a
copy /b %%a P:\Gensrvnt\workdir\Test
)
popd
COPY \\mfbedi02\Gensrvnt\workdir\TestA\*.* \\mfbedi02\Gensrvnt\TestB\


When i run the script i am getting
P:\Gensrvnt\SCRIPTS>ydmr.cmd

P:\Gensrvnt\SCRIPTS>REM******Yoplait Musgraves Monthly Files***************
'REM******Yoplait' is not recognized as an internal or external command,
operable program or batch file.

P:\Gensrvnt\SCRIPTS>echo on

P:\Gensrvnt\SCRIPTS>set ff=2013

P:\Gensrvnt\SCRIPTS>set aa=08

P:\Gensrvnt\SCRIPTS>if 08 == 01 set /a ff=2013-1

P:\Gensrvnt\SCRIPTS>set /a bb=08-1
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
set was unexpected at this time.


Does anyone know what is wrong with the script or what is causing the error?