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/scriptcenter/resources/qanda/jun07/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

Re: Opening a file of known path but different extension by Pegasus

Pegasus
Thu Jul 17 06:57:42 CDT 2008


"zilog jones" <zilog.jones@gmail.com> wrote in message
news:740e7125-d48f-405a-8441-f0d65ce4f347@p25g2000hsf.googlegroups.com...
> 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/scriptcenter/resources/qanda/jun07/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")