在循环文件夹路径时编辑vbscript - PHP

时间:2017-06-23 09:23:22

标签: php vbscript preg-match opendir

我有一个PHP代码,它将遍历我的文件夹并同时编辑我的vbscript。我的代码正在运行,但不是我预期的方式。

以下是示例文件夹的内容:

enter image description here

这是我的 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);

1 个答案:

答案 0 :(得分:1)

尝试将您的refular表达式修改为bolow,因为您还需要数值和/

preg_match('#myFolder="([A-Za-z0-9:.\\\\/]+)#', $line, $matches);