How to execute batch file with a pathname using Javascript? (Windows XP and IE)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huiling25
    New Member
    • Dec 2006
    • 35

    How to execute batch file with a pathname using Javascript? (Windows XP and IE)

    I tried for several days, but none can work. IE keep showing yellow exclamation mark at the status bar after clicking "Submit" and batch file cannot be executed. The HTML file and batch file is in the same folder.

    Here is the code for printpage.html
    Code:
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    function printFile()
    {
       var pg = document.getElementById("page").value;
     
    var shell = new ActiveXObject("WScript.Shell"); 
    
    shell.run( '"printfile.bat" ' + pg, 1, true ); 
    
    }
    
    </script>
    </head>
    <body>
    <form>
    <input id="page" type="file"/> <br/>
    <input type="button" onclick="printFile();" value="Submit"/>
    </form>
    </body
    </html>
    Here is the code for printfile.bat
    Code:
    @ECHO ON
    ECHO %1  
    PAUSE
  • huiling25
    New Member
    • Dec 2006
    • 35

    #2
    I have found another way to print files from a folder. However, it can't print .prn or .doc files.

    Here is the code of "printall.v bs"

    Code:
    set shApp = createobject("shell.application") 
    set shFolder = shApp.namespace("C:\Documents and Settings\Administrator\Desktop\Form 
    Generator\PrintAll") 
    set shItems = shFolder.Items() 
    for each shItem in shItems 
    
     shItem.invokeverb "&Print" 
    
    next
    I tried to create if-statements to cater for .prn and .doc, but i only have a vague idea to do it. So, it can't work.

    Code:
    set shApp = createobject("shell.application") 
    set shFolder = shApp.namespace("C:\Documents and Settings\Administrator\Desktop\Form Generator\PrintAll") 
    set shItems = shFolder.Items() 
    for each shItem in shItems 
    
    
    If shItem.Extension = "prn" Then
    
    set wshshell=createobject("wscript.shell")
    
    iret=wshshell.run("%comspec% /c copy /b " & chr(34) & prnfilespec & chr(34) & " prn",0,false)set 
    
    wshshell=nothing
    
    ElseIf shItem.Extension = "doc" Then
    
    wordfilespec=shItem   
    
    set oword=createobject("word.application")on error resume next
    with oword    
       .visible=false    
       .documents.open wordfilespec   
       .printout    
    do until .backgroundprintingstatus=0       
        wscript.sleep 100   
       loopend withoword.activedocument.close 
       falseoword.quitset 
       oword=nothingon error goto 0
    
    Else 
    
     shItem.invokeverb "&Print" 
    
    End If
    
     
    next

    Comment

    • huiling25
      New Member
      • Dec 2006
      • 35

      #3
      Code to print documents with 2 printers

      I managed to come out with the code. But when the folder "PrintAll" has 4 documents, the computer will hang and spool error will occur. If "PrintAll" folder has only 1 document, this code run smoothly.


      Code:
      Set WshNetwork = WScript.CreateObject("WScript.NetWork") 
      
      
      set shApp = createobject("shell.application") 
      set shFolder = shApp.namespace("C:\Documents and Settings\Administrator\Desktop\Form Generator\PrintAll") 
      set shItems = shFolder.Items()
      
      
      Dim strInfo
      
      Sub printFile()
      
      for each shItem in shItems 
      
      If shItem.Type = "doc" Then
      
      wordfilespec=shItem   
      
      Set objWord = CreateObject("Word.Application")
      Set objDoc = objWord.Documents.Open(shItem)
      
      objDoc.PrintOut()
      objWord.Quit
      
      
      Else 
      
       shItem.invokeverb "&Print" 
      
      
      End If
      
      Next
      
      
      End Sub
      
      
      Set oPrinters = WshNetwork.EnumPrinterConnections
      
      
      strInfo = "Network printer mappings:"
      
      For i = 0 to oPrinters.Count - 1 Step 2
      
             
      strInfo = strInfo & vbcr & "Port: " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1) 
         
      
      
      If oPrinters.Item(i+1)="HP Officejet Pro 8500 A909a Series" Then
      
      
      printFile()
      End If
      If oPrinters.Item(i+1)="HP Officejet Pro 8500 A909a Series(Copy 1)" Then
      
      printFile()
      
      
      End If
      
      WshNetwork.SetDefaultPrinter(oPrinters.Item(i+1))
      
      WScript.Echo oPrinters.Item(i+1)
      
      
      Next
      
      MsgBox strInfo

      Comment

      Working...