简体   繁体   中英

How to Write a VBA Shell Script to Run a PHP Script on Variable Files

I'm trying to write a shell script that will run a PHP script on a number of files at once (this requires using an exe file and a php file on a json file). The json file is the only one that is variable in this situation.

If I were using the command prompt manually, here's the command I would need to input to achieve the desired result-

C:/Users/myusername/Desktop/XAMPP/php/php.exe transcript.php transcribedfile.json

Cell G2 of my worksheet contains the json filename, so that's why I've activated it in the second line. Once I can get the shell script to work I will code in a loop that will cycle through all values in the G column, beginning with G2, until the ActiveCell value is "".

Here's what I've tried-

Sub Shell()
Range("G2").Activate
Dim strProgramName As String
Dim strArgument As String

strProgramName = "C:\Users\myusername\Desktop\XAMPP\php\php.exe transcript.php " & ActiveCell.Value
strArgument = "/G"

Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)

End Sub

When I run the sub I get a "File Not Found" error. I tested strProgramName and found that I could get php.exe to run if I cut the code off at that point, but I would need it to recognize the whole string.

Any ideas would be welcome. Thank you in advance.

Here's how I'd do this:

Const PHP_PATH As String = "C:\Users\myusername\Desktop\XAMPP\php\"
Dim strProg As Variant

strProg = """{pth}php.exe"" ""{pth}transcript.php"" ""{pth}" & _
          Range("G2").Value & """ \G"

strProg = Replace(strProg, "{pth}", PHP_PATH)
Debug.Print strProg '<< pass this to Shell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM