在文件夹路径中循环时编辑vbscript-PHP
我有一个php代码,可以在我的文件夹中循环,同时编辑我的vbscript。我的代码正在工作,但不是我预期的方式 以下是我的示例文件夹的内容:在文件夹路径中循环时编辑vbscript-PHP,php,vbscript,preg-match,opendir,Php,Vbscript,Preg Match,Opendir,我有一个php代码,可以在我的文件夹中循环,同时编辑我的vbscript。我的代码正在工作,但不是我预期的方式 以下是我的示例文件夹的内容: Option Explicit Dim oFSO, myFolder Dim xlCSV myFolder= "C:/Users/user/Desktop/SampleFolders/20170503/Users/user/Desktop/SampleFolders/20170502/Users/user/Desktop/SampleFolders
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
$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);
这是我的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
$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);
我希望变量myFolder仅为:
myFolder= "C:/Users/user/Desktop/SampleFolders/20170503"
因为它已经在所有文件夹中循环,应该是我的最终输出
你能给我一些提示吗?我认为这与我的循环有关,但我似乎找不到哪里出错了
这是我的php代码:
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
$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);
尝试将refular表达式修改为bolow,因为您还需要数值和
/
preg_match('#myFolder="([A-Za-z0-9:.\\\\/]+)#', $line, $matches);
不要编辑现有的
.vbs
文件。每次需要更改模板时,使用模板生成它。它更简单,更不容易出错,而且当.vbs
文件通过其他方式更改甚至删除时,它不会失败。这对我的代码很有效!谢谢!:)