Processing a Folder of Files

A few days ago I was speaking to one of my users about creating some PDF files from Microsoft Word and securing them so that they can’t be copied or printed.  Rather than using expensive software from Adobe or a generic creator like CutePDF I recommended using the add-in for Word 2007 that Microsoft provide as it provides some useful accessibility options (thanks to JISC TechDis for highlighting that one to me).

Unfortunately the add-in doesn’t have an option to secure a PDF so that’s where using a tool like PDFTK can help.  Being a command line tool though and the user having to produce more than 100 PDFs meant that a little help could come in handy.   Making use of the select folder VBScript I refined a little while ago, I created a generic VBScript to take a folder selection from a user and a command line operation via a simply single line input box to then process every file in the folder using the command line.

A percentage symbol is used as a place holder for the file path to be substituted into and any file paths for the command line (with the exception of the substitution - %) should include double quotes where there are spaces in the path.

Option Explicit

Dim strFolder, strRootFolder, strBaseCommand

'Get the information and process it
strRootFolder = SelectFolder("Select folder containing files to be processed:")
strBaseCommand = InputBox("Enter the command line to run against all files." & vbCrLf & "Place a percentage symbol (%) where the filename will be substituted")
ProcessFolder strRootFolder

'------------
'SUB ROUTINES
'------------

'Identify each file in a folder and run the specified command against it.
Sub ProcessFolder(pstrFolder)
Dim objCurrentFolder, objFile
Dim colFilesInFolder
Dim strCommand
Dim objWSHShell, objFSO

'Initialise
Set objWshShell = Wscript.CreateObject("Wscript.Shell")
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objCurrentFolder = objFSO.GetFolder(pstrFolder)
Set colFilesInFolder = objCurrentFolder.Files

For Each objFile in colFilesInFolder
strCommand = Replace(strBaseCommand,"%","""" & objFile.Path & """")
objWSHShell.Run strCommand
Next
End Sub

'---------
'FUNCTIONS
'---------

Function SelectFolder(pstrDialogLabel)
'Select a folder
Const BIF_returnonlyfsdirs = &H0001
Const BIF_editbox = &H0010

Dim objBrowseFolderDialog, objFolder, objFSO, objSelection
Dim bBrowseForFolder

Set objBrowseFolderDialog = WScript.CreateObject("Shell.Application")

bBrowseForFolder = true

While bBrowseForFolder
Set objFolder = objBrowseFolderDialog.BrowseForFolder (&H0, pstrDialogLabel, BIF_editbox + BIF_returnonlyfsdirs)

'Check that something has been returned
If IsValidFolder(objFolder) Then
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objSelection = objFolder.Self
If objFSO.FolderExists(objSelection.Path) Then
'A valid folder has been selected
SelectFolder = objSelection.Path
bBrowseForFolder = false
Else
'The selection is not a valid folder, try again...
MsgBox objFolder.Title & " is not a valid folder, please select another folder" _
, vbOKOnly & vbExclamation, "Invalid Selection"
End If
Else
'Nothing was selected, so return a null string
SelectFolder = ""
bBrowseForFolder = false
End If
Wend
End Function

Function IsValidFolder(pobjFolder)
'Check that we have a valid value
'i.e. you can concatenate it to a string
Dim strTest

On Error Resume Next

strTest = " " & pobjFolder

If Err <> 0 Then
IsValidFolder = false
Else
IsValidFolder = true
End If

On Error GoTo 0
End Function

As it stands the folder just processes a single folder for all files (which was what I wanted), but simple amendments could allow this script to cater for specific file types (or patterns) and sub folders (a recursive call in the ProcessFolder() routine would allow this).

Author: Stephen Millard
Tags: | vbs |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read