Script: Copy Here with Suffix Number

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Script: Copy Here with Suffix Number

Post by Stefan »

Script: Create new file from base name
- how to handle duplicates?
- how to get the computed new name?
-----------------------------------------------------------------------

I am working on an "create new file from <curitem>"
and tried to code an algo like "#164 Copy Here with Suffix Number" at position (3a)


I can use #164 in an script..... but
Question:
how can i select this fresh created file to open it?
(i mean simple!, no big workarounds like select by creating time,... on the other hand... maybe an idea, will try...)

Or could #164 calculate the new, unique file name for me and provide it me for re-use?


EDIT: see next posts below for an corrected version.

Code: Select all

// Create an new TXT-file with base-name of current selected item:
// (1) if cursor is over an file or folder use this current name (base only, without extension)
//      else use "New File" as name:

$base = (<curbase>)? <curbase>: "New file from <date yyyy-mm-dd>";
     // (2) prompt user to correct the name:
    $base = Input('Create new file',New file name based on: $base, $base.".txt");

   // (3) if file already exists, add an trailing digit:
    IF exists($base)=1 {
          // (3a) Copy Here with Suffix Number (my own code is removed to simplify, and because not really working)
           #164;
         // (4) open notepad to edit this new file:
            Run Notepad $new_#164_file;

    }else{
         // (3b) create the new file in current dir:
            new $base, file;
         // (4) open notepad to edit this new file:
            Run Notepad $base;
    }
---------------
Last edited by Stefan on 15 Oct 2009 22:13, edited 1 time in total.

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Re: Script: Copy Here with Suffix Number

Post by jacky »

I'm a bit confused, as your code seem to either create a new (empty) file or make a copy of the current item, but either way : Why don't you simply just use SC new ?

Look it up, it accepts as third parameter a path/name to a source item, meaning the newly created item will indeed by a clone/copy of the specified source. And, if there is already an item by the name you're trying to create, XY does automatically add a number suffix (using format as defined on Configuration - Templates). Sounds like what you're trying to do, no?
Proud XYplorer Fanatic

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Script: Copy Here with Suffix Number

Post by TheQwerty »

jacky wrote:And, if there is already an item by the name you're trying to create, XY does automatically add a number suffix
And there's the rub.
Stefan, wants to open the file after creating it, so when XY automatically gives it a suffix the script now has to guess what the name is (or set about determining it).

Ideally, New() and similar should return or provide some means to discover the eventually used filename.

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

Re: Script: Copy Here with Suffix Number

Post by Stefan »

Hi jacky, thanks for help.
jacky wrote: Why don't you simply just use SC new ?

Look it up, it accepts as third parameter a path/name to a source item,
meaning the newly created item will indeed by a clone/copy of the specified source.
This would clone the whole file including the content.
If i understand the help right:
Cloning a source: Create Files Or Folders With Content
....
...a file, a new file with the same content will be created;
I just want to "clone" the base name of an file:
Select 'XYplorer.exe'
create 'XYplorer.TXT'



jacky wrote: And, if there is already an item by the name you're trying to create,
XY does automatically add a number suffix (using format as defined on Configuration - Templates).
You are absolutely right, that's what i try to reinvent :lol: silly me.
(I have though i have to think about this possibility too, but have overseen then Donald already have though on this)



Sounds like what you're trying to do, no?
Nearly done. Yes, but...

EXAMPLE:
Select 'XYplorer.TXT'
$base = 'XYplorer'
new $base . TXT ----- will create 'XYplorer-01.TXT' ---- fine!

but 'run Notepad $base' will open the old file, not the newly created one ('XYplorer-01.TXT').

QUESTION:
So how can i automatically open the new created file, which name i doesn't know?
The only workaround i can imaging is to sort by create date and select the first one.
Or, Don, would we be able to access the new developed 'undo list' and pick the last item of them?

--
EDIT: thanks to TheQwerty too. He is right, that is what i am looking for.

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Re: Script: Copy Here with Suffix Number

Post by jacky »

Alright, sorry about the confusion. I wasn't sure because you mention & your script use #164; (Copy Here with Suffix Number), so I didn't know whether you wanted to actually clone the file or not. Anyways...
Stefan wrote:So how can i automatically open the new created file, which name i doesn't know?
Well, it seems you are in the folder where said new file is being created, right? Correct me if I'm wrong, but in that case isn't the newly created file automatically selected (on List) upon creation (meaning you can simply use <curitem> then) ? I don't remember if there's any setting affecting this, but it works that way for me (w/ or w/out AR).

(I agree though, it would be nice indeed if new could be a function returning the name of the created item, especially when it's not in the current location, when there is then no way to know the name of the newly created item.)
Proud XYplorer Fanatic

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

Re: Script: Create new text file from current item base name

Post by Stefan »

jacky wrote: Well, it seems you are in the folder where said new file is being created, right? Correct me if I'm wrong, but in that case isn't the newly created file automatically selected (on List) upon creation (meaning you can simply use <curitem> then) ?
I can't correct you because you are right.

> simply use <curitem> then
Clever :lol:

This work as expected:

Code: Select all

/* Purpose: Create an new *.txt-file.
   If cursor is over an file or folder, use this current name 
   (base only, without extension) as name for the new file.
   F.ex.: this script on 'XYplorer.exe' creates new file 'XYplorer.txt'
   ELSE create an file with name "New file from 2009-10-15" 
*/ 
// (1) Check if there is an file or folder selected
$base = (<curbase>)? <curbase>: "New file from <date yyyy-mm-dd>";
 // (2) Add an '.txt' extension and prompt user to confirm the name:
    $base = Input('Create new file',New file name based on: $base, $base.".txt");
 // (3) Create the new file in current dir,
 // if the file name already exists XY does automatically add a number suffix
 // (using format as defined on Configuration - Templates).
    new $base, file;
 // (4) Open the new created file with notepad:
    //Run Notepad <curitem>;
    Run "<xypath>\Tools\NotePad2\NotePad2.exe" <curitem>;
Only the Input-Box shows the expected name only and not the computed name, if needed.

Thanks mates, for the great support :mrgreen:

Post Reply