I need help with for loop

January 5, 2011 at 00:34:36
Specs: Windows XP
I have a text file (C:\pubs.txt) which contain the following lines:
ACC_
JC_
JD_
JE_

Is it possible to condense the following lines of code into a for loop to read each line in pubs.txt as a variable? In the event that new Publications are added my sqlcmd script would automatically add the new pub to the text file instead of having to rummage through batch files to add them. Any help would be greatly appreciated...

FIND "ACC_" "%Loc%\bparow\RCE.txt" >nul
if errorlevel 1 (del %Loc%\Results\PUBS\ACC_*.pdf
) else (
call %Loc%\Results\ACC_.bat
move %Loc%\Results\PUBS\ACC_*.pdf %Loc%\%FolderName%)

@ echo off
FIND "JC_" "%Loc%\bparow\RCE.txt" >nul
if errorlevel 1 (del %Loc%\Results\PUBS\JC_*.pdf
) else (
call %Loc%\Results\JC_.bat
move %Loc%\Results\PUBS\JC_*.pdf %Loc%\%FolderName%)

@ echo off
FIND "JD_" "%Loc%\bparow\RCE.txt" >nul
if errorlevel 1 (del %Loc%\Results\PUBS\JD_*.pdf
) else (
call %Loc%\Results\JD_.bat
move %Loc%\Results\PUBS\JD_*.pdf %Loc%\%FolderName%)

@ echo off
FIND "JE_" "%Loc%\bparow\RCE.txt" >nul
if errorlevel 1 (del %Loc%\Results\PUBS\JE_*.pdf
) else (
call %Loc%\Results\JE_.bat
move %Loc%\Results\PUBS\JE_*.pdf %Loc%\%FolderName%)


See More: I need help with for loop

Report •

#1
January 5, 2011 at 07:45:35
@echo off
set Loc=c:\yourpath
set FolderName=yourfoldername

for %%a in (ACC_ JC_ JD_ JE_) do (
  find %%a "%Loc%\bparow\RCE.txt" >nul
  if errorlevel 1 (
    del %Loc%\Results\PUBS\%%a*.pdf
  ) else (
    call %Loc%\Results\%%a.bat
    move %Loc%\Results\PUBS\%%a*.pdf %Loc%\%FolderName%
  )
)

Not tested and I had to make assumptions on the variables because I did not get to see your complete code. Check this site out for more...

http://www.robvanderwoude.com/for.php

The code seems a little too simple. What is it you are trying to do? There may be a better way.


Report •

#2
January 5, 2011 at 07:59:24
Ace,
I work for the New England Journal of Medicine and we have 17 other Publications. Every week we process certain publications for distribution and pdf reports are created that need to be looked at by various employees.
The reason for the batch file is to check if any of the 18 publications have been processed recently and if not to delete the pdf files for that pub. If so, it calls the corresponding batch file to print out the reports and then move them to an archive directory.
Right now all 18 pubs have there own section in the batch file which makes it a little hard to manage if any changes need to happen. The reason I would like to shorten it up with a for loop and use an external text file to get the variables is that I want to use a sqlcmd to create the text file with the current list of Publications. This way if any new Pubs are launched or old ones deleted I don't have to remember to edit the batch files...

Report •

#3
January 5, 2011 at 08:26:52
I would integrate the

ACC_.bat

into this batch file so you don't have to have a batch file for each publication. The current batch file will only work with the four Hard Coded publications which would mean you have to modify it each time you add or remove a publication.

You can integrate it as a subroutine and still use the CALL command. If you post the code for these batch files I can show you how.


Report •

Related Solutions

#4
January 5, 2011 at 08:46:22
I want to keep them seperate because each of the 18 publication has 17 different pfd files created for them and the number of copies printed for each report is different for each publication. So it would be too messy to keep them all in one place.
An example of one of the printing batch files:
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSC036A.pdf
C:\pdfp\pdfp.exe -c 2 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSC050A.pdf
C:\pdfp\pdfp.exe -c 3 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSC050B.pdf
C:\pdfp\pdfp.exe -c 2 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSC050C.pdf
C:\pdfp\pdfp.exe -c 2 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSC050D.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSL024.pdf
C:\pdfp\pdfp.exe -c 4 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSL036.pdf
C:\pdfp\pdfp.exe -c 3 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSL037.pdf
C:\pdfp\pdfp.exe -c 3 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSL039G.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_BBSL039S.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_C_BBSC027.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_D_BBSC027.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_I_BBSC027.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_C_BBSC027_S.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_D_BBSC027_S.pdf
C:\pdfp\pdfp.exe -c 1 \\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\ACC_I_BBSC027_S.pdf

Report •

#5
January 5, 2011 at 10:30:26
Thanx Ace,
I used your code and changed it to look at the text file and initial tests seem to work just fine.
Thanx again Ace...

for /f "tokens=1,2" %%p in (%Loc%\bparow\pubs.txt) do (
find "%%p" "%Loc%\bparow\RCE.txt" >nul
if errorlevel 1
(del %Loc%\Results\PUBS\%%p*.pdf"
) else (
call %Loc%\Results\%%p.bat
move %Loc%\Results\PUBS\%%p*.pdf %Loc%\%FolderName%)
)


Report •

#6
January 5, 2011 at 10:43:22
Ok here we go. I can not test because I don't have the files you have and I don't have the PDFP.EXE. You may want to REM out the DEL commands until you test it...

@echo off
  setlocal ENABLEEXTENSIONS
  set Loc=c:\yourpath
  set FolderName=yourfoldername

  for %%a in (ACC_ JC_ JD_ JE_) do (
    find %%a "%Loc%\bparow\RCE.txt" >nul
    if errorlevel 1 (
      del %Loc%\Results\PUBS\%%a*.pdf
    ) else (
      call :PrintFiles %%a
      move %Loc%\Results\PUBS\%%a*.pdf %Loc%\%FolderName%
    )
  )
goto :EOF

:PrintFiles
  set ReportPath=\\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\
  For /F "tokens=*" %%b in ('DIR %ReportPath%\%1_*.pdf') do (
    C:\pdfp\pdfp.exe -c 1 %%b
  )
goto :EOF

Now do you have to delete the files that are not in the list? The problem is that it needs to know who your publishers are in order to know what is not in your SQL generated list. This means you either have to hard code this as in the example above or use a compare file of some sort. If you don't have to delete the files and only want to print and copy what is in your list then you could use code like this...

@echo off
  setlocal ENABLEEXTENSIONS
  set Loc=c:\yourpath
  set FolderName=yourfoldername

  For /F "tokens=*" %%b in ("%Loc%\bparow\RCE.txt") do (
    call :PrintFiles %%a
    move %Loc%\Results\PUBS\%%a*.pdf %Loc%\%FolderName%
  )
goto :EOF

:PrintFiles
  set ReportPath=\\Corpprod1\Shared\Bits\OPPSGEN\ACS\ReportRunner\Results\PUBS\
  For /F "tokens=*" %%b in ('DIR %ReportPath%\%1_*.pdf') do (
    C:\pdfp\pdfp.exe -c 1 %%b
  )
goto :EOF

This code is more robust because you don't have to use a hard coded list of publishers but it does not delete the old files.

If the %Loc%\Results\PUBS\ folder held only the files just published, I might tackle this problem from a different angle. That would be to DEL *.* the %Loc%\%FolderName% directory and then I would move all of the files from %Loc%\Results\PUBS\ to %Loc%\%FolderName% and just print the contents of the %Loc%\%FolderName% directory. Just something you might want to think about because it would make your code way simpler and more robust and you would not need to generate a list from SQL.

Some times it is easier to solve the maze from the inside out.


Report •

#7
January 5, 2011 at 12:32:17
I am going to keep the print batch files seperate. One of the reasons for this automation is that we are moving away from printing and I've incorporated a sqlcmd query to instead send links to the reports in an email. As users OK the email works for them the number of copies that get printed from the print bat files will diminsh until they are no longer needed. So the print bat files should only be a concern for a month or so.

I really appreciate all your help in optimizing my code and sending me in the right direction. I also learned a bit more with your other examples. You ROCK!!!


Report •

Ask Question