Search code examples
phpvbscriptpreg-matchopendir

Editing vbscript while looping through folder path - PHP


I have a php code that will loop through my file folder and edit my vbscript at the same time. My code is working but not the way I expected.

Here are the contents of my Samplefolders:

enter image description here

Here is my VB Script Code:

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

I want the variable myFolder to be only:

 myFolder= "C:/Users/user/Desktop/SampleFolders/20170503"

since it has already loop through all folders and it should be my final output.

Can you give me hints on this? I think this has something to do with my loop but I can't seem to find where I'm getting it wrong.

Here is my php code:

  $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);

Solution

  • Try to modify your refular expression as bolow because you also need numeric value and /

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