Gibbo Posted May 29, 2009 Share Posted May 29, 2009 In my quest to print files from Autoit I came across this: PrintAny.bat, Version 2.00expandcollapse popup@ECHO OFF :: For Windows NT 4 or later only IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Localize variables SETLOCAL SET PrintCmd= SET FileType= SET Temp=%Temp:"=% :: Command line parsing IF NOT "%~2"=="" GOTO Syntax IF "%~1"=="" GOTO Syntax IF "%~n1"=="" GOTO Syntax IF "%~x1"=="" GOTO Syntax ECHO."%~1" | FIND "/" >NUL && GOTO Syntax ECHO."%~1" | FIND "?" >NUL && GOTO Syntax ECHO."%~1" | FIND "*" >NUL && GOTO Syntax IF NOT EXIST "%~1" GOTO Syntax :: Check if REG 3.0 and FINDSTR are available REG /? 2>&1 | FINDSTR /R /C:"3\.0" >NUL 2>&1 || GOTO Syntax :: Get the file association from the registry FOR /F "tokens=1* delims==" %%A IN ('ASSOC %~x1') DO ( FOR /F "tokens=1 delims==" %%C IN ('FTYPE ^| FIND /I "%%~B"') DO ( CALL :GetPrintCommand %%~C ) ) :: Check if a print command was found IF NOT DEFINED PrintCmd GOTO NoAssoc :: Print the file using the print command we just found CALL START /MIN "PrintAny" %PrintCmd% :: Done GOTO End :GetPrintCommand :: Get the print command for this file type from the registry FOR /F "tokens=3*" %%D IN ('REG.EXE Query HKCR\%1\shell\print\command /ve 2^>NUL') DO SET PrintCmd=%%E IF NOT DEFINED PrintCmd GOTO:EOF :: "Unescape" the command SET PrintCmd=%PrintCmd:\"="% SET PrintCmd=%PrintCmd:""="% SET PrintCmd=%PrintCmd:\\=\% :: Remove double double quotes in file name if applicable ECHO.%PrintCmd% | FINDSTR.EXE /R /C:"\"%%1\"" >NUL && SET PrintCmd=%PrintCmd:"%1"="%%%~1"% GOTO:EOF :NoAssoc CLS ECHO. ECHO Sorry, this batch file works only for known file types with associated ECHO print commands defined in the registry hive HKEY_CLASSES_ROOT. ECHO No print command seems to be assiociated with %~x1 files on this computer. ECHO. :Syntax ECHO. ECHO PrintAny.bat, Version 2.00 for Windows NT 4 and later ECHO Prints any known file type from the command line ECHO. ECHO Usage: PRINTANY file_to_print ECHO. ECHO Where: "file_to_print" is the name of the file to be printed ECHO (use double quotes for long file names) ECHO. ECHO Notes: This batch file works only if the file type's print command is ECHO defined in the registry hive HKEY_CLASSES_ROOT. ECHO This batch file uses REG 3.0 and will fail with older versions. ECHO This batch file uses FINDSTR too. Windows NT 4 users need to check ECHO for the availability of both REG and FINDSTR (Resource Kit). ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com :End IF "%OS%"=="Windows_NT" ENDLOCALI was hoping to gather some input from the gurus here as to whether this would convert over to pure Autoit (no external programs needed)Not quite sure where to start... Link to comment Share on other sites More sharing options...
NiVZ Posted May 29, 2009 Share Posted May 29, 2009 here's the Pseudo-code you need to do: 1. Read the command line parameter for the file you want to print 2. Get the file extension (characters after the last '.') 3. Check the registry to see if that file type exists under HKEY_CLASSES_ROOT 4. If it does, get the corresponding file class name from the 'default' value 5. Look up the "HKEY_CLASSES_ROOT\<file class name>\Shell\Print\Command" 6. The 'default' value under here is the print command 7. If there is a %1 replace it with the file name, and if not just add filename to the end Here's a quick go I had at it - remember to compile it as a console app so you can run it from command prompt. expandcollapse popup; You should compile this script as a "CONSOLE" app ; Check we have exactly 1 Command Line parameter If $CMDLine[0] <> 1 Then ; Show Usage screen Usage() Else ; Get the File Extension, eg .txt $FileType = StringMid($CMDLine[1], StringInStr($CMDLine[1], ".", "", -1), StringLen($CMDLine[1]) - StringInStr($CMDLine[1], ".", "", -1) + 1) ; Call the function to lookup the print command from the registry $PrintCommand = GetPrintCommand($FileType) If $PrintCommand = -1 Then ; Unregistered filetypr ConsoleWrite("Unregistered Filetype: " & $FileType & @CRLF) ElseIf $PrintCommand = -2 Then ; No print command ConsoleWrite("No print command associated with Filetype: "& $FileType & @CRLF) Else ; If there is a %1 then replace it with the command line filename If StringInStr($PrintCommand, "%1") Then $PrintCommand = StringReplace($PrintCommand, "%1", $CMDLine[1]) Else ; If there is no %1 then just add the command line filename to the end of the print command $PrintCommand &= " " & $CMDLine[1] EndIf ; Write the print command for debugging ConsoleWrite(@CRLF & "PrintAny Running Command: " & $PrintCommand & @CRLF) ; Now run the command Run(@COMSPEC & " /c " & $PrintCommand, @SystemDir, @SW_SHOW) EndIf EndIf ; -- User Functions ---------- Func GetPrintCommand($inFileType) ; Get associated file class - need this to get print command $FileClass = RegRead("HKEY_CLASSES_ROOT\" & $inFileType, "") If @error = 1 Then ; File Type is not registered Return -1 Else ; Try to get the Shell Print command $PrintCommand = RegRead("HKEY_CLASSES_ROOT\" & $FileClass & "\Shell\Print\Command", "") If $PrintCommand = "" Then ; No associated Print command Return -2 Else ; We have the print command Return $PrintCommand EndIf EndIf EndFunc Func Usage() ; Show a usage screen ConsoleWrite(@CRLF & "Usage: PRINTANY file_to_print " & @CRLF & @CRLF) EndFunc NiVZ Link to comment Share on other sites More sharing options...
Gibbo Posted June 2, 2009 Author Share Posted June 2, 2009 Thats Fantastic! Has helped heaps! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now