VBScript help please.

Caporegime
Joined
1 Nov 2003
Posts
35,691
Location
Lisbon, Portugal
Hi guys,

I've never been a VBScript guru but a couple of years back I got to a point where I could butcher a script to make it do what I wanted :p - I've since had a long break from any form of scripting.

Anyway, I am making a script for work and I've got it doing 95% of what we need, but just stuck on one bit.

The scripts function is to copy files from multiple directories to others. Variables are set in an external VBS file in case the directories change in the future.

Initially the script prompts to user to confirm they want to run the file copy, then it gets the machine name for the output logfile at the end.

After that it loads an external VBS file which has the variables it needs for source and destination directories.

After that it uses Robocopy to make the file copies.

Finishing with a simple message box confirming everything is finished.

Here is the script.

Code:
'Message Box Prompt

a=MsgBox("This will copy the files",1, "Message Copy")

'Get Computer Name 
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName

'Enter path & VBSript name of external file

	Dim gsLibDir : gsLibDir = "C:\Users\jacobe\Documents\Scripts\"
	Dim goFS     : Set goFS = CreateObject( "Scripting.FileSystemObject" )
	ExecuteGlobal goFS.OpenTextFile( gsLibDir & "EFBRS.vbs" ).ReadAll()

'Robocopy Begin process

	Set objshell = CreateObject("WScript.Shell")
		'FOR MANUALS
			'Run Robocopy with logging
				objcommand = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath & " /E /TS /FP /LOG+:C:\log\%COMPUTERNAME%.log"
				objshell.run(objcommand)

		'FOR OPT Files 
			'Run Robocopy with logging
				objcommand = "Robocopy.exe " & sLocalDestinationPath2 & " " & sFinalDestinationPath2 & " /E /TS /FP /LOG+:C:\log\%COMPUTERNAME%.log"
				objshell.run(objcommand)

			'File copy from multiple sources into one destination				
				objcommand = "Robocopy.exe "  & sLocalDestinationPath & " " & sFinalDestinationPath3 & " /E /TS /FP /LOG+:C:\log\%COMPUTERNAME%.log"
				objshell.run(objcommand)
	
				objcommand = "Robocopy.exe "  & sLocalDestinationPath2 & " " & sFinalDestinationPath3 & " /E /TS /FP /LOG+:C:\log\%COMPUTERNAME%.log"
				objshell.run(objcommand)

				
'Copy Complete Message Box
result=MsgBox("Copy Complete",0, "Copy Status")

So this script works. It's been tested. The log file is great, tells you what was skipped/copied/failed etc.

However my boss wants to go one step further and this is where I get stuck. As it will be idio...sorry I mean users running this script. He wants an overall result displayed in the end message box. I've tried googling and cannot find a way to do this.

For example he wants it to show

"Actions finished

- 4 files copied
- 2 skipped
- 0 Failed

*OK*"

The idea being is that if the user gets a message that any of the copy actions have failed it shows them, so they can then proceed to contact us lot to check out the log file.

Is this possible?

Also quick note. I'm a script noob. So go easy on me!
 
Shouldnt be too hard mate, where abouts are you getting stuck with the message box showing the actions?

Stelly

Well from googling it seems like a standard "msgbox" has very limited properties/parameters you can mess around with. I am struggling with pulling the results into the message box, finding it difficult to find out where to start! :-/

Suggestions welcome.
 
The message you put into the message box should be able to be formatted before it goes in. Therefore you can format the string to go in with your results inserted. I don't use VB Net myself (use C# and other). I had a quick google for formatting string in vb net https://www.dotnetperls.com/format-vbnet.

An example with string format and msg box from another quick google http://stackoverflow.com/questions/15055217/how-to-pass-a-string-value-in-an-msgbox-vb-net.

Very similar to C# really, but I am not surprised :p.
 
Last edited:
But the thing with the script is that it outputs the results to a log file.

How can I string format the total results of all the file copies?

I understand how once it's captured that info as a string I can build that into the msgbox.

Sorry for the nooby questions.
 
Can't you read the log file for the copied file amounts, I would need to see the log file outputs but read them into there then calculate the files as needed?

Stelly
 
Heres a logfile output

Code:
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------
  Started : 4 de janeiro de 2017 11:11:14
   Source : C:\sourcefolder2\
     Dest : C:\destfolder2\
    Files : *.*
	    
  Options : *.* /TS /FP /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30 
------------------------------------------------------------------------------
	                   2	C:\sourcefolder2\
------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         2         0         2         0         0         0
   Bytes :       937         0       937         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : 4 de janeiro de 2017 11:11:14
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------
  Started : 4 de janeiro de 2017 11:11:14
   Source : C:\sourcefolder\
     Dest : C:\destfolder3\
    Files : *.*
	    
  Options : *.* /TS /FP /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30 
------------------------------------------------------------------------------
	                   2	C:\sourcefolder\
	  *EXTRA File 		     937 2017/01/03 14:57:39	C:\destfolder3\test source 2.contact
	  *EXTRA File 		       0 2017/01/03 14:51:16	C:\destfolder3\testsource2.txt
	                   0	C:\sourcefolder\1stcopytest\
------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         2         0         2         0         0         0
   Files :         2         0         2         0         0         2
   Bytes :       937         0       937         0         0       937
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : 4 de janeiro de 2017 11:11:14
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------
  Started : 4 de janeiro de 2017 11:11:15
   Source : C:\sourcefolder2\
     Dest : C:\destfolder3\
    Files : *.*
	    
  Options : *.* /TS /FP /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30 
------------------------------------------------------------------------------
	                   2	C:\sourcefolder2\
	*EXTRA Dir        -1	C:\destfolder3\1stcopytest\
	  *EXTRA File 		     927 2017/01/03 13:10:48	C:\destfolder3\1stcopytest.contact
	  *EXTRA File 		      10 2016/12/28 14:58:30	C:\destfolder3\testing1.txt
------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         1
   Files :         2         0         2         0         0         2
   Bytes :       937         0       937         0         0       937
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : 4 de janeiro de 2017 11:11:15
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------
  Started : 4 de janeiro de 2017 11:11:15
   Source : C:\sourcefolder\
     Dest : C:\destfolder\
    Files : *.*
	    
  Options : *.* /TS /FP /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30 
------------------------------------------------------------------------------
	                   2	C:\sourcefolder\
	                   0	C:\sourcefolder\1stcopytest\
------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         2         0         2         0         0         0
   Files :         2         0         2         0         0         0
   Bytes :       937         0       937         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : 4 de janeiro de 2017 11:11:15

Link to download if anyone wants it

http://jacobellwood.co.uk/public/log/LP10PTE5450-04.log
 
Last edited:
Think I understand what you're trying to do, here's a vbscript I use for deleting files and folders from a number of directories, depending on the file type -

Code:
' Script designed to sit in a folder with scripts and search all sub folders, deleting
' non-essential VUGen files and folders, with pop-up to summarise result.
'
'==============================================================================================

'Declare/set variables
Dim aFile
Dim i
deletedFileCount = 0
deletedFolderCount = 0

'Create objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSH = CreateObject("WScript.Shell")
On Error Resume Next
Set vuserFolder = objFSO.GetFolder(".")

'Display starting popup message
message = "This will remove non-essential VUGen files from subfolders."
proceed = objWSH.Popup (message, 0, "Tidy Scripts", 1)

'If ok to tidy, do work
If proceed = 1 Then
	tidyFolder(vuserFolder)

	'Display completion popup message
	message = "Deleted: " & deletedFileCount & " File(s) and " & deletedFolderCount & " Folder(s)"
	objWSH.Popup message, 0, "Tidying Scripts Complete", 64
End If

Sub tidyFolder(aFolder)
	'For each script folder
	For Each aFolder In aFolder.SubFolders
	
		'Except for shared folder
		If aFolder.Name <> "shared" Then
			'There maybe up to 5 result folders if VUGen locked results
			For i = 1 To 5
				If objFSO.FolderExists (aFolder & "\result" & i) = true Then
					killFolder (aFolder & "\result" & i)
				Else
					'If result1 does not exist, then assume result2 upwards does not either, so end loop
					i=6
				End If
			Next
		End If
		
		'Recursively check all sub folders
		tidyFolder(aFolder)
		
		'Except for shared folder
		If aFolder.Name <> "shared" Then
			For Each aFile In aFolder.Files
		 		If UCase(objFSO.GetExtensionName (aFile)) = "LOG" Then
					killFile (aFile)
				ElseIf UCase(objFSO.GetExtensionName (aFile)) = "BAK" Then
					killFile (afile)
				ElseIf UCase(objFSO.GetExtensionName (aFile)) = "IDX" Then
					killFile (afile)
				ElseIf UCase(objFSO.GetExtensionName (aFile)) = "CI" Then
					killFile (afile)
				ElseIf UCase(objFSO.GetFileName (aFile)) = "MDRV_CMD.TXT" Then
					killFile (afile)
				ElseIf UCase(objFSO.GetFileName (aFile)) = "OUTPUT.TXT" Then
					killFile (afile)
				ElseIf UCase(objFSO.GetFileName (aFile)) = "OPTIONS.TXT" Then
					killFile (afile)
				ElseIf UCase(objFSO.GetFileName (aFile)) = "DEBUG.INF" Then
					killFile (afile)
				End If
			Next
		End If
	Next
End Sub

Sub killFile (aFile)
	On Error Resume Next
	objFSO.DeleteFile (aFile)
	deletedFileCount = deletedFileCount + 1
End Sub

Sub killFolder (aFolder)
	On Error Resume Next
	objFSO.DeleteFolder (aFolder)
	deletedFolderCount = deletedFolderCount + 1
End sub

So I think you just need to declare variables such as copied, skipped and failed, and then just print these in your final message?
 
Back
Top Bottom