Moon-Man.net Posted September 4, 2006 Share Posted September 4, 2006 Hey forum,Could someone please give me a hand to write a script that will go through any given folder in a linux system, and remove BAD charactures? We have some macs that need to move files accross to a Samba inviroment, and it forbidds these cahractures. "\/ : * ? " < > |" but there are about 10,000 files that need to be renamed! Im not gonna do all them :oThe only issue is that it needs to be a reocursive script. And that part im lost on :SCould someone please help?Cheers,Nathan Link to comment https://forums.phpfreaks.com/topic/19631-php-file-renaming-script/ Share on other sites More sharing options...
ToonMariner Posted September 4, 2006 Share Posted September 4, 2006 OK msn me toonmariner(at)hotmail.com Link to comment https://forums.phpfreaks.com/topic/19631-php-file-renaming-script/#findComment-85503 Share on other sites More sharing options...
Moon-Man.net Posted September 4, 2006 Author Share Posted September 4, 2006 Can anyone else offer some help?Cheers,Nathan Link to comment https://forums.phpfreaks.com/topic/19631-php-file-renaming-script/#findComment-85510 Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 [code]<?php$dir = '/path/to/old/dir';$handle = opendir($dir);while(($file = readdir($handle)) !== false) { if (preg_match('/' . preg_quote('\/ : * ? " < > |', '/') . '/', $file)) { $path = $dir . DIRECTORY_SEPARATOR . $file; if (!in_array($file, array('..', '.')) && is_file($path)) { $contents = file_get_contents($path); $newPath = $dir . DIRECTORY_SEPARATOR . preg_replace('/' . preg_quote('\/ : * ? " < > |', '/') . '/', '_', $file); if (file_put_contents($newPath, $contents) !== false) { $unlink($path); //delete old file only if able to create new } } }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/19631-php-file-renaming-script/#findComment-85619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.