[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

Call a BATCH FILE from COLDFUSION page

Hi All you EXPERTS out there!

I have a cfm page that executes a simple query:


<cfquery datasource="#myDatasource#" name="getFiles">
      Select source, destination, workgroup
      from myDBtable
</cfquery>


Then I want to run a batch file inside a loop where I can pass the variables #source# and #destination# to the batch file. Inside the batch file, it will do an xcopy using the %1 and %2 variables I pass, then close, and return to the calling web page.

I need to call this batch file from inside the page. I am adding an exit at the end of the batch file, so it will close on completion of each copy, AND I want the loop to continue ONLY when the batch file closes and I also want it to collect the exitcode for use in tracking the success of the copy.

For the user of the page, I am building a table inside the loop to display the status:

<cfoutput query="getFiles">
<table>
<tr>
      <th>Source</th>
      <th>Action</th>
      <th>Destination</th>
      <th>Exit Code</th>
</tr>

***DO THE BATCH FILE STUFF****

<tr align="center">
      <td>#Source#</td>
      <td>was copied to</td>
      <td>#Destination#</td>
      <td>#ExitCode#</td>
</tr>
</cfoutput>

</table>

To summarize, is there a way to call a batch file from inside a web page... (cffile won't work - I need something else). Second, how can I track the execution of the bat file and continue the loop only when it returns an exitcode (no matter what the exitcode is for now), and how can I collect the exit code from the dos batch file, and show it as above?

Later I would like to add in error handling, but for now, I would be thrilled if the copy just worked and showed the table to the user. I am also toying with how to collect all the path and file names copied by the action.

Thanks a MILLION! (My head really hurts from banging it on this brick wall....)
0
Bauerchick
Asked:
Bauerchick
  • 4
  • 2
  • 2
  • +2
1 Solution
 
SidFishesCommented:
something like

<cfoutput query="getFiles">
<table>
<tr>
     <th>Source</th>
     <th>Action</th>
     <th>Destination</th>
     <th>Exit Code</th>
</tr>


<cfexecute
  name = "C:\temp\mybat.bat"
  arguments = " #source# , #destination# "
  outputfile="c:\temp\exitcode.txt"
>
</cfexecute>

you could then use cffile to read exitcode.txt and assign the results to your exitcode variable.

<tr align="center">
     <td>#Source#</td>
     <td>was copied to</td>
     <td>#Destination#</td>
     <td>#ExitCode#</td>
</tr>
</cfoutput>


http://livedocs.macromedia.com/coldfusion/6/CFML_Reference/Tags-pt126.htm
0
 
SidFishesCommented:
a simpler method that avoids cffile is

<cfsavecontent variable="exitcode">

<cfexecute timeout="1"
  name = "C:\temp\mybat.bat"
  arguments = " #source# , #destination# "
 >
</cfexecute>

</cfsavecontent>
0
 
BauerchickAuthor Commented:
I cannot use CFEXECUTE because of cf permissions issues, but as a user logged in, I can execute a batch file under my login name with no problems.

The user will plug their laptop into the network (for example), navigate to a web page that has an "Update your files now" button. When they push the button, the action page will run the query to the db, and need to loop through the returns making a batch file call on each loop. Inside the batch file is simple network path management, and xcopy commands....

*************************************************
NET USE Z: /DELETE /YES
NET USE T: /DELETE /YES
NET USE Z: \\%COMPUTERNAME%\c$
NET USE T: \\COBDATA7\GIS-COV$

z:
xcopy %1 %2 %3 %4 %5 %6
exit /b
*************************************************


Any ideas?
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
pcaiooCommented:
the thing you need to do is , you can change your cold fusion server service setting.
use login as to solve your problem

open control panel service and coldfusion
set login as , create a new user
and add the required access permission to the user
or simply logon colfusion (application) server as administrator(BE CAREFUL)
then you can use CFEXECUTE.
0
 
BauerchickAuthor Commented:
Thanks to all the folks who attempted to help... Unfortunately, altering permissions for either the user or coldfusion, is not an option in our environment. I did find a perfect tag that would overcome these permissions issues - <CFIMPERSONATE> -  but MM has depricated it, with no replacement. I also found this link for those of you that know ASP. It seems to allow you to run a process while impersonating the user... should overcome any access permissions the server or software might have - but I don't know ASP so it is hard to test out.

                    http://support.microsoft.com/default.aspx?scid=kb;EN-US;248187

Not sure if it will help anyone else or not, but there it is.  I would appreciate hearing from anyone about whether this works or not, and the code for solving this situation, if it does.

Anyway, I figured this out on my own... I think. This COLDFUSION solution appears to work.

I dynamically generate the batch file using CFCONTENT and CFHEADER tags along with DOS, then when the user accesses the webpage, it generates the dos batch file, populating all the xcopy commands from a cfquery into my sql table for source and destination. Then it prompts the user to RUN or SAVE the batch file it just wrote. I intended to just create a file that they could save and then run manually, but I discovered accidentally that if I click RUN, it appears to run the batch file as the USER not CF, and seems to work fine.

Here is the simple version of the code I am using (I'm adding in logging and error handling, too, but this is the basics)...


*******************************************************************

<cfquery datasource="#myDatasource#" name="getFiles">
     Select source, destination, workgroup
      from myMSSQLTable
</cfquery>

<cfcontent type="application/unknown">
<cfheader name="Content-Disposition" value="type=attachment;filename=copythefiles2c.bat">

:: pick up the computername envirnoment var so I don't get any accidental writes to the server's c drive

NET USE Z: /DELETE /YES  
NET USE Z: \\%COMPUTERNAME%\c$

z:
<cfoutput query="getFiles">
          xcopy #getFiles.source# #getFiles.destination# /y /s/e /f /r /D
</cfoutput>


*******************************************************************

If you don't use application/unknown, it will append a different filetype to whatever filename you provide... like copythefiles2c.bat.txt - which then won't run, of course. Seems to work, but I will post more next week after I fully test it, for those who might find this search later and need help.

If anyone sees any issues with this approach that I am not aware of, please let me know...



0
 
pcaiooCommented:
CFIMPERSONATE avaliable since on CF 4.5 and not avaliable after 6 due to security structure change in CF server
0
 
dgrafxCommented:
If you can't run cfexecute -
get a different host ...
OR
You'll need to rewrite the batch file using your query variables.

First setup a dummy batch file for easy reading and replacing (it may be already - I'm just saying)
What I mean is
where you have:
<td>#Source#</td>
<td>was copied to</td>
<td>#Destination#</td>
<td>#ExitCode#</td>
change to:
<td>^Source^</td>
<td>was copied to</td>
<td>^Destination^</td>
<td>^ExitCode^</td>
<cffile action="read" file="yourDUMMYbatchfileandpath" variable="temp">
<cfset temp=replacenocase(temp,"^source^","#getFiles.Source#","all")>
<cfset temp=replacenocase(temp,"^Destination^","#getFiles.Destination#","all")>
<cfset temp=replacenocase(temp,"^ExitCode^","#getFiles.ExitCode#","all")>
<cffile ACTION="Write" FILE="yourNEWbatchfileandpath" OUTPUT="#temp#" ADDNEWLINE="no">

You can then let the user run this NEW batch file under your permissions.
Put some code where needed like you mentioned "loop only when it returns an exitcode"
I think if you toy with this you'll get it.
0
 
dgrafxCommented:
I believe I've answered the question and deserve the points.
I do this exact same thing using ldap.
0
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0
 
dgrafxCommented:
GranMod
I understand situations where poster does not reply and question is deleted with no point refund.
Moderators generally have no idea if solutions work.
But to give their points back when they don't reply!
Surely you realize that some posters use the "No Response" technique to get their questions answered for "free".
0
 
GranModCommented:
The "No response" for free answers was the reason for starting the cleanup process.
But sorry, I didn't check the proposal for the refund/no refund.
Changed the Q into PAQ/No refund as there was no reply to the expert comments.

Hope you run into a more gratefull questioner the next time.

GranMod
Community Support Moderator
0
 
dgrafxCommented:
Thanks for replying ...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now