jeremycherfas Posted February 13, 2016 Posted February 13, 2016 I have about 200 files, all called item.html, each in a folder with a unique name. I would like to rename the file or enclosing folder, based on the content of the file. I know that each file contains title="xxxx-xx-xx" where x is a digit 0-9 Is there already a workflow that will look for a target within a file? I couldn't see one. If not, any idea how I might do this? I suppose I could open each file in something like BBEDIT, but that seems awfully difficult. Thanks
vitor Posted February 13, 2016 Posted February 13, 2016 Should be easy enough. However, there’s two things (I can think of right now) I need to be able to help: What exactly do you want (is it to rename the file, the enclosing directory, or both)? Can you show me the contents of at least one of the files? I don’t need everything, but at least the line with title, one above and one below. It can be fake data, but the structure needs to be faithful to the original. Also, as a kind of 2a, seeing as I don’t have access to the files I need to be sure they’re structured exactly the same way, at least in what pertains to title (title ="4444-44-44" is different from title="4444-44-44" (different spacing around = sign) or title='4444-44-44' (different quote types)).
rice.shawn Posted February 18, 2016 Posted February 18, 2016 Based on your specs, this php snippet (not fully tested) should do the trick: <?php // Only deals with first level directories // Get all the directories in the main directory, excluding '.' and '..' foreach ( array_diff( scan_dir( $main_dir ), [ '.', '..' ] ) as $dir ) : // check to make sure that the dir is a dir and that item.html exists if ( ! ( is_dir( $dir ) && file_exists( $dir . '/item.html' ) ) ) { continue; } // grab the contents of the file $contents = file_get_contents( $dir . '/item.html' ); // regex pattern to catch stuff with weird spacing. Add in more quotation types // if you need them $pattern = '/test[ ]*=[ ]*[\'"]([0-9]{4})-([0-9]{2})-([0-9]{2})[\'"]/'; // match all the contents preg_match( $pattern, $contents, $matches ); // the first match isn't what we want array_shift( $matches ); // we could use a join, but there is a possibility that more things would match further down, // so we'll just manually use the first three. $name = $matches[0] . '-' . $matches[1] . '-' . $matches[2]; // If you want a dry run, comment out the "rename" functions and uncomment the next two lines: // print "Renaming {$dir}/item.html to {$dir}/{$name}.html\r\n"; // print "Renaming {$dir} to {$name}\r\n"; // rename the file rename( $dir . '/item.html', $dir . '/' . $name . '.html' ); // rename the directory rename( $dir, $name ); endforeach; Read through the comments to understand it better. Unless you use this often, then you needn't create a workflow but can just run the script. If you're doing the latter, then just copy/paste that into a file called, say, "renamer.php" and drop it into the directory above all the other ones. Then open a terminal and run "php renamer.php", and you should be done. If you want to test it out first to make sure it does what you want, then comment out the "rename" lines and uncomment the "print" lines above it. The regex used should account for the minor variation that Vítor mentioned in his 2a.
jeremycherfas Posted February 18, 2016 Author Posted February 18, 2016 Thanks both. I've been away from my machine and so unable to reply until now. There's no variation in the target expression, other than the dates, but you're right, Shawn, it doesn't hurt to try. I'm going to test it now and will let you know how it goes. Thanks so much.
jeremycherfas Posted February 18, 2016 Author Posted February 18, 2016 Thanks Shawn. In the end I simplified a bit because there wasn't the variability, and it worked really well.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now