Solved

Previous month problem

Posted on 2013-06-11
7
243 Views
Last Modified: 2013-06-11
Here is the deal, we have a contractor that processes our monthly bills, and they then post .zip files of the PDF copies of the bills. The contractor normally gets the .zip files posted before the end of the month, but on short months, like February they are posted the following month. I have the batches, there are three in total, scheduled in task scheduler, they start running on the 25th (soonest day the .zip files could be posted) to the 5th, (the last day the bills could be posted). I need the posted zipped PDF’s as soon as they are available.

After the PDF’s are unzipped they are placed in a folder with the year (YYYY)under the year is two digit month of the billing period, if they are posted before the last day of the month, the billing period is the current month. Between 1 and 5 the PDF bills go into the previous month. This part is working well.

After the bills are unzipped and place in the correct month, I need to do some error checking (making sure all of the PDF bills have been downloaded, checking the one csv file (the index) to make the correct number of bills have been downloaded and send an email with the results. Doing the email with a fourth bat file with blat.

The problem I am having is on the error checking part of the previous month, in this part of the script the batch is looking for the folder 5; I need it to look for 05, in June.
I am new to writing batch files more than one line, I am a bit embarrassed to post my who script, I am sure there are far more elegant ways to write this, but here it is:

@echo off
CD /D "D:\files\public1\Member Services Shared Forms\PDFBills"
set source=D:\files\public1\Member Services Shared Forms\PDFBills\UnZip
set targetRoot=D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    if not exist "%targetRoot%\%%C\%%A" mkdir "%targetRoot%\%%C\%%A"
    move "%%~fF" "%targetRoot%\%%C\%%A"
  )
)



@echo off
cd /d "D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills"
setlocal
setpath=%path%;"D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
set "mm=%date:~4,2%
set /a monthback=%date:~4,2%-1
if %MonthBack% == 100 set MonthBack=112
set monthback=%monthback:~-2%
set "dd=%date:~7,2%"
set "yyyy=%date:~-4%"
set targetRoot==D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills

if exist "%targetRoot%\%%C\%%A" (

Echo There were a total of > "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
attrib.exe /s ./*.* |find /c /v "" >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Files Downloaded for %yyyy%-%mm%  billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo Of them there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.pdf "%TEMP%" /L /S /Q /Y /d:%mm%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo PDF Bills for %yyyy%-%mm% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo and there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.csv "%TEMP%" /L /S /Q /Y /d:%mm%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo csv Files for %yyyy%-%mm% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo In the CSV file, there are >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
FOR /F "usebackq delims=" %a IN (%yyyy%\%mm%\AccountList.csv) DO SET /A count+=1 >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Records >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

) Else (

Echo There were a total of > "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.* "%TEMP%" /L /S /Q /Y /d:%monthback%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Files Downloaded for %yyyy%-%monthback% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo Of them there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.pdf "%TEMP%" /L /S /Q /Y /d:%monthback%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo PDF Bills for %yyyy%-%monthback% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo.>> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo.>> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo and there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.csv "%TEMP%" /L /S /Q /Y /d:%monthback%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo csv Files for %yyyy%-%monthback% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo In the CSV file, there are >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
FOR /F "usebackq delims=" %a IN (%yyyy%\%monthback%\AccountList.csv) DO SET /A count+=1 >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Records >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo.>> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

endlocal
)
0
Comment
Question by:NLITech
  • 4
  • 3
7 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39238723
If I understand you correctly this should get you on the right track.  The basic idea is once you have the month number in a variable you check if it is less than 10, and if so prepend the leading 0 to the left.  Since you are working inside a FOR loop you will need EnableDelayedExpansion as well.  Hope this helps, let me know if you have questions.

@echo off
setlocal EnableDelayedExpansion
cd /d "D:\files\public1\Member Services Shared Forms\PDFBills"
set source=D:\files\public1\Member Services Shared Forms\PDFBills\UnZip
set targetRoot=D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    set MM=%%A
    if !MM! LSS 10 set MM=0!MM!
    if not exist "%targetRoot%\%%C\!MM!" mkdir "%targetRoot%\%%C\!MM!"
    move "%%~fF" "%targetRoot%\%%C\!MM!"
  )
)

Open in new window

~bp
0
 

Author Comment

by:NLITech
ID: 39238867
Bp,
Thank you for tring to help, but I just may be beyond help.
Is this how it should look now?

@echo off
CD /D "D:\files\public1\Member Services Shared Forms\PDFBills"
set source=D:\files\public1\Member Services Shared Forms\PDFBills\UnZip
set targetRoot=D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    if not exist "%targetRoot%\%%C\%%A" mkdir "%targetRoot%\%%C\%%A"
    move "%%~fF" "%targetRoot%\%%C\%%A"
  )
)



@echo off
setlocal EnableDelayedExpansion
cd /d "D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills"
set source=D:\files\public1\Member Services Shared Forms\PDFBills\UnZip
set targetRoot==D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills
set "yyyy=%date:~-4%"
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    set MM=%%A
    if !MM! LSS 10 set MM=0!MM!
    if not exist "%targetRoot%\%%C\!MM!" mkdir "%targetRoot%\%%C\!MM!"
    move "%%~fF" "%targetRoot%\%%C\!MM!"
  )
)
Echo There were a total of > "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
attrib.exe /s ./*.* |find /c /v "" >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Files Downloaded for %yyyy%-%mm%  billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo Of them there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.pdf "%TEMP%" /L /S /Q /Y /d:%mm%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo PDF Bills for %yyyy%-%mm% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo and there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.csv "%TEMP%" /L /S /Q /Y /d:%mm%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo csv Files for %yyyy%-%mm% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo In the CSV file, there are >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
FOR /F "usebackq delims=" %a IN (%yyyy%\%mm%\AccountList.csv) DO SET /A count+=1 >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Records >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39238880
I'm a little confused on what you are showing there.  It looks like there are TWO batch files, not one?  Which one is it that needs to be changed?

~bp
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:NLITech
ID: 39238906
@echo off
setlocal EnableDelayedExpansion
cd /d "D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills"
set source=D:\files\public1\Member Services Shared Forms\PDFBills\UnZip
set targetRoot==D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills
set "yyyy=%date:~-4%"
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    set MM=%%A
    if !MM! LSS 10 set MM=0!MM!
    if not exist "%targetRoot%\%%C\!MM!" mkdir "%targetRoot%\%%C\!MM!"
    move "%%~fF" "%targetRoot%\%%C\!MM!"
  )
)

if exist "%targetRoot%\%%C\%%A" (

Echo There were a total of > "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
attrib.exe /s ./*.* |find /c /v "" >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Files Downloaded for %yyyy%-%mm%  billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo Of them there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.pdf "%TEMP%" /L /S /Q /Y /d:%mm%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo PDF Bills for %yyyy%-%mm% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo. >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"

Echo and there were >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
xcopy *.csv "%TEMP%" /L /S /Q /Y /d:%mm%-%dd%-%yyyy%  >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo csv Files for %yyyy%-%mm% billing period >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo In the CSV file, there are >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
FOR /F "usebackq delims=" %a IN (%yyyy%\%mm%\AccountList.csv) DO SET /A count+=1 >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
Echo Records >> "D:\files\public1\Member Services Shared Forms\PDFBills\logs\BillCount.txt"
0
 

Author Comment

by:NLITech
ID: 39239342
This is working for both %mm% and %monthback%, so I believe I have it.
@echo off
setlocal EnableDelayedExpansion
cd /d "D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills"
set "mm=%date:~4,2%
set /a monthback=%date:~4,2%-1
if !monthback! LSS 10 set monthback=0%monthback%
set "dd=%date:~7,2%"
set "yyyy=%date:~-4%"
set targetRoot==D:\files\public1\Member Services Shared Forms\PDFBills\MonthlyPDFBills

Thank you BP!
0
 

Author Closing Comment

by:NLITech
ID: 39239344
Thank you!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39239593
Sorry was away for a bit, but glad you got to a good place.

~bp
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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