【发布时间】:2017-06-23 09:23:21
【问题描述】:
我有一个 php 代码,它将遍历我的文件夹并同时编辑我的 vbscript。我的代码正在运行,但不是我预期的那样。
这是我的示例文件夹的内容:
这是我的 VB 脚本代码:
Option Explicit
Dim oFSO, myFolder
Dim xlCSV
myFolder= "C:/Users/user/Desktop/SampleFolders/20170503/Users/user/Desktop/SampleFolders/20170502/Users/user/Desktop/SampleFolders/20170501"
Set oFSO = CreateObject("Scripting.FileSystemObject")
xlCSV = 6 'Excel CSV format enum
Call ConvertAllExcelFiles(myFolder)
Set oFSO = Nothing
我希望变量 myFolder 是唯一的:
myFolder= "C:/Users/user/Desktop/SampleFolders/20170503"
因为它已经遍历所有文件夹,它应该是我的最终输出。
你能给我一些提示吗?我认为这与我的循环有关,但我似乎找不到哪里出错了。
这是我的php代码:
$path = "C:/Users/user/Desktop/SampleFolders";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ('.' === $file) continue;
if ('..' === $file) continue;
// Read file into array
$data = file('C:/Users/user/Desktop/changecsv.vbs');
// This is the location we want
$newLocation = "C:/Users/user/Desktop/SampleFolders/$file";
// Read each line and try to find the myFolder string
$data = array_map(function($line) use ($newLocation) {
// If we have myFolder make sure it's followed by a path of some kind,
capture
// this path into $matches
preg_match('/myFolder="([A-Za-z:.\\\\]+)/', $line, $matches);
// Replace old path with new path
if (count($matches)) {
$line = str_replace($matches[1], $newLocation, $line);
}
return $line;
}, $data);
// Replace contents of file with new location
file_put_contents('C:/Users/user/Desktop/changecsv.vbs', implode('', $data));
}
echo "editted";
closedir($handle);
【问题讨论】:
-
不要编辑现有的
.vbs文件。每次需要更改时使用模板生成它。它更简单,更不容易出错,并且在.vbs文件被其他方式更改甚至删除时不会失败。
标签: php vbscript preg-match opendir