我们可以从 URL 动态获取 php 值到批处理文件吗?

Can we get php value dynamically from URL to a batch file?

这是我的批处理文件脚本我正在通过任务调度程序运行设置这个脚本 通过使用下面的脚本,我可以将所有文件从 invoice_feed 文件夹复制到网络共享测试文件夹 (shared_folder) 两台服务器都通过 VPN

连接
Echo off
net use \windows_servername\test_folder shared_folder_password /user:shared_folder_username
xcopy C:\Apache\htdocs\Invoices\invoice_feed \windows_servername\test_folder

如果我想将特定文件复制到网络共享文件夹,那么我将该文件手动添加到上面的批处理脚本中,它运行良好。下面是代码

Echo off
net use \windows_servername\test_folder shared_folder_password /user:shared_folder_username
xcopy C:\Apache\htdocs\Invoices\invoice_feed\file_to_copy.csv \windows_servername\test_folder

我可以使用 PHP 运行 这个脚本吗?如果是,那么我可以从 URL 传递动态值并传递它来代替下面的特定文件名吗?

xcopy C:\Apache\htdocs\Invoices\invoice_feed\<?php echo $_GET['csv_file_name']?> \windows_servername\test_folder

或者是否有任何其他方法而不是在批处理文件中执行此操作,我的意思是完全使用 php? 我正在尝试 运行 如下所示的脚本

$file_name = $_GET['csv_file_name'];
echo exec('net use \windows_servername\test_folder shared_folder_password /user:shared_folder_username');
echo exec('xcopy C:\Apache\htdocs\Invoices\invoice_feed$file_name \windows_servername\test_folder');

但运气不好。它显示已复制 0 个文件。我哪里做错了?

在批处理文件中,您可以传递参数并捕获该参数。

batchname.bat arg1 arg2 在批处理文件中,您可以捕获如下参数。

set arg1=%1
set arg2=%2
xcopy C:\Apache\htdocs\Invoices\invoice_feed\%arg1% \windows_servername\test_folder

因此,如果您从 php 触发此批处理,您可以轻松地将 get 参数作为参数传递给批处理文件。

您可以使用 system 函数 运行 批处理文件。

$last_line = system("cmd /c C:/path/to/bat/batchname.bat ".$_GET['csv_file_name'], $ret_val);
echo 'last line: '.$last_line;
echo 'return values: '.$ret_val;