VB icon

Using and Escaping a Do Loop

Email
Submitted on: 2/11/2019 10:38:43 PM
By: A_X_O 
Level: Beginner
User Rating: Unrated
Compatibility: VbScript (browser/client side)
Views: 82
 
     A simple demonstration of using a Do Loop with limitations to maintain control. This script monitors the existence of a txt file placed on the desktop. If the target file is deleted, it will be re-created for a maximum of 3 times.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Using and Escaping a Do Loop
' Description:A simple demonstration of using a Do Loop with limitations to maintain control. This script monitors the existence of a txt file placed on the desktop. If the target file is deleted, it will be re-created for a maximum of 3 times.
' By: A_X_O
'**************************************

'
'
'	NOTE: 	The "Do Loop" is limited to 3 runs in this demonstration. 
'		To exit sooner:
'		Open Task-Manager.
'		Under The "Processes" Tab, Select: Microsoft Windows Based Script Host.
' 		Then choose "End Task" 
' 
'	TEST:	Run the VBScript. A text file named "NewText.txt" 
'		Will be created on the Desktop containing the vbscript code.
'		When you Delete the "NewText.txt" file, it will be re-created automatically. 
'
'		Manually Refresh the desktop and the txt file will be visible again.
'
'------------------------------------------------------------------------------	'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	'
'		Demonstration: Windows 10, Maintaining control in a Loop	'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	'
'										'
'	Purpose		:	Demonstrates VBScript Exiting "Do" Loop		'
'	-----------------------------------------------------------------------	'
'	Creation Date	: 	09/02/2019 [dd/mm/yyyy]				'
'	Version		:	1.2						'
'	Designer	:	Fabian						'
'										'
'##############################################################################	'
'			MODIFICATION HISTORY					'
'------------------------------------------------------------------------------	'
'	Version		:	1:0	09/02/2019	Create the Sample	'
'			:	1:1	11/02/2019	Free Objects on Exit	'
'			:	1:2	12/02/2019	Add Comments & Tidy up	'
'										'
'										'
'------------------------------------------------------------------------------	'		
'
'	[Memory Dimensions - VBScript only uses the "Variant" Data Type]
'
Dim FSO									'As Object	
Dim WshShell								'As Object
Dim ThisScript								'As String
Dim VBScriptFile							'As Long
Dim VBSCode								'As String
Dim StrMyDesktop							'As String
Dim ObjectTarget							'As String
'
'	[RunCounter holds the "Integer" count of runs]
'
Public RunCounter							'As Integer
'
'	[Constants for FSO File I/O]
'
Public Const ForReading = 1
Public Const ForWriting = 2
'
Public Const NoDelete = "\NewText.txt"					'Name and type of the target file
'
'	[Create the Environments for the Script]
'
Set FSO = CreateObject("Scripting.FileSystemObject")			'File System Object
Set WshShell = Wscript.CreateObject("Wscript.Shell")			'Windows Scripting Host
'
 	StrMyDesktop = WshShell.SpecialFolders("Desktop")		'Users desktop path
'
	ThisScript = Wscript.ScriptFullName				'Full path of the requesting script
'
	ObjectTarget = StrMyDesktop & NoDelete				'Full path of the target file on the users desktop			
'
If FSO.FileExists(ThisScript) Then					'Not much need for this check.
'
Set VBScriptFile = FSO.OpenTextFile(ThisScript, ForReading)		'Open Self
'
 	VBSCode = VBScriptFile.ReadAll					'Read Self and store in the Variable "VBSCode"
'
End If
'
'	[Start this "Do Loop" at the end of the script]
'
Do									'Do Loop. Repeat everything between "Do" and "Loop"
'
If RunCounter > 3 Then: WScript.Quit					'Exit after a few runs. Limited for demonstration.
'
If FSO.FileExists(ObjectTarget) = False Then				'Check for existance of the target file
'
Set NewVBScript = FSO.OpenTextFile(ObjectTarget, ForWriting, True)	'On a False Return Value, Create the target file.
'
With NewVBScript
'
	.Write VBSCode							'Write Self into the target file
	.Close								'Close the target. Failure to close, locks the file by the system.
'
End With
'
	RunCounter = RunCounter + 1					'Increment and Monitor Number of Runs
'
End If
'
Loop
'
Set FSO = Nothing							'Release both the File System and Scripting Host Objects.
Set WshShell = Nothing
'
		


Other 12 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.