Jump to content
jeremycherfas

Rename files based on content

Recommended Posts

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

Share this post


Link to post

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)).

Share this post


Link to post

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.

Share this post


Link to post

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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...