Filename with "$" is getting missed by preg_match - use escapeshellcmd?


#1
UnluckyForSome
New Members
0
1 post

Report post 

Posted just now

I am trying to build a filter for sorting/removing files;

However, upon testing I have noticed a certain type of file which is getting through the net despite being named with a “(Japan)” country identifier (which should be filtered) at the end. I think this is because of the dollar sign ($).

The file in question:

Quiz$Millionaire - (Japan).zip

I get the error below when running the script. It echo’s the file correctly, but then when attempting the mv
it reports it with “$Millionaire” missing from the middle of the string!

mv: cannot stat ‘Quiz (Japan) (v1.0).zip’: No such file or directory

I have been told I need to use $thisGame = escapeshellcmd($thisGame); in the code below, but when I do it messes up my regex matching for everything else - so I think I have to modify my regex? - but I’m a beginner and don’t really know how to fix it! Code below:

[php]$gameList = trim(shell_exec(“ls -1”));
$gameArray = explode("\n", $gameList);

shell_exec(‘mkdir -p Removed’);

// Do this magic for every file
foreach ($gameArray as $thisGame)
{
if (!$thisGame) continue;
// Probably already been removed
if (!file_exists($thisGame)) continue;

// Non-Engish speaking countries e.g. (France) or (Japan)
if (preg_match('%\((Asia|Austria|Brazil|China|Denmark|Finland|France|Germany|Greece|Hungary|Israel|Italy|Japan|Japan, Asia|Korea|Netherlands|Norway|Poland|Portugal|Russia|Scandinavia|Spain|Sweden)\)%', $thisGame))
{
    echo "{$thisGame} is a foreign language release. Moved to Removed folder.\n";
    shell_exec("mv \"{$thisGame}\" Removed/");
    continue;
}

}[/php]

I’ve also been told to replace the line:

shell_exec("mv \"{$thisGame}\" Removed/");

with

shell_exec("mv \"" . escapeshellcmd($thisGame) . "\" Removed/");

But unfortunately if I try that I get the error:

mv: cannot stat ‘Quiz$Millionaire (Japan) (v1.0).zip’: No such file or directory

Could anybody help me? I’d be super greatful! Many thanks


#2

If you are going to just copy and paste the same question across multiple forums, you could have at least dropped the text you copied off their forum.


#3

Sorry! I don’t know how this got posted twice :-[

I think I tried to edit out the top part and post it again and delete this one, but It wouldn’t let me! Sorry again.

For anyone interested, I solved this with
[php]shell_exec(“mv “.escapeshellarg($thisGame).” Removed/”);[/php]