Page 1 of 2

clean filename

Posted: 28 Mar 2016 17:03
by lian00
Hello,
is there a way to clean filename by script - I need to get rid of spaces and capitalized letters ?

Re: clean filename

Posted: 28 Mar 2016 17:54
by highend
Clean spaces -> replace()
Lower letters -> recase()
renameitem() does the rest

+ a foreach loop for all files / folders

Re: clean filename

Posted: 28 Mar 2016 18:00
by lian00
Thanks, I will study this - I did not think to look at documentation :roll:

Re: clean filename

Posted: 29 Mar 2016 11:28
by lian00
OK, a simple rename s managed the spaces - the more important because XnView Convert does not manage files with spaces in the name. But for capitalisation, I suspect I will have more work to do.
Grabbing the name of the file, cleaning it and rename it after this.

Re: clean filename

Posted: 29 Mar 2016 11:37
by lian00
OK, I'm proud to show MY very complicated code :-)

Code: Select all

        $nom = <curbase>;
	$nomsimple = recase("$nom", "lower");
	$nomsimple = replace("$nomsimple", " ","-"); 
 	rename s, "$nom/$nomsimple";

Re: clean filename

Posted: 29 Mar 2016 11:57
by highend
Great :)

And for better code in the future, follow these rules (for better readability):

- Do not use localized variable names, use english ones
- If you use glued names (like $nomsimple), either use camel case (with the first letter in lower case) or e.g. an underscore
For example: $simpleName or $simple_name
- Use correct indentation (4 spaces or a tab, spaces preferred)
- You don't need quotation marks around variable names (at least not if they stand alone like in line 2 + 3)

So in the end it could look like this:

Code: Select all

    $name = <curbase>;
    $nameSimple = recase($name, "lower");
    $nameSimple = replace($nameSimple, " ", "-");
    rename s, "$name/$nameSimple";

Re: clean filename

Posted: 29 Mar 2016 12:43
by lian00
highend wrote:Great :)

And for better code in the future, follow these rules (for better readability):
Thanks for these precisions. I must admit I'm not a coder...

Re: clean filename

Posted: 23 May 2016 08:55
by lian00
For some reason, my code does not work anymore.
Resume: I change files name and resize images.

Code: Select all

"Redim 950"
	$nom = <curbase>;
	$nomsimple = recase("$nom", "lower");
	$nomsimple = replace("$nomsimple", " ","-"); 
 	rename s, "$nom/$nomsimple";
	wait 300; 
    foreach($file, "<get SelectedItemsPathNames |>") {  
    $script= '"C:\Program Files\nconvert\nconvert.exe" '."-ratio -rtype lanczos -rflag orient -quiet -resize longest 950 -out jpeg -o E:\images\Output\%.jpg"." $file";
        run $script;  
        wait 1000; 
        } 
    end confirm ("Allez à Output ?") == 0; 
    goto "E:\images\Output";
It works only for first file and don't apply to next files selected.
Sorry, I know code is not very clean.

Re: clean filename

Posted: 23 May 2016 09:41
by highend
Put your first lines into the foreach loop and replace <curbase> with gpc($file, "base")

Re: clean filename

Posted: 23 May 2016 10:10
by lian00
highend wrote:Put your first lines into the foreach loop and replace <curbase> with gpc($file, "base")
Thanks for help. But it won't work as $file is necessary for nconvert script and the renaming the file "breaks" the validity of $file.

OK, I seem to solved it with a second foreach

Code: Select all

foreach($file, "<get SelectedItemsPathNames |>") { 
    $nom = gpc($file, "base");
	$nomsimple = recase("$nom", "lower");
	$nomsimple = replace("$nomsimple", " ","-"); 
 	rename s, "$nom/$nomsimple";
	wait 300; 
     } 
    foreach($file, "<get SelectedItemsPathNames |>") { 
    $script= '"C:\Program Files\nconvert\nconvert.exe" '."-quiet -ratio -rtype lanczos -rflag orient -resize longest 850 -out jpeg -o E:\images\Output\%.jpg"." $file";
        run $script;  
        wait 1000; 
        } 
    end confirm ("Allez à Output ?") == 0; 
    goto "E:\images\Output";

Re: clean filename

Posted: 23 May 2016 10:32
by highend
You want the original file renamed and each of these renamed files converted by nconvert, right?

Code: Select all

"Redim 950"
    foreach($file, "<get SelectedItemsPathNames |>") {
        $ext     = gpc($file, "ext");
        $oldName = gpc($file, "base");
        $newName = replace(recase($oldName, "lower"), " ", "-");
        rename "s", "$oldName/$newName";
        runret("""C:\Program Files\nconvert\nconvert.exe"" -ratio -rtype lanczos -rflag orient -quiet -resize longest 950 -out jpeg -o ""E:\images\Output\%.jpg"" ""<curpath>\$newName.$ext""");
    }
    end confirm("Allez à Output ?") == 0;
    goto "E:\images\Output";

Re: clean filename

Posted: 23 May 2016 10:44
by lian00
highend wrote:You want the original file renamed and each of these renamed files converted by nconvert, right?

Code: Select all

"Redim 950"
    foreach($file, "<get SelectedItemsPathNames |>") {
        $ext     = gpc($file, "ext");
        $oldName = gpc($file, "base");
        $newName = replace(recase($oldName, "lower"), " ", "-");
        rename "s", "$oldName/$newName";
        runret("""C:\Program Files\nconvert\nconvert.exe"" -ratio -rtype lanczos -rflag orient -quiet -resize longest 950 -out jpeg -o ""E:\images\Output\%.jpg"" ""<curpath>\$newName.$ext""");
    }
    end confirm("Allez à Output ?") == 0;
    goto "E:\images\Output";
Thanks, your code works nice. Why do you chose runret instead than run ( I saw there is no more dos window blinking with runret) ?

Re: clean filename

Posted: 23 May 2016 11:06
by highend
If necessary the output of that command can be captured and parsed.

Re: clean filename

Posted: 23 May 2016 11:45
by lian00
highend wrote:If necessary the output of that command can be captured and parsed.
Well, too complex for my knowledge :-)

Re: clean filename

Posted: 23 May 2016 13:25
by LittleBiG
lian00 wrote:
highend wrote:If necessary the output of that command can be captured and parsed.
Well, too complex for my knowledge :-)
Sorry but I doubt it. It means you can catch the output of command in the DOS window into a variable. So if the NConvert wrote something as a result into the DOS window, you could put it into a variable.