Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Previous month problem

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
NLITech
Asked:
NLITech
  • 4
  • 3
1 Solution
 
Bill PrewCommented:
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
 
NLITechAuthor Commented:
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
 
Bill PrewCommented:
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
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
NLITechAuthor Commented:
@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
 
NLITechAuthor Commented:
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
 
NLITechAuthor Commented:
Thank you!
0
 
Bill PrewCommented:
Sorry was away for a bit, but glad you got to a good place.

~bp
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now