Author Topic: Create new file with name taken from selected file or clipboard  (Read 16889 times)

multitek

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hello,

I'm trying to create a button/hotkey that would create a new empty textfile (without extension) in source and name it ideally using the filename (also without extension) of the file currently selected in source, or if that's not possible from clipboard.

Example with name from selected in source:
In source I select a file named: NiceName.whatever (the extension is irrelevant, could be anything)
Running the script does the following:
1. takes NiceName from the selected file
2. creates a new empty text file in source
3. names it NiceName (the filename doesn't have any extension at all)

The clipboard variant:
I copy the name I want to the clipboard, for example ClipboardName
Running the script does the following:
1. creates a new empty text file in source
2. names it ClipboardName (again no extension, what's on the clipboard is the full name)


optional bonus: doing this on multiple files at once. So I select multiple files and the script performs the described procedure on all of them.

I've been going through the documentation for a while now, but sadly haven't found a way to create such a script, so your help is much appreciated.

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 536
  • Old Skull
    • View Profile
Re: Create new file with name taken from selected file or clipboard
« Reply #1 on: April 06, 2017, 08:54:23 »
Try this:
Code: [Select]
@var $dir = GetSourcePath();
@var $sel = GetSourceSelectedFileNames();
//@var $sel = StrLines2Array( GetClipboardText() );

@var $c = arrayCount( $sel );
@var $n;
@var $f;

for( $n = 0; $n < $c; $n++ )
{
  $f = $dir ^ "\\" ^ PathGetNamePart( $sel[ $n ], 1 );
  if( FileExists( $f ) == 0 )
  {
    SaveStringToFile( $f, "" );
  }
}

For selected files use
Code: [Select]
@var $sel = GetSourceSelectedFileNames();
For clipboard use
Code: [Select]
@var $sel = StrLines2Array( GetClipboardText() );

multitek

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Create new file with name taken from selected file or clipboard
« Reply #2 on: April 06, 2017, 15:01:17 »
Oh man you are a gem, the script works great (both variants). I really appreciate it, thanks a lot :)


Now let me see if I correctly understand how the code works, to get a better grasp on scripting that I just started looking at yesterday...

@var $dir = GetSourcePath();
// assign source path to the dir variable

@var $sel = GetSourceSelectedFileNames();
// assign selected filenames to the sel variable. since sel is not explicitly declared as an array, does this mean a variable automatically becomes an array by assigning to it multiple values (ie filenames)?

@var $sel = StrLines2Array( GetClipboardText() );
// assign filenames on the clipboard to the sel variable. internal function StrLines2Array converts mutiple lines of string into an array

@var $c = arrayCount( $sel );
// assign the number of elements in the array sel to the c variable

@var $n;
@var $f;
// declare variables, n for the following loop counter, f for filenames

for( $n = 0; $n < $c; $n++ )
// setting up the loop to go through all the elements in the array. start with n at zero, execute the loop and increase n by 1 for as long as n is smaller than c, ie the number of elements in the array

{
  $f = $dir ^ "\\" ^ PathGetNamePart( $sel[ $n ], 1 );
//this gets the filename part from the current array element and assigns it to the variable f, but I'm confused about the syntax. Specifically the "$dir ^ "\\" ^ " part. I'm pretty sure I would have made the mistake here of simply writing "$f = PathGetNamePart( $sel[ $n ], 1 );" Ah wait (sorry for writing as I think ;) ), we need the full length path name for the file we are about to create, not just its filename, so the part I'm asking about is prepending the filename with the full path, correct? Ok, just found ^ in the documentation so I think I was right there. But looking at the documentation example, why wouldn't "$f = $dir ^ PathGetNamePart( $sel[ $n ], 1 );" work? Or would it?

  if( FileExists( $f ) == 0 )
//check if a file already exists so that we don't overwrite it with the file we're about to create.
  {
    SaveStringToFile( $f, "" );
//finally create the file with the filename we stored in variable f
  }
}

Again let me thank you for your help, hope you don't mind me commenting the code :)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4402
    • View Profile
    • Multi Commander
Re: Create new file with name taken from selected file or clipboard
« Reply #3 on: April 06, 2017, 18:37:57 »
Code: [Select]
$dir ^ "\\" ^ PathGetNamePart( $sel[ $n ], 1 );
If I'm going to be picky I don't think the "\\" part is needed  :)
The idea with ^ is that when you append a path items, it will add the "\" part it self if needed. It will check it already is there on one of the strings, and if not add it.

This is the same
Code: [Select]
$s = "C:\\MyFolder" + "\\" + "filename.txt";
$s = "C:\\MyFolder" ^ "filename.txt";

multitek

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Create new file with name taken from selected file or clipboard
« Reply #4 on: April 06, 2017, 19:32:13 »
Thanks for the clarification Mathias :)