help on auto renaming files

Discuss and share scripts and script files...
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

help on auto renaming files

Post by tiago »

I'm getting no success with this one so I'd like to ask for help.
I'm trying to rename several files according to this criteria:

-> scan a text file, locate the first occurrence of asterisks in the format ([space]*[space]), then the first sentence before the asterisk "Entrevista - Stephen Kanitz * TV Cultura" and use that "Entrevista - Stephen Kanitz" to finally name the document, currently having generic names like "showone(164).pl"

-> or the last sentence separated by ([space]-[space]): "Read Designer Notes! - Two-in-one - Yellow.2.4" then naming the documents after such occurrences, "Yellow.2.4" in case. Sometimes the second (in the middle) phrase has the actual title I'd like to use, so the script should be able to recognize them too ("Two-in-one", as per previous example).
Thanks in advance.
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: help on auto renaming files

Post by Stefan »

tiago wrote:I'm getting no success with this one so I'd like to ask for help.
I'm trying to rename several files according to this criteria:

-> scan a text file, locate the first occurrence of asterisks in the format ([space]*[space]), then the first sentence before the asterisk "Entrevista - Stephen Kanitz * TV Cultura" and use that "Entrevista - Stephen Kanitz" to finally name the document, currently having generic names like "showone(164).pl"
Please write no prose but provide real examples.

FROM:

Code: Select all

showone(164).pl
   Entrevista - Stephen Kanitz * TV Cultura
TO:

Code: Select all

Entrevista - Stephen Kanitz.pl
   Entrevista - Stephen Kanitz * TV Cultura
Is the new file name the part from start of line till the first [space]*[space]?
or more like

Code: Select all

Entrevista - Stephen Kanitz
 * TV Cultura
or what is the start of the "sentence"?

And, no real matter perhaps, but is the new name found each time at the same line number? Which is that if yes?


================================
-> or the last sentence separated by ([space]-[space]): "Read Designer Notes! - Two-in-one - Yellow.2.4" then naming the documents after such occurrences, "Yellow.2.4" in case. Sometimes the second (in the middle) phrase has the actual title I'd like to use, so the script should be able to recognize them too ("Two-in-one", as per previous example).
Thanks in advance.
Is that an AND or an OR criteria to the first one? Or should that be two scripts?
This sentence are always the last in the file?

What should be the new file name?
"Two-in-one" or "Yellow.2.4" ?
How could one tell?


================================

And common questions:
* are that plain text files?
* how many lines to they contain? Or: how big is the file size?
* can you post relevant but small pieces of such an file to see how it looks like?

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: help on auto renaming files

Post by tiago »

-> Is the new file name the part from start of line till the first [space]*[space]?
Yes.

-> is the new name found each time at the same line number?
Yes.
-> Which is that if yes?
Depends on the project I'm working with, so hard-coding may not be a good idea. Sometimes there are different types of source files and each one will have a different structure.

-> Is that an AND or an OR criteria to the first one?
That's an OR.
-> Or should that be two scripts?
No. I think that variables may suffice for each case. If a condition is not found, file should be left intact. I can handle it in case of errors, anyway.
-> This sentence are always the last in the file?
No, these sentences are generally at the top of the files.

->What should be the new file name? "Two-in-one" or "Yellow.2.4" ? How could one tell?
Something like this: $position = 3 //1, 2, 3, etc
It depends on the type* of files and separators in use to locate just the first occurrence and work with them.
*Type of files: document structure, not extensions.

-> are that plain text files?
Yes.
-> how many lines to they contain? Or: how big is the file size?
Not much, 10-30 KB most.
-> can you post relevant but small pieces of such an file to see how it looks like?
Attached.
Power-hungry user!!!

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: help on auto renaming files

Post by tiago »

Attaching is not working for me so I posted a simplified sample (eliminated extra white spacing).
=========================================================

Code: Select all

Stickies

A {text-decoration:none; color:1F75CC; }
BODY {font-family:"Segoe UI", "Verdana", Sans-serif; }
TD {font-family:"Segoe UI", "Verdana"; font-size:0.8em; }

&nbsp

home
 | 
support
 |
 contact
 | 
forum

Zhorn Software

stickies
 | 
bart
 | 
zbar
 | 
caffeine
 |
cas
 | 
birthday reminder
 | 
keycounter
 | 
soundboard
 |
vnchelper

Stickies

Download

Screenshots

FAQ

Tools

API

Skins
 
Browse

Newest
 
Last week
 
Most popular
 
List

Skinner

Versions

Read Designer Notes! - Two-in-one - Yellow.2.4

Created by 
Sandiger
 on 
23/03/2010
...
This is the first, main part of the document. It goes on with more irrelevant text for the OT matter.
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: help on auto renaming files

Post by Stefan »

tiago wrote:-> or the last sentence separated by ([space]-[space]): "Read Designer Notes! - Two-in-one - Yellow.2.4"
Stefan wrote:This sentence are always the last in the file?
tiago wrote:-> This sentence are always the last in the file?
No, these sentences are generally at the top of the files.
What now? Last or top? Anyway irrelevant because of...


- - -

tiago wrote:->What should be the new file name? "Two-in-one" or "Yellow.2.4" ? How could one tell?
Something like this: $position = 3 //1, 2, 3, etc
It depends on the type* of files and separators in use to locate just the first occurrence and work with them.
*Type of files: document structure, not extensions.
what is ? :shock:
I need something 100% statement like : at that position that part from here to there.
Or: IF file type=X then that ELSE type=y then use that.

How should one find the wanted line out of:
Versions

Read Designer Notes! - Two-in-one - Yellow.2.4

Created by
and how should one catch if you want to have "Two-in-one" or "Yellow.2.4" as new file name?


- - -
tiago wrote:I posted a simplified sample
There is nothing like [space]*[space] in that example.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: help on auto renaming files

Post by Stefan »

An possible script could looks like this start:

FOR example:

Code: Select all

Stickies

A {text-decoration:none; color:1F75CC; }
BODY {font-family:"Segoe UI", "Verdana", Sans-serif; }
TD {font-family:"Segoe UI", "Verdana"; font-size:0.8em; }

Entrevista - Stephen Kanitz * TV Cultura

&nbsp

home
|
support
[...]
List

Skinner

Versions

Read Designer Notes! - Two-in-one - Yellow.2.4

Created by
Sandiger
on
23/03/2010
...

Code: Select all

//works only for one selected file:
if (get("CountSelected") != 1){msg "Select one but only one file."; end 1;}

       //read content of an file into an var $file:
       // $file = readfile("x:\xyplorer\test\stickies.txt");
          $file = readfile("<curitem>"); //current selected file

      //for each line in lineS do:
      foreach($line, $file, "<crlf>"){
              // IF " * " is found:
              if ( strpos($line, " * ") > -1 ){
                     msg "Found at line: $line",1;
                     
                      // get the position of the star *-sign:
                      $strpos = strpos($line, "*");
                          
                       // get the part of that line from start of line till the blank right before the found star position:
                      $newname = substr($line,, $strpos-1);
                           
                      // invoke rename() with preview:
                      msg "NewName would be: #$newname# plus orig extension",1;
                      rename b, $newname, p;                     
              }
      }
                      

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: help on auto renaming files

Post by tiago »

-> or the last sentence separated by ([space]-[space]): "Read Designer Notes! - Two-in-one - Yellow.2.4"
->->The last sentence in that first occurrence.

-> I need something 100% statement like : at that position that part from here to there.
->->Impossible, as I said it's a multi-purpose task and that's why "$position" is so important.

-> How should one find the wanted line out of:
->->That's why I'm talking about "first occurrence", imagining this is a regex job, for instance.
The contents of those documents differ but generally they have a structure in which "Title[space][separator][space]Comment" and this could trigger a regex match to start working with.

-> and how should one catch if you want to have "Two-in-one" or "Yellow.2.4" as new file name?
->->Me knowing beforehand what I want, having that "$position" variable to control the searchment. Got it?

->There is nothing like [space]*[space] in that example.
->->This is because I was illustrating a "[space][separator][space]" example. So "$separator" will be a variable too. So [space]*[space] or [space]-[space] will be on different types of documents and each type will be worked at a different time with different specifications (via variables).

On your script:
GREAT JOB!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CARACA!!!
But:
1) It eliminated the last "4" out of "Read Designer Notes! - Two-in-one - Yellow.2.4.diz", so the result was "Read Designer Notes! - Two-in-one - Yellow.2.diz";

2) It should not invoke another renaming for the same document, stopping at the first match of each document;

3) I'll need this to work with several documents so it's not just a matter of cutting off "if (get("CountSelected") != 1){msg "Select one but only one file."; end 1;}" and adding "foreach"? I see no harm in doing that, except for...

4) The prompts. Is there a way to get rid of them, leaving the script to work with all documents at once without any user intervention?;

5) $position. For this specific document, $position=3 in the sense of "last match". A general solution may be this:
$position=1 - gets the first sentence before the first separator on the first occurrence. In case, "Read Designer Notes!".
$position=2 - gets a sentence in the middle of the occurrence, in case "Two-in-one". Problem: "Designer - Notes - Two-in-one - Yellow.2.4". Is there a way for the script to calculate $pos with something like: 4($test)/2 = 2 OR 2+1 OR 2-1, depending on the project I am working with? (2 = Notes, 2+1 = Two-in-one, 2-1 = Designer). $test then will be a way to determine the $position in case $position=2 is specified and the occurrence has numbers which can divided by 2, so strictly talking there's no "middle". Logically, "", "+1" or "-1" must be set beforehand.
$position=3 - gets the last sentence after the last separator on the first occurrence. In case, "Yellow.2.4". Which is the actual wanted name, so I'd specify "$position=3" for this kind of document.

6) [if ( strpos($line, " * ")] -> converted in variable [if ( strpos($line, "$separator")]. Not a problem.

7) On name collisions append increasing numbers: "-001". It's an "auto" task after all...

Comments:
In your example you mixed the titles "Read Designer Notes! - Two-in-one - Yellow.2.4" and "Entrevista - Stephen Kanitz * TV Cultura".
Bear in mind they belong to different documents that will be worked on different times, that's why variables $separator and $position ["", "-1" or "+1" in case of $pos=2] are essentially required.

Using default separator [ * ] produced "Entrevista - Stephen Kanitz.diz", which is right in this case.

Mixed documents is not a problem as they'll be an exception. The script just needs to be ready to deal with several similarly-structured documents at a time containing a separator and a regular position to extract titles from.
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: help on auto renaming files

Post by Stefan »

tiago> generally they have a structure in which "Title[space][separator][space]Comment"
tiago> So [space]*[space] or [space]-[space] will be on different types of documents
tiago> Me knowing beforehand what I want, having that "$position" variable to control the searchment.
tiago> Got it?
I don't know yet, let's see.




tiago> On your script:
tiago> 1) It eliminated the last "4" out of "Read Designer Notes! - Two-in-one - Yellow.2.4.diz",
The script was not meant for that example yet.



tiago> 2) It should not invoke another renaming for the same document, stopping at the first match of each document;
tiago> 3) I'll need this to work with several documents
tiago> 4) The prompts. Is there a way to get rid of them,
tiago> 6) [if ( strpos($line, " * ")] -> converted in variable [if ( strpos($line, "$separator")]. Not a problem.
tiago> 7) On name collisions append increasing numbers: "-001". It's an "auto" task after all...
Such will come only with the final release, after it works in general.


tiago> 5) $position. For this specific document, $position=3 in the sense of "last match". A general solution may be this:
tiago>
tiago>
tiago>
I will try to understand.



tiago> Comments:
tiago> In your example you mixed the titles
tiago> "Read Designer Notes! - Two-in-one - Yellow.2.4"
tiago> and
tiago> "Entrevista - Stephen Kanitz * TV Cultura".
tiago> Bear in mind they belong to different documents that will be worked on different times,
I think this is clear now.
I will simple search for " * " and if not found search for " - ", ok?


Still not clear with
"Read Designer Notes! - Two-in-one - Yellow.2.4"
where i have understood first you want
"Two-in-one"
OR
"Yellow.2.4"
but maybe it will make sense once i read your point 5.) in whole.

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: help on auto renaming files

Post by tiago »

Great!
I was thinking on your proposition to use a line as name source.
It will be great for situations in which there is no separator at all.
So

Code: Select all

$line = 7 //"0" to use separator-based solution
IF ($line == 0) { ...separator-based solution... }
ELSEIF ($line != 0) { ...use the specified whole line as base name... }
In case output will be: "Entrevista - Stephen Kanitz * TV Cultura.diz" (reading whole line 7).
-extra care with forbidden characters in file names?

tiago> 5) $position. For this specific document, $position=3 in the sense of...
->->I will try to understand.
->->...but maybe it will make sense once i read your point 5.) in whole.
Some examples:

Designer - Notes - Two-in-one - Yellow.2.4
$pos = 1 (first sentence before the first separator on the first occurrence)
Out: Designer
=============
$pos = 2 (there's no mid term!)*
IF $pos == 2, $base = GETTOKEN (count) = 4; 4/2 = 2
$offset = "" //user will define this
$final = $base $offset

so
$offset = "" -> $final = 2"" -> $pos = $final -> $pos = 2 -> Out: Notes
$offset = "+1" -> $final = 2"+1" -> $pos = $final-> $pos = 3 -> Out: Two-in-one
=============
$pos = 3 (last sentence after the last separator on the first occurrence)
Out: Yellow.2.4

====================================
*You are free to think on an error-check test for cases in which there are tokens like 3
1 - 2 - 3 = mid term is 2.
or 5
1 - 2 - 3 - 4 - 5 = mid term is 3.
or 7
1 - 2 - 3 - 4 - 5 - 6 - 7 = mid term is 4.
ceil($base / 2) may be enough.
Of course this may be rare but the script will be unbeatable then.
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: help on auto renaming files

Post by Stefan »

This evening i had a little bit of time to see what is possible and how.

I came up with this algorithm:

(test code, real script will follow...i hope)

Code: Select all

  // test input:
  $file = "Designer - Notes - Two-in-one - Yellow.2.4";

  // user settings:
  $Pattern = " - ";         // the pattern to split the string at
  $StartPattern = "SOL";    // "SOL", 1, 2, 3,... Give me the part after pattern no.1, or 2, or start from "StartOfLine"
  $StartPosAdj = 3;         // 1, 2, 3,...        Remove additional signs (trim)
  $EndPattern = 3;          // "EOL", 2, 3, 4,... Till the part before pattern no.3, or 4, or till "EndOfLine"
  $EndPosAdj = ;            // 1, 2, 3,...        Remove additional signs (trim)


    // the code, don't modify:
         if ($StartPattern=="SOL"){ 
             $StartPos = 0;}
         else{
             $loop = 1;
             $PatternPos=1;
             while($loop <= $StartPattern){
               $StartPos = strpos($file, $Pattern, $PatternPos+1);
               $PatternPos = $StartPos;
               incr $loop;
             }
             $StartPos = $StartPos + $StartPosAdj;
         }
         if ($EndPattern=="EOL"){ 
             $EndPos = "";
             $namelen = "";}
         else{
             $loop = 1;
             $PatternPos=1;
             while($loop <= $EndPattern){
               $EndPos = strpos($file, $Pattern, $PatternPos+1);
               $PatternPos = $EndPos;
               incr $loop;
             }
             $EndPos = $EndPos - $EndPosAdj;
             $namelen = $EndPos-$StartPos;
         }

   $newname = substr($file, $StartPos, $namelen);
   msg $file<crlf>StartPos: $StartPos<crlf>EndPos:    $EndPos<crlf>#$newname#;  

Results examples:

Code: Select all

 $file = "Designer - Notes - Two-in-one - Yellow.2.4";

   // user settings:
  $Pattern = " - ";        // the pattern to split the string at
  $StartPattern = "SOL";   // "SOL", 1, 2, 3,... Give me the part after pattern no.1, or 2, or start from "StartOfLine"
  $StartPosAdj = 3;        // 1, 2, 3,...        Remove additional signs (trim)
  $EndPattern = 3;         // "EOL", 2, 3, 4,... Till the part after pattern no.3, or 4, or till "EndOfLine"
  $EndPosAdj = ;           // 1, 2, 3,...        Remove additional signs (trim)

---------------------------
XYplorer
---------------------------
Designer - Notes - Two-in-one - Yellow.2.4
StartPos: 0
EndPos: 29
#Designer - Notes - Two-in-one#
---------------------------
OK   
---------------------------

Code: Select all

   // user settings:
  $Pattern = " - ";      // the pattern to split the string at
  $StartPattern = 1;     // "SOL", 1, 2, 3,... Give me the part after pattern no.1, or 2, or start from "StartOfLine"
  $StartPosAdj = 3;      // 1, 2, 3,...        Remove additional signs (trim)
  $EndPattern = 2;       // "EOL", 2, 3, 4,... Till the part before pattern no.3, or 4, or till "EndOfLine"
  $EndPosAdj = ;         // 1, 2, 3,...        Remove additional signs (trim)
  
  ---------------------------
XYplorer
---------------------------
Designer - Notes - Two-in-one - Yellow.2.4
StartPos: 11
EndPos: 16
#Notes#
---------------------------
OK   
---------------------------

Code: Select all

   // user settings:
  $Pattern = " - ";      // the pattern to split the string at
  $StartPattern = 2;     // "SOL", 1, 2, 3,... Give me the part after pattern no.1, or 2, or start from "StartOfLine"
  $StartPosAdj = 3;      // 1, 2, 3,...        Remove additional signs (trim)
  $EndPattern = "EOL";   // "EOL", 2, 3, 4,... Till the part before pattern no.3, or 4, or till "EndOfLine"
  $EndPosAdj = ;         // 1, 2, 3,...        Remove additional signs (trim)

---------------------------
XYplorer
---------------------------
Designer - Notes - Two-in-one - Yellow.2.4
StartPos: 19
EndPos: 
#Two-in-one - Yellow.2.4#
---------------------------
OK   
---------------------------

Code: Select all

   // user settings:
  $Pattern = " - ";      // the pattern to split the string at
  $StartPattern = 2;     // "SOL", 1, 2, 3,... Give me the part after pattern no.1, or 2, or start from "StartOfLine"
  $StartPosAdj = 3;      // 1, 2, 3,...        Remove additional signs (trim)
  $EndPattern = 3;       // "EOL", 2, 3, 4,... Till the part before pattern no.3, or 4, or till "EndOfLine"
  $EndPosAdj = ;         // 1, 2, 3,...        Remove additional signs (trim)

---------------------------
XYplorer
---------------------------
Designer - Notes - Two-in-one - Yellow.2.4
StartPos: 19
EndPos: 29
#Two-in-one#
---------------------------
OK   
---------------------------



And no, i don't like how the forum look-a-like since last week. And i don't like this small, scrollable code boxes.




.

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: help on auto renaming files

Post by admin »

Stefan wrote:And no, i don't like how the forum look-a-like since last week. And i don't like this small, scrollable code boxes..
Huh? I did not change anything. :?

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: help on auto renaming files

Post by Stefan »

admin wrote:
Stefan wrote:And no, i don't like how the forum look-a-like since last week. And i don't like this small, scrollable code boxes..
Huh? I did not change anything. :?
Oh, that's good, then i say "sorry".
I think i had an rendering problem, that's why i thought you had changed smtg over the weekend according that thread >> Forum Style Poll.
And then i switched to subsilver2 where it has this smaller but scrollable code box.
Switching back to prosilver and today i can't understand anymore what i wanted to say. False alarm.

Post Reply