Quick File Edit

Discuss and share scripts and script files...
Post Reply
jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Quick File Edit

Post 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.
Proud XYplorer Fanatic

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

Re: Quick File Edit

Post by admin »

Wow, slick! :D

Slavaon
Posts: 158
Joined: 29 Mar 2012 07:35

Re: Quick File Edit

Post 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.

kiwichick
Posts: 557
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Quick File Edit

Post 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.
Windows 10 Pro 22H2

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Quick File Edit

Post 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);
    }
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 557
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Quick File Edit

Post 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!!!
Windows 10 Pro 22H2

sinilill
Posts: 111
Joined: 02 Dec 2013 18:37

Re: Quick File Edit

Post 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?

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Quick File Edit

Post 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).
Last edited by bdeshi on 16 May 2014 11:14, edited 1 time in total.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Quick File Edit

Post 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.
One of my scripts helped you out? Please donate via Paypal

sinilill
Posts: 111
Joined: 02 Dec 2013 18:37

Re: Quick File Edit

Post by sinilill »

:appl: :appl: to both of you

aurumdigitus
Posts: 1075
Joined: 30 May 2008 21:02
Location: Lake Erie

Re: Quick File Edit

Post by aurumdigitus »

More code blocks of near inestimable value!

Thanks fellas. :appl:

Post Reply