Removing characters from filenames Part II

Engaged ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

Hi everyone, everything fine?
Continuing with the script that removes characters, I'm trying to add a function that displays a "before and after" comparison at the end of the task, but I can't fix a nasty error. The before doesn't work.
I count on your help. Thanks.

Capturar.PNG

Current script:

var folder = Folder.selectDialog("Selecione a pasta de destino")
var files = folder.getFiles();
var file, extension;
for (var i = 0; i < files.length; i++){
    file = files[i];
    fName = decodeURI(file.name)
    extension = file.name.slice(file.name.length-4, file.name.length);
    file.rename("Pg#"+ fName.replace(/[^\d]+/g,"")+extension);
    
    var out = new File(Folder.desktop + "/fileNames.txt");  
    out.open("w");  
    out.encoding="UTF8";  
    for(var a in files){out.writeln( fName + " = "+decodeURI(files[a].name));}  
    out.close();  
        
    var ftxt = File (Folder.desktop + "/fileNames.txt" );
    ftxt.open("r");  var stringFromTextFile = ftxt.read();  
    var Result = stringFromTextFile.split();
}

 alert (Result, "Result" )
TOPICS
Actions and scripting

Views

263

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 01, 2022 May 01, 2022
Why not there (as continuation): Removing characters from filenames if ((fldr = Folder.selectDialog()) && (lngth = (fls = fldr.getFiles()).length)) { (out = File(Folder.desktop + '/nms.txt')).open('w') while(fls.length) nme = decodeURI((fle = fls.shift()).name), fle.rename('Pg#' + nme.replace(/[^\d]+/g, '') + fle.name .slice(-4)), out.writeln(nme + ' = ' + decodeURI(fle.name)) with(out) close(), open('r'), r = read(), close(), alert(r) }

Likes

Translate

Translate
LEGEND ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

Why not there (as continuation): Removing characters from filenames

if ((fldr = Folder.selectDialog()) &&
(lngth = (fls = fldr.getFiles()).length)) {
	(out = File(Folder.desktop + '/nms.txt')).open('w')
	while(fls.length) nme = decodeURI((fle = fls.shift()).name),
	fle.rename('Pg#' + nme.replace(/[^\d]+/g, '') + fle.name
	.slice(-4)), out.writeln(nme + ' = ' + decodeURI(fle.name))
	with(out) close(), open('r'), r = read(), close(), alert(r)
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

@Kukurykus , sorry for the delay, you are genius, it worked very well. thanks for the script.

"Why not there (as continuation): Removing characters from filenames"

I understand that a lot of master users don't appreciate pushing a topic that has already been resolved.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

Yes, but it's your topic and current thread is much related to original request.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

This script is very useful, it impressed me a lot.
@Shulipa Bernad@Kukurykus,I also tested it here and saw that the script modifies the name of the subfolders if they exist inside the main folder, is this on purpose?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

It was wrote to work only with files.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

@Kukurykusthanks for your instant reply.
I've been looking for this script for years.
a script that batch renames the name of any file, but without changing the numerical order of the files and the name of the subfolders if they exist:
ex:
Change to: prefix + 3 digits before dot( prefix+000.jpg)
"ALBUM_000_000_001 .jpg" = myPrefix_001.extension
"XXXXXX_000_000_002.jpg" = myPrefix_002.extension
" xxxxx(1).jpg" = myPrefix_001.extension

Could you write a script like this? Please share. Thanks.

The before and after function also extends the script

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

quote

@Kukurykusthanks for your instant reply.
I've been looking for this script for years.
a script that batch renames the name of any file, but without changing the numerical order of the files and the name of the subfolders if they exist:
ex:
Change to: prefix + 3 digits before dot( prefix+000.jpg)
"ALBUM_000_000_001 .jpg" = myPrefix_001.extension
"XXXXXX_000_000_002.jpg" = myPrefix_002.extension
" xxxxx(1).jpg" = myPrefix_001.extension

Could you write a script like this? Please share. Thanks.

The before and after function also extends the script


By @mauricior6328708
@Kukurykus this would be a more complete version of the script you wrote.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

 

/*'given file name convention'*/
.replace(/.*(\(\d+\)|\d+)(?=\.)/,
function(value) {return 'myPrefix_' +
('00' + value.split(/\D/).join('')).slice(-3)})

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

Great, wonderful script! @Kukurykus thank you for fulfilling my request.
It's almost 100% perfect,
1: It adds the extension twice
2: Still renames the subfolder.

How do we fix these two mistakes? Thanks

Captura de tela 2022-05-02 164247.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

It works exactly as it should, try my snippet with these names: 'ALBUM_000_000_001.jpg', 'XXXXXX_000_000_002.jpg', 'xxxxx(1).jpg', 'Folder Name', so few examples you shared.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

 

if ((fldr = Folder.selectDialog()) &&
(lngth = (fls = fldr.getFiles()).length)) {
	(out = File(Folder.desktop + '/nms.txt')).open('w')
	while(fls.length) nme = decodeURI((fle = fls.shift()).name),
	fle.rename(nme.replace(/.*(\(\d+\)|\d+)(?=\.)/,function(v){return 'myPrefix_' + ('00' + v.split(/\D/).join('')).slice(-3)}) + fle.name
	.slice(-4)), out.writeln(nme + ' = ' + decodeURI(fle.name))
	with(out) close(), open('r'), r = read(), close(), alert(r)
}

 

I tested all types of name and the result is the same

Captura de tela 2022-05-02 174330.jpg

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

Test it without script, but use only code I wrote for you.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

I'm leaving work, I'll test it when I get home

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

 

if ((fldr = Folder.selectDialog()) &&
(lngth = (fls = fldr.getFiles()).length)) {
	re = /.*(\(\d+\)|\d+)(?=\.)/; while(fls.length)
		(fle = fls.shift()).rename(decodeURI(fle.name)
		.replace(re, function(v) {return 'myPrefix_'
		+ ('00' + v.split(/\D/).join('')).slice(-3)}))
}

 

Including part I wrote 2 hours ago

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

It went wrong!

Capturar.PNG

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

I changed now incorrect order of assigned fle variable. Next time read execution result.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

@Kukurykus  throws an error on line 4

Capturara.PNG

Your last code seems to be incomplete, you eliminated the function that shows the alert with the names of the files "before and after", this function is fundamental.

 

 

if ((fldr = Folder.selectDialog()) &&
(lngth = (fls = fldr.getFiles()).length)) {
	(out = File(Folder.desktop + '/nms.txt')).open('w')
	while(fls.length) nme = decodeURI((fle = fls.shift()).name),
	fle.rename(nme.replace(/.*(\(\d+\)|\d+)(?=\.)/,function(v){return 'myPrefix_' + ('00' + v.split(/\D/).join('')).slice(-3)}) + fle.name
	.slice(-4)), out.writeln(nme + ' = ' + decodeURI(fle.name))
	with(out) close(), open('r'), r = read(), close(), alert(r)
}

 

 

This script works almost perfectly, if you can only fix the part that duplicates the extension and not rename the subfolder. Only that.

Captura de tela 2022-05-02 174330.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

I corrected it 3 seconds ago. Combine yourself an eliminated alert part from previous version.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

@Kukurykus I tried all morning to add the alert function in the current script. however without success. Where was I wrong? Could you show me how to fix it or send me the complete code with the appropriate corrections? Thanks

if ((fldr = Folder.selectDialog()) &&
(lngth = (fls = fldr.getFiles()).length)) {
    (out = File(Folder.desktop + '/nms.txt')).open('w')
    
	re = /.*(\(\d+\)|\d+)(?=\.)/; while(fls.length) nme = decodeURI((fle = fls.shift()).name),
		(fle = fls.shift()),nme.rename(decodeURI(fle.name)
		.replace(re, function(v) {return 'myPrefix_'
		+ ('00' + v.split(/\D/).join('')).slice(-3)}))
    
    out.writeln(nme + ' = ' + decodeURI(fle.name))
	with(out) close(), open('r'), r = read(), close(), alert(r)
}

Captura de tela 2022-05-03 101403.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

if ((fldr = Folder.selectDialog()) &&
(lngth = (fls = fldr.getFiles()).length)) {
	str = ''; re = /.*(\(\d+\)|\d+)(?=\.)/; while(fls.length)
		(fle = fls.shift()).rename((nme = decodeURI(fle.name))
		.replace(re, function(v) {return 'myPrefix_'
		+ ('00' + v.split(/\D/).join('')).slice(-3)})),
		str += decodeURI(fle.name) + ' & '
		+ nme + '\n'; alert(str)
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

LATEST

Perfect! Now it worked very well.
@Kukurykus thanks for being so kind and thoughtful and gifting me with this wonderful script.
@Shulipa Bernad thanks for the topic

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

@mauricior6328708 really I confess that I had not tested with subfolders, it would be perfect not to change the name of the subfolders. Thanks for that important detail

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

For only jpg files use fldr.getFiles('*.jpg')

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines