Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista Tutorial - Opening a file of known path but different extension

Reply
 
Old 07-17-2008   #1 (permalink)
zilog jones
Guest


 
 

Opening a file of known path but different extension

Hi, I've made a script for running the command-line program pdflatex.
The script uses a file open dialogue for the user to choose a TeX
file, which pdflatex will convert to PDF.

After running pdflatex I would like the script to open the file in
Adobe Reader (AcroRd32 <filename>). The script knows what the
input .tex file is, and the PDF file output from pdflatex will have
the same name and path but .pdf extenstion. How can I get the string
of the input file and change the extension to .pdf to put into a run
method? I have found scripts for changing file extensions but these
all seem to use explicit paths (e.g. here:
http://www.microsoft.com/technet/scr...7/hey0601.mspx).
I assume I can do something with regular expressions but I'm lost as
to how to use them in this case.

Here's my script so far:

----------------------------------------------------------------------

'Create shell object
Set WshShell = WScript.CreateObject ("WSCript.shell")


StartBox


Function GetFileName( myDir, myFilter )
' This function opens a File Open Dialog and returns the
' fully qualified path of the selected file as a string.
'
' Arguments:
' myDir is the initial directory; if no directory is
' specified "My Documents" is used;
' NOTE: this default requires the WScript.Shell
' object, and works only in WSH, not in HTAs!
' myFilter is the file type filter; format "File type description|
*.ext"
' ALL arguments MUST get A value (use "" for defaults), OR otherwise
you must
' use "On Error Resume Next" to prevent error messages.
'
' Dependencies:
' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and
later.
' To use the default "My Documents" WScript.Shell is used, which isn't
' available in HTAs.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Standard housekeeping
Dim objDialog

' Create a dialog object
Set objDialog = CreateObject( "UserAccounts.CommonDialog" )



' Check arguments and use defaults when necessary
If myDir = "" Then
' Default initial folder is "My Documents"
objDialog.InitialDir =
CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
Else
' Use the specified initial folder
objDialog.InitialDir = myDir
End If
If myFilter = "" Then
' Default file filter is "All files"
objDialog.Filter = "All files|*.*"
Else
' Use the specified file filter
objDialog.Filter = myFilter
End If

' Open the dialog and return the selected file name
If objDialog.ShowOpen Then
GetFileName = objDialog.FileName

Else
WScript.Quit
End If
End Function

Function CheckFileExists(sFileName)
'Function for checking file exists. It returns 'True' if the file
exists, or 'False' if it does not.

Dim FileSystemObject

Set FileSystemObject =
WScript.CreateObject("Scripting.FileSystemObject")

If (FileSystemObject.FileExists(sFileName)) Then
CheckFileExists = True
Else
CheckFileExists = False
End If

Set FileSystemObject = Nothing

End Function

Sub StartBox( )
'Start UI

Dim objPrnDlg, strPrompt, strTitle

Const vbOK = 1
Const vbCancel = 2
Const vbAbort = 3
Const vbRetry = 4
Const vbIgnore = 5
Const vbYes = 6
Const vbNo = 7

'Start prompt - if user chooses OK file browse dialogue is opened
strPrompt = "This will convert a TeX file to PDF." & VbCr & "Press
OK to browse for file:"
strTitle = "Pdflatex Script"
If MsgBox( strPrompt, vbOKCancel, strTitle ) = vbOK Then
'Check if pdflatex exists. If true open file dialogue and run
pdflatex on file, else give error message
'**NOTE**: Script will only work if pdflatex is in the path shown
below. Add an or statement for other possible instances
If CheckFileExists("C:\Program Files\MiKTeX 2.7\miktex\bin
\pdflatex.exe") Then

'Prompt for file selection and run pdflatex on file
WshShell.Run "pdflatex " & Chr(34) & GetFileName( "", "TeX files|
*.tex" ) & Chr(34), 1, True
set WshShell = nothing

Else
WScript.Echo("Cannot find pdflatex program. Please make sure you
have MikTex 2.7 installed.")

End If

End If
End Sub

My System SpecsSystem Spec
Old 07-17-2008   #2 (permalink)
Pegasus \(MVP\)
Guest


 
 

Re: Opening a file of known path but different extension


"zilog jones" <zilog.jones@xxxxxx> wrote in message
news:740e7125-d48f-405a-8441-f0d65ce4f347@xxxxxx
Quote:

> Hi, I've made a script for running the command-line program pdflatex.
> The script uses a file open dialogue for the user to choose a TeX
> file, which pdflatex will convert to PDF.
>
> After running pdflatex I would like the script to open the file in
> Adobe Reader (AcroRd32 <filename>). The script knows what the
> input .tex file is, and the PDF file output from pdflatex will have
> the same name and path but .pdf extenstion. How can I get the string
> of the input file and change the extension to .pdf to put into a run
> method? I have found scripts for changing file extensions but these
> all seem to use explicit paths (e.g. here:
> http://www.microsoft.com/technet/scr...7/hey0601.mspx).
> I assume I can do something with regular expressions but I'm lost as
> to how to use them in this case.
>
> Here's my script so far:
>
> ----------------------------------------------------------------------
>
> 'Create shell object
> Set WshShell = WScript.CreateObject ("WSCript.shell")
>
>
> StartBox
>
>
> Function GetFileName( myDir, myFilter )
> ' This function opens a File Open Dialog and returns the
> ' fully qualified path of the selected file as a string.
> '
> ' Arguments:
> ' myDir is the initial directory; if no directory is
> ' specified "My Documents" is used;
> ' NOTE: this default requires the WScript.Shell
> ' object, and works only in WSH, not in HTAs!
> ' myFilter is the file type filter; format "File type description|
> *.ext"
> ' ALL arguments MUST get A value (use "" for defaults), OR otherwise
> you must
> ' use "On Error Resume Next" to prevent error messages.
> '
> ' Dependencies:
> ' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and
> later.
> ' To use the default "My Documents" WScript.Shell is used, which isn't
> ' available in HTAs.
> '
> ' Written by Rob van der Woude
> ' http://www.robvanderwoude.com
>
> ' Standard housekeeping
> Dim objDialog
>
> ' Create a dialog object
> Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
>
>
>
> ' Check arguments and use defaults when necessary
> If myDir = "" Then
> ' Default initial folder is "My Documents"
> objDialog.InitialDir =
> CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
> Else
> ' Use the specified initial folder
> objDialog.InitialDir = myDir
> End If
> If myFilter = "" Then
> ' Default file filter is "All files"
> objDialog.Filter = "All files|*.*"
> Else
> ' Use the specified file filter
> objDialog.Filter = myFilter
> End If
>
> ' Open the dialog and return the selected file name
> If objDialog.ShowOpen Then
> GetFileName = objDialog.FileName
>
> Else
> WScript.Quit
> End If
> End Function
>
> Function CheckFileExists(sFileName)
> 'Function for checking file exists. It returns 'True' if the file
> exists, or 'False' if it does not.
>
> Dim FileSystemObject
>
> Set FileSystemObject =
> WScript.CreateObject("Scripting.FileSystemObject")
>
> If (FileSystemObject.FileExists(sFileName)) Then
> CheckFileExists = True
> Else
> CheckFileExists = False
> End If
>
> Set FileSystemObject = Nothing
>
> End Function
>
> Sub StartBox( )
> 'Start UI
>
> Dim objPrnDlg, strPrompt, strTitle
>
> Const vbOK = 1
> Const vbCancel = 2
> Const vbAbort = 3
> Const vbRetry = 4
> Const vbIgnore = 5
> Const vbYes = 6
> Const vbNo = 7
>
> 'Start prompt - if user chooses OK file browse dialogue is opened
> strPrompt = "This will convert a TeX file to PDF." & VbCr & "Press
> OK to browse for file:"
> strTitle = "Pdflatex Script"
> If MsgBox( strPrompt, vbOKCancel, strTitle ) = vbOK Then
> 'Check if pdflatex exists. If true open file dialogue and run
> pdflatex on file, else give error message
> '**NOTE**: Script will only work if pdflatex is in the path shown
> below. Add an or statement for other possible instances
> If CheckFileExists("C:\Program Files\MiKTeX 2.7\miktex\bin
> \pdflatex.exe") Then
>
> 'Prompt for file selection and run pdflatex on file
> WshShell.Run "pdflatex " & Chr(34) & GetFileName( "", "TeX files|
> *.tex" ) & Chr(34), 1, True
> set WshShell = nothing
>
> Else
> WScript.Echo("Cannot find pdflatex program. Please make sure you
> have MikTex 2.7 installed.")
>
> End If
>
> End If
> End Sub
You could replace .tex with .pdf:
Filename = replace(Filename, ".tex", ".pdf")


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Extracting drive, path filebasename or extension from complete filename? VB Script
opening .exe extension files just hang. Software
opening mkv extension General Discussion
Opening .PPS Extension Files In Windows Mail Vista mail
How do you change file name extension in File/Dir Explorer? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46