473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript" type="text/javascript">
  4. function printFile()
  5. {
  6.    var pg = document.getElementById("page").value;
  7.  
  8. var shell = new ActiveXObject("WScript.Shell"); 
  9.  
  10. shell.run( '"printfile.bat" ' + pg, 1, true ); 
  11.  
  12. }
  13.  
  14. </script>
  15. </head>
  16. <body>
  17. <form>
  18. <input id="page" type="file"/> <br/>
  19. <input type="button" onclick="printFile();" value="Submit"/>
  20. </form>
  21. </body
  22. </html>
Here is the code for printfile.bat
Expand|Select|Wrap|Line Numbers
  1. @ECHO ON
  2. ECHO %1  
  3. PAUSE 
  4.  
Aug 2 '10 #1
2 13945
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.vbs"

Expand|Select|Wrap|Line Numbers
  1. set shApp = createobject("shell.application") 
  2. set shFolder = shApp.namespace("C:\Documents and Settings\Administrator\Desktop\Form 
  3. Generator\PrintAll") 
  4. set shItems = shFolder.Items() 
  5. for each shItem in shItems 
  6.  
  7.  shItem.invokeverb "&Print" 
  8.  
  9. next
  10.  
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.

Expand|Select|Wrap|Line Numbers
  1. set shApp = createobject("shell.application") 
  2. set shFolder = shApp.namespace("C:\Documents and Settings\Administrator\Desktop\Form Generator\PrintAll") 
  3. set shItems = shFolder.Items() 
  4. for each shItem in shItems 
  5.  
  6.  
  7. If shItem.Extension = "prn" Then
  8.  
  9. set wshshell=createobject("wscript.shell")
  10.  
  11. iret=wshshell.run("%comspec% /c copy /b " & chr(34) & prnfilespec & chr(34) & " prn",0,false)set 
  12.  
  13. wshshell=nothing
  14.  
  15. ElseIf shItem.Extension = "doc" Then
  16.  
  17. wordfilespec=shItem   
  18.  
  19. set oword=createobject("word.application")on error resume next
  20. with oword    
  21.    .visible=false    
  22.    .documents.open wordfilespec   
  23.    .printout    
  24. do until .backgroundprintingstatus=0       
  25.     wscript.sleep 100   
  26.    loopend withoword.activedocument.close 
  27.    falseoword.quitset 
  28.    oword=nothingon error goto 0
  29.  
  30. Else 
  31.  
  32.  shItem.invokeverb "&Print" 
  33.  
  34. End If
  35.  
  36.  
  37. next
  38.  
  39.  
Aug 2 '10 #2
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.


Expand|Select|Wrap|Line Numbers
  1. Set WshNetwork = WScript.CreateObject("WScript.NetWork") 
  2.  
  3.  
  4. set shApp = createobject("shell.application") 
  5. set shFolder = shApp.namespace("C:\Documents and Settings\Administrator\Desktop\Form Generator\PrintAll") 
  6. set shItems = shFolder.Items()
  7.  
  8.  
  9. Dim strInfo
  10.  
  11. Sub printFile()
  12.  
  13. for each shItem in shItems 
  14.  
  15. If shItem.Type = "doc" Then
  16.  
  17. wordfilespec=shItem   
  18.  
  19. Set objWord = CreateObject("Word.Application")
  20. Set objDoc = objWord.Documents.Open(shItem)
  21.  
  22. objDoc.PrintOut()
  23. objWord.Quit
  24.  
  25.  
  26. Else 
  27.  
  28.  shItem.invokeverb "&Print" 
  29.  
  30.  
  31. End If
  32.  
  33. Next
  34.  
  35.  
  36. End Sub
  37.  
  38.  
  39. Set oPrinters = WshNetwork.EnumPrinterConnections
  40.  
  41.  
  42. strInfo = "Network printer mappings:"
  43.  
  44. For i = 0 to oPrinters.Count - 1 Step 2
  45.  
  46.  
  47. strInfo = strInfo & vbcr & "Port: " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1) 
  48.  
  49.  
  50.  
  51. If oPrinters.Item(i+1)="HP Officejet Pro 8500 A909a Series" Then
  52.  
  53.  
  54. printFile()
  55. End If
  56. If oPrinters.Item(i+1)="HP Officejet Pro 8500 A909a Series(Copy 1)" Then
  57.  
  58. printFile()
  59.  
  60.  
  61. End If
  62.  
  63. WshNetwork.SetDefaultPrinter(oPrinters.Item(i+1))
  64.  
  65. WScript.Echo oPrinters.Item(i+1)
  66.  
  67.  
  68. Next
  69.  
  70. MsgBox strInfo
Aug 3 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sergio Otoya | last post by:
Hi all, Is there any way of copying a file using javascript, not using the Filesystemobject (ActiveX). I need this to run in Windows and MACS. Any help would be greatly appreciated. Thanks...
2
by: DB | last post by:
Hi All, I have to execute dos command containing batch file using asp.net application. Code for batch file execution is working properly when I tried it with console application but same code...
3
by: Chris | last post by:
Hi, How can I execute a batch file form asp.net app. I have a form which a user uploads a text file. after the file is uploaded I want a batch file to then execute and do some work. Thanks
1
by: coosa | last post by:
Hi all, As the topic describes, I'd like to run a batch file using sql server. How do i do it? Thanks in advance
1
by: guitarsajee | last post by:
hi.. i want to know how to validate file tag using javascript... plz let me know soon as possible.. thanx...
0
by: Elroyskimms | last post by:
I need to execute a batch file via ASP.Net. In my VB.Net code, I'm using System.Diagnostics.Process to call the batch file and its appropriate command line arguments. I'm using...
6
by: mitchell | last post by:
hi i was trying to delete a file in a javascript function but couldn't do it. myActiveXObject = new ActiveXObject("Scripting.FileSystemObject"); file =...
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
1
by: Ira Sinha | last post by:
I have created a batch file, which is using 7z command to encrypt and compress a file. The command in the batch file is 7za -TZIP a -p I am able to run the batch file successfully from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.