DesktopX: Using External Files (Advanced)
From WinCustomize Wiki
| Using External Files (Advanced) | ||||||||
| ||||||||
Contents |
[edit] DesktopX: Using External Files (Advanced)
This tutorial will show you how to create, load and interact with external files from within the DesktopX Script Builder.
[edit] Before Starting
The user should be familiar with the use of DesktopX Builder in Scripting Mode and have at the least a basic understanding of scripting.
[edit] Overview
Two methods currently exist that allow the user to access external files. Both have significant usefulness depending on your scripting requirements
[edit] Step 1: Creating the External File
An external file can be either script (vbs,js) or a simple text file. If it is not well formed script this will result in errors occurring within DesktopX. The best possible way to avoid these errors is to use a script editor or DesktopX's built in script editor then copy and paste the script into notepad and save.
To begin let's open up notepad or your preferred script editor and add/create the following basic FileSystemObject(FSO) functions. Save the file as either fsofunctions.vbs or fsofunctions.txt.
These functions should be easy to understand based on how they have been named.
[edit] External File Style 1
In this external script example notice how the FSO object is created & closed within each function.
'Declaring variables here works the same as declaring them within the DesktopX Script
'These variables are declared globally as we will be using them in other functions or subroutines
Dim oFilePath,oFileParent,oFileFullName,oFileName,oFileExt 'parseFile Function Variables
Dim oFldrPath,oFldrParent,oFldrName 'parseFolder Variables
Function parseFilename(iFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(iFile)
oFilePath = objFSO.GetAbsolutePathName(objFile)
oFileParent = objFSO.GetParentFolderName(objFile)
oFileFullName = objFSO.GetFileName(objFile)
oFileName = objFSO.GetBaseName(objFile)
oFileExt = objFSO.GetExtensionName(objFile)
Set objFile = nothing
Set objFSO=nothing
End Function
Function parseFoldername(iFldr)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(iFldr)
oFldrPath = objFSO.GetAbsolutePathName(objFolder)
oFldrParent = objFSO.GetParentFolderName(objFolder)
oFldrName = objFSO.GetBaseName(objFolder)
Set objFolder = nothing
Set objFSO=nothing
End Function
Function copyFile(iFile,oFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile iFile,oFile
Set objFSO=nothing
End Function
Function moveFile(iFile,oFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile iFile,oFile
Set objFSO=nothing
End Function
Function deleteFile(iFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(iFile)
Set objFSO=nothing
End Function
Function createFolder(iFldr,oFldrName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
fldrPath=objFSO.GetAbsolutePathName(iFldr)
fldrNew=fldrPath&"\"& oFldrName
objFSO.CreateFolder(fldrNew)
Set objFSO=nothing
End Function
Function copyFolder(iFldr,oFldr)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder iFldr,oFldr
Set objFSO=nothing
End Function
Function moveFolder(iFldr,oFldr)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder iFldr,oFldr
Set objFSO=nothing
End Function
Function deleteFolder(iFldr)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(iFldr)
Set objFSO=nothing
End Function
[edit] External File Style 2
In this second example of an external script notice how the FSO object is not created & closed within each function. Before calling any of these functions you need to Create the FSO Object using the global variable "objFSO".
'Declaring variables here works the same as declaring them within the DesktopX Script
'These variables are declared globally as we will be using them in other functions or subroutines
Dim oFilePath,oFileParent,oFileFullName,oFileName,oFileExt 'parseFile Function Variables
Dim oFldrPath,oFldrParent,oFldrName 'parseFolder Variables
Function parseFilename(iFile)
Set objFile = objFSO.GetFile(iFile)
oFilePath = objFSO.GetAbsolutePathName(objFile)
oFileParent = objFSO.GetParentFolderName(objFile)
oFileFullName = objFSO.GetFileName(objFile)
oFileName = objFSO.GetBaseName(objFile)
oFileExt = objFSO.GetExtensionName(objFile)
Set objFile = nothing
End Function
Function parseFoldername(iFldr)
Set objFolder = objFSO.GetFolder(iFldr)
oFldrPath = objFSO.GetAbsolutePathName(objFolder)
oFldrParent = objFSO.GetParentFolderName(objFolder)
oFldrName = objFSO.GetBaseName(objFolder)
Set objFolder = nothing
End Function
Function copyFile(iFile,oFile)
objFSO.CopyFile iFile,oFile
End Function
Function moveFile(iFile,oFile)
objFSO.MoveFile iFile,oFile
End Function
Function deleteFile(iFile)
objFSO.DeleteFile(iFile)
End Function
Function createFolder(iFldr,oFldrName)
fldrPath=objFSO.GetAbsolutePathName(iFldr)
fldrNew=fldrPath&"\"& ofldrName
objFSO.CreateFolder(fldrNew)
End Function
Function copyFolder(iFldr,oFldr)
objFSO.CopyFolder iFldr,oFldr
End Function
Function moveFolder(iFldr,oFldr)
objFSO.MoveFolder iFldr,oFldr
End Function
Function deleteFolder(iFldr)
objFSO.DeleteFolder(iFldr)
End Function
[edit] Warning
The delete functions bypass the recycle bin meaning that your file is permanently deleted. If you wish to send files or folders to the recycle bin then you need to use the Shell.Application method on Windows XP.
[edit] Step 2: Loading the External File
[edit] Method 1: DesktopX External Script Feature
- Create a new object and name it objScriptTest.
- Under Object Properties tab select the new button in the script section.
- From the Script Menu in the DesktopX Script Editor select the last menu entry titled "External Script". The browse for folder dialog should appear.
- Browse to your external file, select it and press the open button.
- In the Script Editor underneath the menu bar you should see the script language and the full path to the selected script file. See image below:
[edit] Method 2: Include Function
Essentially what the Include Function does is the same as DesktopX's internal feature. It reads the external file into memory.
Where the advantage lies is that you are able to use it to access more than one external file or you can use it in conjunction with the internal feature.
'Include Function
Dim objFSO, objFile, strFile
Function Include(vbsFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(vbsFile)
strFile = objFile.ReadAll()
objFile.Close
ExecuteGlobal strFile
Set objFSO = Nothing
Set objFile = Nothing
End Function
[edit] Load External Script Using the Include Function
In order to load the external file using the Include Function you need to use a Call Statement. Depending on your script size the best solution is to put the Call Statement within the Sub Object_OnEnter event as shown in the code below.
'Called when the script is executed Sub Object_OnScriptEnter Dim myFile 'The path to your external script file myFile="C:\fsofunctions.vbs" Call Include(myFile) End Sub
[edit] Script Notes
The "Include Function" code specifically allows you to load only one file at a time. Each time the function is called the original file string is replaced.
If for some reason you need to load more than one external file at the same time then the best solution is to construct an array. Or a less "clean" solution is the creation of a second Include Function and change "strFile" to "strFile2" within this new function.
[edit] Step 3: Interacting with the External File
[edit] Overview: Call Statement
Once the External File is loaded you can use a normal Call Statement placed within a sub-routine or function the same way you would script normally.
Here are a few examples.
Using External File Style 1 as your external file this code creates a copy of a file using the copyFile Function.
'Sample call from main script
'Copies the test.txt file to the new file test_copy.txt
Call copyFile("C:\test.txt","C:\test_copy.txt")
Using External File Style 2 this code uses the moveFile Function to move a file to a new folder.
'Sample call from main script
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Moves the file test.txt from the C drive to the existing sub folder sorted.
Call moveFile("C:\test.txt","C:\sorted\test.txt")
Set objFSO=nothing
[edit] Examples
[edit] Overview: Example Scripts
These Examples use the External File Style 1 external script above.
In order to keep this article to a reasonable size most of these examples will use predefined variables, simple input boxes and Browse for folder dialog boxes.
DesktopX makes available a number of ways for your objects, widgets or gadgets to receive input from the user. Some of the most popular methods include using forms, widget preferences, ActiveX controls or various input/drop objects.
[edit] Basic Example
[edit] Copy or Move a File
From the DesktopX Script Editor use Method 1: DesktopX External Script Feature to load the external file into memory.
'Called when a file is dropped onto object
Function Object_OnDropFiles(file)
'Parses file path in order to retrieve original filename and extension
Call parseFilename(file)
'Launch Browse for folders box
targetFldr = System.FolderDialog("", "", &H4000)'--Browse for all folders
'Verifies that a folder was select then builds a string from the selected folder
'and previously parsed drop file path
If targetFldr <> "" Then
targetPath=targetFldr&"\"&oFileName&"."&oFileExt
'This function copies the file. Comment this line in order to use movefile
Call copyFile(file,targetPath)
'Uncomment this line to move the file
'Call moveFile(file,targetPath)
Else
msgbox "You didn't select a folder."
End If
End Function
[edit] Advanced Example
[edit] Copy or Move Multiple Files
This script uses the Method 2: Include Function to load the external file into memory.
'Include Function
Dim objFSO, objFile, strFile
Function Include(vbsFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(vbsFile)
strFile = objFile.ReadAll()
objFile.Close
ExecuteGlobal strFile
Set objFSO = Nothing
Set objFile = Nothing
End Function
Dim arrfiles
'Called when a file is dropped onto object
Function Object_OnDropFiles(files)
'Sets counter to zero
i=0
'Splits dropped files into an array
arrFiles=Split(files,"|")
'Launch Browse for folders dialog box
targetFldr = System.FolderDialog("", "", &H4000)'--Browse for all folders
'Verifies that a folder was selected. If one wasn't then exits
If targetFldr <> "" Then
For x=0 To UBound(arrFiles)
'Increases counter for each file
i=i+1
'Gets the next file from array
oFile=arrFiles(x)
'Parses file path in order to retrieve original filename and extension
Call parseFilename(oFile)
'Builds the path to copy/move the file to
targetPath=targetFldr&"\"&oFileName&"."&oFileExt
'This function copies the file. Comment this line in order to use movefile
Call copyFile(oFile,targetPath)
'Uncomment this line to move the file
'Call moveFile(oFile,targetPath)
Next
ElseIf targetFlder = "" Then
msgbox "Please select a folder."
Exit Function
End If
'Msgbox tells you how many files you copied or moved
msgbox "You copied or moved "& i &" files"&vbcrlf&"To: "&targetFldr
End Function
'Called when the script is executed
Sub Object_OnScriptEnter
Call Include("C:\fsofunctions.vbs")
End Sub


