Solved

Previous month problem

Posted on 2013-06-11
7
247 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
[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
7 Comments
 
LVL 56

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 56

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 56

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
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…
Suggested Courses

617 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