473,796 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

35 New Member
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 14030
huiling25
35 New Member
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"

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
huiling25
35 New Member
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
23767
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 in advance. Sergio Otoya.
2
2816
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 is not working with ASP.net application. Help will be useful......... Thanks n Regards, Deepak
3
2625
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
8415
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
1889
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
2283
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 System.Diagnostics.Process.StandardError and System.Diagnostics.Process.StandardOutput to capture and view the output and error messages. My web.config file has the following: <identity impersonate="true" userName="administrator" password="password" />
6
8089
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 = myActiveXObject.GetFile("c:\\file.txt"); file.Delete(); this code will work in IE
3
8276
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 the file size; however, I'm not happy with this solution because my application is designed to work in multiple browsers and ActiveX is limited to Internet Explorer. I cannot check the file size on the web server because IIS throws the following...
1
3718
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 command prompt, however when I try to execute the same file from the SQL Server using the xp_CMDShell, it gives me the following errors: Can not open file Access is denied. I am logged into the sql server with a Windows account, which is a member of...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.