0

I have a php code that with the exec() function executes a .bat file that is hosted on my computer. I'm trying to show me on the page that I created to execute that function, a progress bar or a percentage of progress of what's left for the .bat file to finish running. Any ideas?

exec('start /B C:\Users\Users\Desktop\backupcopia.bat', $output);

In the script I have the following:

@echo off

robocopy "C:\Users\Users\Desktop\Folder1" "F:\Backup\Copy
Maginon
  • 109
  • 5
  • Maybe [this question might help](https://stackoverflow.com/a/6144213/5897602) you although its based on linux – Jaquarh Jan 17 '19 at 09:25
  • 1
    Does that script have any way to report the progress? Or do you have any way to know what the progress from that script would be? Because if not, then you can't really do it - you can't really know how long a script will take and thus design and implement feedback for it. – VLAZ Jan 17 '19 at 09:28
  • You could potentially write to a file as that file is executing, but like @vlaz stated, without knowing **how long** the process will take, you will need to know how long to read from the output file. – Jaquarh Jan 17 '19 at 09:31
  • @vlaz How can I add a way to report the process to the script? – Maginon Jan 17 '19 at 09:47
  • @Jaquarh more generally, either the script reports *in some way* what it does, e.g., outputting stuff, or if it doesn't, then you have *some knowledge* about the execution of the script that you can use to guess how far along it is, e.g., the script should take about a minute. – VLAZ Jan 17 '19 at 09:48
  • 1
    @Xerox it depends on what the script does. I've no way to know. I can guess that it's doing a backup. So perhaps you might have the script print "Copying **58** files" (the number would be determined by the script) and then have one line for each file, e.g., "Copying abc.txt" so you know there are 58 things and how many of these are done. It's a rough estimation but it can be used to determine progress. What can you show and how would depend a lot on what the script does, of course so there is no one answer to this. – VLAZ Jan 17 '19 at 09:51
  • @vlaz I have a script that makes a backup. I pass the code to you: `@echo off robocopy "C:\Users\Users\Desktop\Folder1" "F:\Backup\Copy` It is what there is only – Maginon Jan 17 '19 at 09:54
  • @Xerox in that case it might make sense to first check how many files are in the first folder and maybe also how big they are. PHP can do that before you even run the batch file. But it will then depend whether that robocopy command outputs anything to show it has processed a file, and whether you can read and parse that info in real-time. Then of course you have to turn that output into HTML and use PHP to flush each time you want to push something to the browser (normally PHP waits until the script finishes and then sends all output to the browser at once) – ADyson Jan 17 '19 at 10:41
  • @Xerox But...I notice also that you have tagged AJAX. If you're calling the PHP script via AJAX, then you'll never be able to get any real-time feedback in the browser because AJAX doesn't support getting real-time data from the server (via the output flushing I just described) - it will always wait until the HTTP request ends before it does anything. So it would make your requirement impossible. You might want to consider using something like websockets instead to communicate from browser to server and back again. – ADyson Jan 17 '19 at 10:43
  • @ADysonI do not call the PHP sequence through Ajax. – Maginon Jan 17 '19 at 10:50
  • @Xerox then why did you tag the question with AJAX?? Don't add things which are not relevant to the question...it just confuses people. I see you already removed it, so thankyou. – ADyson Jan 17 '19 at 11:11
  • @Xerox anyway in that case, once you work out how to get relevant output from your batch script into PHP, you can use PHP's [flush functionality](http://php.net/manual/en/function.flush.php) to send real-time output to the browser. – ADyson Jan 17 '19 at 11:12

0 Answers0