clean filename
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
clean filename
Hello,
is there a way to clean filename by script - I need to get rid of spaces and capitalized letters ?
is there a way to clean filename by script - I need to get rid of spaces and capitalized letters ?
Windows 10 64 bits
-
highend
- Posts: 14996
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: clean filename
Clean spaces -> replace()
Lower letters -> recase()
renameitem() does the rest
+ a foreach loop for all files / folders
Lower letters -> recase()
renameitem() does the rest
+ a foreach loop for all files / folders
One of my scripts helped you out? Please donate via Paypal
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
Thanks, I will study this - I did not think to look at documentation 
Windows 10 64 bits
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
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.
Grabbing the name of the file, cleaning it and rename it after this.
Windows 10 64 bits
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
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";Windows 10 64 bits
-
highend
- Posts: 14996
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: clean filename
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:
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";
One of my scripts helped you out? Please donate via Paypal
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
Thanks for these precisions. I must admit I'm not a coder...highend wrote:Great
And for better code in the future, follow these rules (for better readability):
Windows 10 64 bits
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
For some reason, my code does not work anymore.
Resume: I change files name and resize images.
It works only for first file and don't apply to next files selected.
Sorry, I know code is not very clean.
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";Sorry, I know code is not very clean.
Windows 10 64 bits
-
highend
- Posts: 14996
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: clean filename
Put your first lines into the foreach loop and replace <curbase> with gpc($file, "base")
One of my scripts helped you out? Please donate via Paypal
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
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.highend wrote:Put your first lines into the foreach loop and replace <curbase> with gpc($file, "base")
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";Windows 10 64 bits
-
highend
- Posts: 14996
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: clean filename
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";
One of my scripts helped you out? Please donate via Paypal
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
Thanks, your code works nice. Why do you chose runret instead than run ( I saw there is no more dos window blinking with runret) ?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";
Windows 10 64 bits
-
highend
- Posts: 14996
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: clean filename
If necessary the output of that command can be captured and parsed.
One of my scripts helped you out? Please donate via Paypal
-
lian00
- Posts: 429
- Joined: 09 Jul 2014 17:12
Re: clean filename
Well, too complex for my knowledge :-)highend wrote:If necessary the output of that command can be captured and parsed.
Windows 10 64 bits
-
LittleBiG
- Posts: 1848
- Joined: 08 Apr 2011 12:57
- Location: Win10x64
Re: clean filename
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.lian00 wrote:Well, too complex for my knowledge :-)highend wrote:If necessary the output of that command can be captured and parsed.
XYplorer Beta Club