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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

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

#1 Post by Bowlardo » 20 Nov 2012 10:42

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\

Aacini
Expert
Posts: 1909
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#2 Post by Aacini » 20 Nov 2012 10:54

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...
Last edited by Aacini on 20 Nov 2012 18:26, edited 2 times in total.

Squashman
Expert
Posts: 4483
Joined: 23 Dec 2011 13:59

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

#3 Post by Squashman » 20 Nov 2012 12:48

What happens on January 1, 2013?

RogerSmith
Posts: 22
Joined: 20 Nov 2012 12:42

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

#4 Post by RogerSmith » 20 Nov 2012 13:10

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

Aacini
Expert
Posts: 1909
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#5 Post by Aacini » 20 Nov 2012 14:23

Squashman wrote:What happens on January 1, 2013?


Oops, my mistake! I fixed it...

Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#6 Post by dbenham » 20 Nov 2012 16:34

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

RogerSmith
Posts: 22
Joined: 20 Nov 2012 12:42

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

#7 Post by RogerSmith » 20 Nov 2012 16:51

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.

Squashman
Expert
Posts: 4483
Joined: 23 Dec 2011 13:59

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

#8 Post by Squashman » 20 Nov 2012 16:58

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.

RogerSmith
Posts: 22
Joined: 20 Nov 2012 12:42

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

#9 Post by RogerSmith » 20 Nov 2012 17:34

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\

Squashman
Expert
Posts: 4483
Joined: 23 Dec 2011 13:59

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

#10 Post by Squashman » 20 Nov 2012 17:39

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

RogerSmith
Posts: 22
Joined: 20 Nov 2012 12:42

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

#11 Post by RogerSmith » 20 Nov 2012 17:43

hehehe you are 100% right

nice eye!

Aacini
Expert
Posts: 1909
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#12 Post by Aacini » 20 Nov 2012 18:28

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...

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

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

#13 Post by Bowlardo » 21 Nov 2012 08:24

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

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

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

#14 Post by Bowlardo » 21 Nov 2012 08:25

Thanks Everyone that is working perfectly now :D

Really impressed.

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

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

#15 Post by Bowlardo » 07 Aug 2013 03:27

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?

Post Reply