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



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) (v1.0).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/");


I’ve also been told to replace the line:

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


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



Solved it. Used

shell_exec("mv ".escapeshellarg($thisGame)." Removed/");


DO NOT MAKE DUPLICATE POSTS OF THE SAME EXACT THING!’$’-is-getting-missed-by-preg_match-use-escapeshellcmd-27506/?topicseen