etrader Posted March 9, 2011 Share Posted March 9, 2011 I have a folder containing files. I want to replace some characters in the file names (e.g. "_" with "-"), then make a list of new files (renamed ones). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/ Share on other sites More sharing options...
litebearer Posted March 9, 2011 Share Posted March 9, 2011 combing glob, str_replace and rename should get you there (if the list is to be just the renamed files, add them to an array as you loop thru the glob results Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/#findComment-1185230 Share on other sites More sharing options...
etrader Posted March 10, 2011 Author Share Posted March 10, 2011 My issue is about renaming. How in a php script I can command to re-write files in a folder? Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/#findComment-1185654 Share on other sites More sharing options...
HuggieBear Posted March 10, 2011 Share Posted March 10, 2011 How about rename() Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/#findComment-1185660 Share on other sites More sharing options...
etrader Posted March 10, 2011 Author Share Posted March 10, 2011 I tried to do this with this code foreach (glob("1/files/*.mp4") as $filename) { $filename1 = str_replace("_", " ", $filename); rename ("1/files/$filename", "1/files/$filename1"); } but it did not work where did I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/#findComment-1185899 Share on other sites More sharing options...
litebearer Posted March 10, 2011 Share Posted March 10, 2011 Untested, un-proof-read, but try... foreach (glob("1/files/*.mp4") as $filename) { $filename1 = str_replace(" ", "_", $filename); $old_full = "1/files/" . $filename; $new_full = "1/files/" . $filename1' rename ($old_full, $new_full); } Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/#findComment-1185902 Share on other sites More sharing options...
etrader Posted March 11, 2011 Author Share Posted March 11, 2011 it was my mistake. $filename of the foreach already has "1/files"; so no need to add it Quote Link to comment https://forums.phpfreaks.com/topic/230147-renaming-files-in-a-folder-and-make-a-list/#findComment-1185907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.