Normal Topic DOS command causing error without Loiter (Read 1194 times)
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
DOS command causing error without Loiter
Mar 20th, 2012 at 11:24pm
Print Post Print Post  
I am using following code to prepare a record in pdf file. Sometimes this may be 100 pages or more. Since I use DOS command to copy, move and delete files, they are relatively slow and sbasic runs ahead of them and gives me error. I have placed @Loiter ( ) but it is arbitrary and you do not know how long wait is required for the subject record. I am concerned about getting error especially in the bigger file as 5 second loiter might not be enough for that, while 5 seconds might be plenty for other records.  How can I make sure that irrespective of size of the record, I should not get the error because of slowing down because of DOS commands.

Code
Select All
var vPrinter1 as String = "DocuCom PDF Driver"
var vPrinter2 as String = @GlobalValue ("ServerDefaultPrinter")
var vPath1 as String = "C:\PDFiles\"
var vPath2 as String = "C:\Assembly\"
var vPath3 as String = "C:\Sesame2\Records\"
var vPage1 as Int
var vSuccess as Int
var vRS as Int
var vLoop as Int
var vTotalRecords as Int
var vVisitDate as Date
var vName as String = First + " " + P_LastName

var vFiles as String
var vFilesWithPath as String
var vFinalFile as String
var n as Int

var vString1 as String


var vRSHandle as Int
var vDescription as String
var vFilename as String
var vExtFields as String = ""
var vNoOfRecords as Int


		AlternateDefaultPrinter ("DocuCom PDF Driver")
		vSuccess = @Shell ("Del " + vPath1 + "*.pdf" )
		vSuccess = @Shell ("Del " + vPath2 + "*.pdf" )








	vRS = @XResultSetSearch(@FN, "Physical", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Chart=" + Chart)    // Since Physical also have Chart element and Chart# never changes
	if(vRS > -1)
	{
		//AlternateDefaultPrinter (vPrinter1)
		vTotalRecords = @XResultSetTotal(vRS)
		for vLoop = 1 to vTotalRecords
			XResultSetCurrentPosition(vRS, vLoop)

			vVisitDate = @XResultSetValue(vRS, "Date1")
			vPage = 1
			vStopReset = 0
			Printing_Physical (vVisitDate)

		next

		XResultSetClose(vRS)
	}

vFiles = @LocalListDirectory ("C:\PDFiles\")

If @CountStringArray (vFiles) > 1 then
	{
		vFiles = @SortStringArray (vFiles, 0)
		For n = @CountStringArray (vFiles) downto 1

				If vFilesWithPath = "" then
					{
						vFilesWithPath = @Chr (34) + vPath1 + @AccessStringArray (vFiles, n) + @chr (34)
					}
					Else
					{
						vFilesWithPath = vFilesWithPath + ";" + @Chr (34) + vPath1 + @AccessStringArray (vFiles, n) + @chr (34)

					}

		Next






		vFilesWithPath = @Replace (vFilesWithPath, ";", " ")
		//WriteLN (vFilesWithPath)
		vFinalFile =  vPath1 +  vName  + " " + @Month(@Date) + "-" + @DOM (@date) + "-" + @Year (@date) +  "-visitnotes" + ".pdf"
		//WriteLN (vFinalFile)
		vSuccess = @Shell ( "PDFill.exe MERGE " + vFilesWithPath + " " + @chr (34) + vFinalFile + @chr (34) )
		//vSuccess = @Shell ( "PDFill.exe MERGE " + @chr (34) + vFinalFile1 + @chr (34) + " " + @chr (34) + vFinalFile2 + @chr (34) + " " + @chr (34) + vFinalFile + @chr (34) )
		//vSuccess = @AsynchShell (@Chr (34) + vFinalFile + @Chr (34))   //This code opens up the file


	}
	Else
	{

		//vFileName = vPath1 + vName + " " + @Month(@Date) + "-" + @DOM (@date) + "-" + @Year (@date)+ ".pdf"
		//vSuccess = @AsynchShell (@Chr (34) + vPath1 + vFiles + @Chr (34))
	}






						vString1 = "CMD /C Move " + @Chr(34) + vFinalFile + @chr (34) + " " + vPath2 //If spcace is present it needs to be in quotes
						//WriteLn (vString1)

						CreateAProcess (vString1)  //CreateAProcess requires cmd /c to open up Shell but does not require in @Shell command

						//vSuccess = @Shell (vString5)

Loiter (5000)   //Merging and Moving are taking time

//*********** Now Working with Documents File





//========= Following Routine Check the Documents file Exists on not and if does not give you choice to delete that record before merging them in pdf files

			vRSHandle = @XResultSetSearch(@FN, "Index!Documents", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Chart=" + Chart)

		If vRSHandle > -1 then
			{
				xResultSetSort (vRSHandle, "DateAdded:1")
					n = @XResultSetTotal (vRSHandle)

					While n >= 1
						{
							xResultSetCurrentPosition (vRSHandle, n)
							vDescription = @xResultSetValue (vRSHandle, "Description" )
							//WriteLN (vDescription)
							vFileName = @xResultSetValue (vRSHandle, "FileName" )  //Checking it again to make certain that no good record is deleted)
							//WriteLN (vFileName)
							If Not Fileexists (vFileName)
								{
									If @AskUser (vDescription + " file does not exist, Do you Want to Delet the Record?", "", "") then
										{
											xResultSetDeleteRecord (vRSHandle)
										}
						 		}
							//WriteLN (@str (n))
							n = n - 1
						}



				xResultSetClose (vRSHandle)

			}






//******************************Convert RTF and PDF File *****************************************************************************************

vFinalFile =  vPath1 +  vName  + " " + @Month (@date) + "-" + @DOM (@date) + "-" + @Year (@date) + "-Documents" + ".pdf"

//Loiter (5000) 

vSuccess = @Shell ("Del " + vPath1 + "*.pdf" )  //Give Little time before a file is there to be moved


vRSHandle = @XResultSetSearch(@FN, "Index!Documents", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Chart=" + Chart, "!Print=<>NO")
if(vRSHandle > -1)
	{
		vNoOfRecords = @XResultSetTotal(vRSHandle)


			for vLoop = 1 to vNoOfRecords
			XResultSetCurrentPosition(vRSHandle, vLoop)
			vFileName = @XResultSetValue(vRSHandle, "FileName")
			if @Right (vFileName, 3) = "rtf" then
				{

					//vSuccess = @Shell ( "c:\htmltools\htmltools.exe "  + "-margin 30x30x30x30" + " " + vFileName + " "  + @Replace (vFileName, "RTF", "PDF"))
					//"C:\Program Files\docPrint Pro v5.0\doc2pdf.exe" -i c:\images\test.rtf -o c:\images\test.pdf

					//vSuccess = @Shell ( @chr (34) + "C:\Program Files\docPrint Pro v5.0\doc2pdf.exe" + @Chr (34)  + " -s ShowOfficeUI=1"  + " -i " + vFileName + " -o "  + @Replace (vFileName, "RTF", "PDF"))

					//vFileName = @Replace (vFileName, "RTF", "PDF")
					vSuccess = @Shell ( "RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n " +  @Chr (34) + vPrinter1 + @chr (34) )
					MergeFilePrint(vFileName, "rtf", "", "", vExtFields, "", 0)

				}
			Else if @Right (vFileName, 3) = "pdf" then
				{
					vSuccess = @Shell ("Copy " + @Chr (34) + vFileName + @Chr (34) + " " +  @Chr (34) + vPath1 + "Document" + @str (vLoop) + ".pdf" + @Chr (34) )


				}

			next
			//WriteLn (vString)
			XResultSetClose(vRSHandle)
	}



vFiles = @LocalListDirectory ("C:\PDFiles\")

If @CountStringArray (vFiles) > 1 then
	{
		vFilesWithPath = ""   //Reset the value of vFileWithPath

		vFiles = @SortStringArray (vFiles, 0)
		For n = @CountStringArray (vFiles) downto 1

				If vFilesWithPath = "" then
					{
						vFilesWithPath = @Chr (34) + vPath1 + @AccessStringArray (vFiles, n) + @chr (34)

					}
					Else
					{
						vFilesWithPath = vFilesWithPath + ";" + @Chr (34) + vPath1 + @AccessStringArray (vFiles, n) + @chr (34)

					}

		Next






		vFilesWithPath = @Replace (vFilesWithPath, ";", " ")
		//WriteLN (vFilesWithPath)
		//vFinalFile =  vPath1 +  vName  + " " + @Month(@Date) + "-" + @DOM (@date) + "-" + @Year (@date) +  "-visitnotes" + ".pdf"
		//WriteLN (vFinalFile)
		vSuccess = @Shell ( "PDFill.exe MERGE " + vFilesWithPath + " " + @chr (34) + vFinalFile + @chr (34) )
		//vSuccess = @Shell ( "PDFill.exe MERGE " + @chr (34) + vFinalFile1 + @chr (34) + " " + @chr (34) + vFinalFile2 + @chr (34) + " " + @chr (34) + vFinalFile + @chr (34) )
		//vSuccess = @AsynchShell (@Chr (34) + vFinalFile + @Chr (34))   //This code opens up the file


	}
	Else
	{

		//vFileName = vPath1 + vName + " " + @Month(@Date) + "-" + @DOM (@date) + "-" + @Year (@date)+ ".pdf"
		//vSuccess = @AsynchShell (@Chr (34) + vPath1 + vFiles + @Chr (34))  //This is only if you want to open one page
	}






						vString1 = "CMD /C Move " + @Chr(34) + vFinalFile + @chr (34) + " " + vPath2 //If spcace is present it needs to be in quotes
						//WriteLn (vString1)

						CreateAProcess (vString1)  //CreateAProcess requires cmd /c to open up Shell but does not require in @Shell command

						//vSuccess = @Shell (vString1)

//==========================Now Combining file in Assembly folder and moving it to C:\Sesame2\Records\

Loiter (5000)    //It seems like moving as well as merging above  is taking time

vFinalFile = vPath3 + vName  + " " + @Month (@date) + "-" + @DOM (@date) + "-" + @Year (@date) + ".pdf"
//WriteLN ("Final File = " + vFinalFile )


vFiles = @LocalListDirectory ("C:\Assembly\")

//WriteLN ("Files in Assembly Folder = " + vFiles)

If @CountStringArray (vFiles) > 1 then
	{
		vFilesWithPath = ""   //Reset the value of vFileWithPath

		vFiles = @SortStringArray (vFiles, 0)
		For n = @CountStringArray (vFiles) downto 1

				If vFilesWithPath = "" then
					{
						vFilesWithPath = @Chr (34) + vPath2 + @AccessStringArray (vFiles, n) + @chr (34)

					}
					Else
					{
						vFilesWithPath = vFilesWithPath + ";" + @Chr (34) + vPath2 + @AccessStringArray (vFiles, n) + @chr (34)

					}

		Next






		vFilesWithPath = @Replace (vFilesWithPath, ";", " ")
		//WriteLN ("Files From Assembley = " + vFilesWithPath)
		//vFinalFile =  vPath1 +  vName  + " " + @Month(@Date) + "-" + @DOM (@date) + "-" + @Year (@date) +  "-visitnotes" + ".pdf"
		//WriteLN ("Final File = " + vFinalFile)
		vSuccess = @Shell ( "PDFill.exe MERGE " + vFilesWithPath + " " + @chr (34) + vFinalFile + @chr (34) )
		//vSuccess = @Shell ( "PDFill.exe MERGE " + @chr (34) + vFinalFile1 + @chr (34) + " " + @chr (34) + vFinalFile2 + @chr (34) + " " + @chr (34) + vFinalFile + @chr (34) )
		vSuccess = @AsynchShell (@Chr (34) + vFinalFile + @Chr (34))   //This code opens up the file


	}
	Else
	{

		//vFileName = vPath1 + vName + " " + @Month(@Date) + "-" + @DOM (@date) + "-" + @Year (@date)+ ".pdf"
		vSuccess = @AsynchShell (@Chr (34) + vFilesWithPath + @Chr (34))  //This is only if you want to open one page
	}




vSuccess = @Shell ( "RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n " +  @Chr (34) + vPrinter2 + @chr (34) )// Default Printer in place again

  

  
Back to top
 
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: DOS command causing error without Loiter
Reply #1 - Mar 21st, 2012 at 12:37pm
Print Post Print Post  
how about inserting a loop that checks for the appearance of the newly created pdf file once per second until the file is detected?

-shell to create new pdf file
- loop
    - check for new file
      if yes, end loop; if no, loiter(1000), then loop again to check
      if yes, end loop;  if no, loiter(1000) then loop again to check
      etc.

I guess you'd have to build in a safety 'out' in case the file doesn't ever get created (if loop = 15 times, then stop loop and send @Error message
  

Larry
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: DOS command causing error without Loiter
Reply #2 - Mar 21st, 2012 at 3:06pm
Print Post Print Post  
lksseven,
Thanks for your input. It seems like it was DOS MOVE command that was dragging the system. I removed the Move command by creating the file in the folder where it was supposed to be moved. I removed Loiter ( ) command altogether and I have not seen the error message yet. Hopefully this will solve the problem, otherwise I will run the loop as you suggested and see if DOS command was successfully completed before throwing in the other commands. Thanks again.
  
Back to top
 
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: DOS command causing error without Loiter
Reply #3 - Mar 21st, 2012 at 4:18pm
Print Post Print Post  
No problem.  This thread got me to thinking about where in my own code I might be able to utilize a 'loop till SUCCESS' technique. 

Best Regards,
  

Larry
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: DOS command causing error without Loiter
Reply #4 - Mar 21st, 2012 at 9:40pm
Print Post Print Post  
To be on safer side I placed the following code after DOS Command through @Shell ( ).

Code
Select All
For n = 1 to 10

		If FileExists (vLastFile)
			{
				n = 10
			}
			Else
			{
				Loiter (1000)

			}


Next
 

  
Back to top
 
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: DOS command causing error without Loiter
Reply #5 - Mar 22nd, 2012 at 4:04am
Print Post Print Post  
Sweet!  Simple and elegant.  I'll find places for that, too.
  

Larry
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: DOS command causing error without Loiter
Reply #6 - Mar 22nd, 2012 at 1:20pm
Print Post Print Post  
For a loop like that it's recommended that you use a quit flag rather than increasing the counter when you need to quit. Something like

Code
Select All
Var n as Int
Var vFileExists as Int

n = 1
vFileExists = 0

While ((n <= 10) And (vFileExists = 0))
{

	If FileExists (vLastFile)
	{
		vFileExists = 1
	}
	Else
	{
		Loiter (1000)
	}
	n = n + 1
} 



This also allows you to check, after the loop is done, to see if the file ever was found or not, simply by checking one integer and not having to do another FileExists() call.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: DOS command causing error without Loiter
Reply #7 - Mar 22nd, 2012 at 3:16pm
Print Post Print Post  
Thanks Ray for your sound advice. I am not using While loop as much as I use For ...Next loop. But with a second thought the same thing can be implemented also in For --Next loop too. Such things can avoid a lot troubles.  Thanks again.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: DOS command causing error without Loiter
Reply #8 - Mar 22nd, 2012 at 3:35pm
Print Post Print Post  
Bharat_Naik wrote on Mar 22nd, 2012 at 3:16pm:
Thanks Ray for your sound advice. I am not using While loop as much as I use For ...Next loop. But with a second thought the same thing can be implemented also in For --Next loop too. Such things can avoid a lot troubles.  Thanks again.


For -- Next loops are too basic of a loop to use a quit flag with as they simply increase or decrease until they match the "To" number. On the other hand a While loop equates the conditional to True or False, no matter how many little conditionals there are inside strung together with And's and Or's , before deciding to break or not.

Yes it can reduce a lot of troubles by using a quit flag, and is even recommended on Pages 71-72 of the Sesame 2 Programming guide for breaking out of a loop.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged