Renaming Scenario in Single Folder Primer Tutorial

Renaming Scenario in Single Folder Primer Tutorial

Renaming Scenario in Single Folder Primer Tutorial

The renaming of files in any automated or bulk or batch line of thinking …

  • is most often tackled with server applications, not web applications … due to security risks … true with today’s work below too with the difference that today’s work can be run with a local (Apache/PHP/MySql) web server (in PHP) like MAMP
  • is complex to totally genericize totally, but various rename patterns can be … one such is presented today
  • ideally can be previewed ahead of doing, in some way shape or form

… versus the time saved of long unwieldy one-file-at-a-time command line commands.

Today’s PHP renaming scenario started with a very specific requirement scenario, and we genericized because we thought it may suit a requirement of yours. We break a file rename run of interest into …

Path Prefix … to …
New Prefix
Stays Goes
(in terms of
its length)
Extension
Preview Only?

… to …

Preview Only?

… that the user interaction with, results in HTML form navigation back to the same PHP (as a child within an HTML iframe element placed below), that represents that Rename Run Report.

This second generic draft of renameidea.php became the way it is from a first (very specific) draft renameidea.php in this way.

Given you’ve reached here, you may well want to read about another Rename themed blog posting thread at Renaming to Aid with Numerical File Sorts Command Line and Curl Tutorial as shown below.


Previous relevant Renaming to Aid with Numerical File Sorts Command Line and Curl Tutorial is shown below.

Renaming to Aid with Numerical File Sorts Command Line and Curl Tutorial

Renaming to Aid with Numerical File Sorts Command Line and Curl Tutorial

Yesterday’s Renaming to Aid with Numerical File Sorts Actions Tutorial had good interaction characteristics, in our books (… but as of this news bulletin, not in our pamphlettes and we hope to resume normal transmission forthwith like …) but, out of PHP’s three modes of use (which we talked about at CSH/KSH/PHP Numerical Bubble Sort Tutorial) …

  • Surfing the Net … is very interactive and we can use HTML form elements for navigation purposes … for example, URL (on local MAMP (Mac) Apache/PHP/MySql web server) … http://localhost:8888/rename_to_aid_sorting.php?dirone=.%2F&dirtwo=Django_Python-&asteriskybit=%5B0123456789%5D*.* … but more than likely the environment where users will think to use this PHP application is via …
  • Command Line Usage … for example, be in MAMP’s /Applications/MAMP/htdocs/ directory with … php rename_to_aid_sorting.php ./Django_Python- # or …
  • Curl Usage … for example, be in MAMP’s /Applications/MAMP/htdocs/ directory with … curl http://localhost:8888/rename_to_aid_sorting.php?inspec=./Django_Python- # you can see in action below …

Up near the top of the code processing input parameters the PHP code differentiation goes …


if (isset($_GET['inspec']) && !isset($_GET['dirone']) && !isset($_GET['dirtwo'])) { // reserved for curl instructions
// Curl usage is hard to differentiate from "Surfing the Net" ... they both ask for a URL whereas Command Line has arguments ... so sometimes Curl usage is best
// organized with one $_GET[] (for us $_GET['inspec']) and limit it to just the one. Why so? Well, "&" on command line is awkward.
// To involve more than one argument in Curl use an internal delimitation character like ^ or ! perhaps to proceed. We were able to just keep it to one today.
//
// ... curl argument processing code goes here
//
} else if ($argv) { // command line usage
//
// ... command line argument processing code goes here
//
} else { // surfing the net usage
//
// ... surfing the net argument processing code goes here
//
}

… and down near the bottom, differentiation regarding the look of the outputs, it goes …


if ($argv || (isset($_GET['inspec']) && !isset($_GET['dirone']) && !isset($_GET['dirtwo']))) { // command line or curl ... just show commands for user to try or not ... they may want a pipe like " | tee do_these_renames.ksh" (and then follow up "ksh -x do_these_renames.ksh")
$outp="";
if (strpos(strtoupper(('' . PHP_OS)), "WINDOW") !== false) {
$lines=explode("\nren" . "ame ");
for ($oi=1; $oi<sizeof($lines); $oi++) {
$outp.="ren" . "ame " . explode("\n", $lines[$oi])[0] . "\n";
}
} else {
$lines=explode("\nm" . "v ",$htmlis);
for ($oi=1; $oi<sizeof($lines); $oi++) {
$outp.="m" . "v " . explode("\n", $lines[$oi])[0] . "\n";
}
}
echo $outp;
} else { // surfing the net form interactions below
echo $htmlis;
}

If you watch the video above, you’ll see us touching on some piping ideas too, with respect to Command Line and Curl modes of use, taking place at the command line. These modes of use just output a set of operating system proposed rename (or non-Windows mv) commands to achieve the same default decisions the Surfing the Net program first shows after you Click/Touch the “Analyze” button. These commands have not been done by the application, as we leave this up to the user, as much as anything because the joys of piping can be used. Take a look at

  • Command Line Usage … for example, be in MAMP’s /Applications/MAMP/htdocs/ directory with … php rename_to_aid_sorting.php ./Django_Python- | tee do_renames.ksh # or …
  • Curl Usage … for example, be in MAMP’s /Applications/MAMP/htdocs/ directory with … curl http://localhost:8888/rename_to_aid_sorting.php?inspec=./Django_Python- | tee do_renames.ksh # you can see in action below …

To make these (default) renamings happen (and now the operating system will supply any error messages, and here, we use our favourite scripting shell on non-Windows …ksh … Korn Shell) …


ksh -x do_renames.ksh

For Windows …

  • Command Line Usage … for example, be in MAMP’s /Applications/MAMP/htdocs/ directory with … php rename_to_aid_sorting.php ./Django_Python- > do_renames.bat # or …
  • Curl Usage … for example, be in MAMP’s /Applications/MAMP/htdocs/ directory with … curl http://localhost:8888/rename_to_aid_sorting.php?inspec=./Django_Python- > do_renames.bat # you can see in action below …

To make these (default) renamings happen, opening the Windows command line application (and now Window’s command line (DOS) operating system will supply any error messages) …


do_renames.bat

The third draft of rename_to_aid_sorting.php, that changed this way, can be tried, by you, at this, still, “forlornly functional, but still showing Analysis phase” (as we outlined with yesterday’s Renaming to Aid with Numerical File Sorts Primer Tutorial) live run link. Its real power, though, is by you downloading it to a local web server environment such as MAMP supplies and code and test things yourself. Feel free.

Did you know?

What is behind how PHP performs the operating system renaming commands … (good ol’) …


exec(urldecode($_POST['cmd']));

Notice “the home of PHP”‘s webpage’s blurb about alternatives …

system() – Execute an external program and display the output
passthru() – Execute an external program and display raw output
escapeshellcmd() – Escape shell metacharacters
pcntl_exec() – Executes specified program in current process space
backtick operator

… and you might be interested in I.T. “land” with the word fork fork fork fork fork fork fork pork … aah … fork.


Previous relevant Renaming to Aid with Numerical File Sorts Actions Tutorial is shown below.

Renaming to Aid with Numerical File Sorts Actions Tutorial

Renaming to Aid with Numerical File Sorts Actions Tutorial

Ah, that’s the thing! “Actions” sounds good’n’all but … and didn’t you … we can’t let anybody loose with a “fully functional” live run to the RJM Programming web server with this renaming web application. We’ll just have to show you what the same codebase produces on our local MAMP (Mac) Apache/PHP/MySql web server in “fully functional” form.

Happily, our second draft of rename_to_aid_sorting.php, that changed this way, can be tried, by you, at this “forlornly functional, but still showing Analysis phase” (as we outlined with yesterday’s Renaming to Aid with Numerical File Sorts Primer Tutorial) live run link.

So after an initial phase of imagining how to resist bad usage thoughts, there were also more things to consider when allowing for real operating system rename commands (or mv on non-Windows operating systems), such as …

  • error (and status) messaging … related to
    1. input file does not exist
    2. output file already exists
    3. rename fails
    4. (else mention if successful)
  • individual checkboxes to allow for individual renamings (which happen at an HTML iframe “child” run of the same codebase PHP that performs an exec rename call from the (relevant) operating system (though we could have used PHP’s rename command to not have to differentiate via operating system, this way), the PHP’s way to tell happening via PHP_OS global)
  • overall checkbox to allow for any non-checked individual checkboxes to be clicked in a batch … and if so …
    1. stagger the timing of it happening so user can read a status message between setTimeout controlled renamings
    2. give a report on the overall run at the end of this run
    3. show progress via cursor:progress; on the overall checkbox going back to cursor:pointer; at the finish
  • control of the individual checkbox “checked” look happens at that HTML iframe’s onload event “Client Pre-Emptive Iframe” inspired logic
  • after a successful rename ensure no bad repeated action, by use of [HTMLinputTextElement].readOnly=true on middle textbox and [HTMLinputCheckboxElement].style.display=’none’ on right hand side checkbox
  • spaces in filenames requires double quoting delimitation

It’s amazing how much extra thought you need to put into procedural web applications like this, because the user needs follow up information in a real sense.

Did you know?

What is behind how PHP takes the input entries and obtains a set of file names (we refer to as “input” files above)? It’s done via one of our favourite PHP functions … (good ol’) …


foreach (glob($dprefix . $asteriskybit) as $filename) {
// process $filename here
}

Notice its abilities as far as wildcards go … * (for any number of character) and ? (for one character) and [0123456789] (we use in this project, for one counting number character).


Previous relevant Renaming to Aid with Numerical File Sorts Primer Tutorial is shown below.

Renaming to Aid with Numerical File Sorts Primer Tutorial

Renaming to Aid with Numerical File Sorts Primer Tutorial

It’s not just the operating systems that throw up issues regarding file names with numerical parts to them that when displayed get sorted alphabetically, but you’d like it either …

  • numerically … or …
  • a hybrid of numerically and alphabetically

We’re not there yet … but “are we there yet”, now? … but we’re starting down the road to this with some PHP we’re developing. This might be useful as a tool because sometimes with real work, you could be talking about hundreds of files needing renaming either to …

  • suit some procedural (process) that is looking for the numbers to determine order, and yet uses defaulting file sorting algorithms … ie. what we are talking about today is a program to make it that both are satisfied, so long as the procedure doesn’t mind filenames being changed this way, to still work
  • annoyance factor issues, the one that springs to mind for me being the assembly of some images that we use Mac OS X desktop application Preview to use to assemble into a PDF slideshow via being in the Mac OS X Finder and highlighting our files and running into the alphabetic/numerical sort conundrum … annoyance only? … by that I mean, you can fix this at Preview, so it rates as an “annoyance” only

What’s involved? Lots … think of this in terms of prefixing the correct number of “0”‘s (ie. zeroes) in the right place of the filename …

  • the file extension … we decide not to cross across file extensions with our mini-renaming-rule sets
  • the number of delimited sets before the period (ie. dot “.”) of the file extension … we decide not to cross across “file extension/number of delimited sets before the period”s with our mini-renaming-rule sets
  • the delimiter itself before the period (and if none, call it the period) … we decide not to cross across “file extension/number of delimited sets before the period/delimiter”s with our mini-renaming-rule sets
  • how many “delimited sets before the period” to apply the renaming logic attempts onto … we’ve decided just the first for now
  • what about non-numerical and non-delimiter characters before the period … hand me the Bex, will you?!

Our first draft of rename_to_aid_sorting.php can be tried, by you, at this live run link.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *