Page 1 of 1

Quick File Edit

Posted: 06 Nov 2008 23:23
by jacky
Just a little script to allow you to quickly edit small text files directly within XY without the need of any external app -- especially useful for quick edits on text files, INI/Config files, etc

Code: Select all

"Quick File Edit... : QuickFileEdit"
	sub (getinfo("CountSelected") == 1) ? ((report("{Size RAW}", 1) <= 102400) ? "_QuickFileEdit" : "_qfeSizeError") : "_qfeSelError";
"_qfeSelError"
	msg "You need to have one (and only one) selected item !";
"_qfeSizeError"
	msg "You can only edit files of 100 KB (102 400 bytes) and less !";
"_QuickFileEdit"
	$file = report("{Fullname}", 1);
	input $data, "Quick File Edit: $file", readfile($file), w;
	status writefile($file, $data) ? "File saved" : "Error writing file";
Note that the limit to 100 KB files is totally arbitrary, I didn't do any test to see how much XY could actually handle or anything, just seemed a right choice for me. XY probably supports for more, but obviously this is probably best suited only for small files, otherwise you'll want a real text editor.

Re: Quick File Edit

Posted: 07 Nov 2008 09:20
by admin
Wow, slick! :D

Re: Quick File Edit

Posted: 06 Feb 2013 18:40
by Slavaon
Hi jacky and all.
Wonderful and creative script!
It is possible to add command to keep and close in your script? I just need to convert the *.txt files from UTF-8 in 1251 ANSI.
Thanks to all responded.

Re: Quick File Edit

Posted: 11 Sep 2013 22:55
by kiwichick
Hi there, I stumbled across this old thread and thought what a great idea it was!!! So I copied the code, cleaned up the SC changes and away it went - awesome!!! But I wanted to extend it and make it a bit more than it was. I wanted it to:

1. End when more than one file is selected.
2. End when a 'non-valid' filetype is selected.
3. Open inputfile dialog when nothing is selected (then it could be used for any file, anywhere, not just limited to the current folder).

So far I've got two lots of code that work independently but I'm having real trouble getting them to work together.

This one fulfills numbers 1&3:

Code: Select all

"QuickFileEdit-2-works"
//opens inputfile on no selection
//BUT works on any filetype
   end(get("CountSelected") > 1), "Only one item may be selected.";
   if (get("CountSelected") < 1) {
   focus "l";
   $file = inputfile("<curpath>", "txt|xys|ini", "Select file to Edit");
   }
   else {
   $file = report("{Fullname}", 1);
   }
   $data = input("Quick File Edit: $file", ,readfile($file), w);
   status writefile($file, $data);
And this one fulfills numbers 1&2:

Code: Select all

"QuickFileEdit-3-works"
//works on valid filetypes only
   end(get("CountSelected") > 1), "Only one item may be selected.";
   $file = report("{Fullname}", 1);
   $ext = get("curitem", "ext");
   if($ext==txt||$ext==xys||$ext==ini){
   $data = input("Quick File Edit: $file", ,readfile($file), w);
   status writefile($file, $data);
   }
   else {
   msg "Invalid filetype selected.";
   break;
   }
I'm pretty sure I just need to include an elseif but I can't for the life of me figure out what order the pieces of code should go in.

I'm also sure there must be a better way than using if($ext==txt||$ext==xys||$ext==ini) to set the 'valid' file extensions but I couldn't figure out how to make a variable contain multiple values (or any other command I should use instead).

Thanks for any help.

Re: Quick File Edit

Posted: 11 Sep 2013 23:38
by highend

Code: Select all

    end(get("CountSelected") > 1), "Only one item may be selected.";
    if (get("CountSelected") < 1) {
        focus "l";
        $file = inputfile("<curpath>", "txt|xys|ini", "Select file to Edit");
    } else {
        $file = "<curitem>";
    }
    $ext = getpathcomponent("$file", "ext");
    if ("txt|xys|ini" LikeI "*$ext*") {
        $data = input("Quick File Edit: $file", ,readfile($file), w);
        status writefile($file, $data);
    }

Re: Quick File Edit

Posted: 12 Sep 2013 00:39
by kiwichick
Fabulous highend :appl: :appl: :appl: :appl: :appl: :appl: Thank you so much!!

Now I'm off to compare yours with mine to see where you made cuts and changes - a great way to learn more!!!

Re: Quick File Edit

Posted: 15 May 2014 14:12
by sinilill
highend wrote:

Code: Select all

    end(get("CountSelected") > 1), "Only one item may be selected.";
    if (get("CountSelected") < 1) {
        focus "l";
        $file = inputfile("<curpath>", "txt|xys|ini", "Select file to Edit");
    } else {
        $file = "<curitem>";
    }
    $ext = getpathcomponent("$file", "ext");
    if ("txt|xys|ini" LikeI "*$ext*") {
        $data = input("Quick File Edit: $file", ,readfile($file), w);
        status writefile($file, $data);
    }
Is it possible to change the editing window size?

Re: Quick File Edit

Posted: 15 May 2014 14:26
by bdeshi
Sure, just edit a line near the end of the script.
Find this line:

Code: Select all

$data = input("Quick File Edit: $file", ,readfile($file), w);
And replace it with:

Code: Select all

$data = input("Quick File Edit: $file", ,readfile($file), w, , 350, 200);
Here 350 is width and 200 is height (on in pixels).

Re: Quick File Edit

Posted: 15 May 2014 14:51
by highend

Code: Select all

// Quick edit a file
    $validExtensions = "|bat|cmd|txt|xys|ini|";

    end(get("CountSelected") > 1), "Only one item may be selected.";
    if (get("CountSelected") < 1) {
        focus "l";
        $file = inputfile("<curpath>", $validExtensions, "Select file to Edit...");
    } else {
        $file = "<curitem>";
    }
    $ext = getpathcomponent("$file", "ext");
    if (strpos($validExtensions, "|$ext|") != -1) {
        $data = input("Quick File Edit: $file", , readfile($file), "w", , 800, 500);
        writefile($file, $data);
    }
Window size edited and made a change to allow the editing of allowed file extensions easier.
Leading and Trailing "|" in $validExtensions are necessary, don't remove them.

Re: Quick File Edit

Posted: 15 May 2014 15:05
by sinilill
:appl: :appl: to both of you

Re: Quick File Edit

Posted: 30 May 2014 20:03
by aurumdigitus
More code blocks of near inestimable value!

Thanks fellas. :appl: