Script to rename files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Script to rename files

24 Nov 2018, 17:26

I'm gonna face a very tedious task of having to rename a bunch of files distributed in several folders. I would like to make a script that automatically applies some changes to a file's name when it's selected. I don't want a batch renamer, as not all the files in the folder will need to be renamed. I'd rather press a hotkey to do it. There are three things I need the script to do:
  1. It needs to append a prefix to every file's name;
  2. It needs to replace all spaces with underscore ("_");
  3. It needs to replace all special symbols with the regular letter (e.g. "à" becomes "a"; "ç" becomes "c"
I found this script on the archived forums:

F1:: ;f1 is the hotkey
Backupclip := clipboard ;backs up the clipboard
Send ^a^c ;control a then control c
StringReplace, clipboard, clipboard, _, %a_space%, All
send ^v ; control +v (paste)
clipboard := Backupclip ;updated thanks to codekiller
return


It seems to work OK if the text is written in a text editor, but not for file names. Besides, it only does the opposite of what I need on item 2. I tried to edit it, and this is what I got so far:

^B::
Send {F2}
;Backupclip := clipboard ;backs up the clipboard
;Send ^c
;StringReplace, clipboard, clipboard, _, %a_space%, All
;send ^v ; control +v (paste)
;clipboard := Backupclip ;updated thanks to codekiller
Send {HOME}
Send Prefix_ ;This is the prefix I need to append to all file names
Send {Enter}
return


The uncommented lines are the ones I managed to get working right, but they only get item 1 done. I can't get it to replace the whitespace with "_". I assume whatever I do to achieve this could be edited to replace special characters with the regular letters.

Any help is appreciated.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

24 Nov 2018, 23:50

this code will loop through all selected files in windows explorer and display a yes/no question before renaming the file for you.

Code: Select all

oWord := {"à":"a", "ç":"c"}		; add more letter pairs as you want
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
return

^b::
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename
	Newfilename := StrReplace(Newfilename, " ", "_") 
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 
	MsgBox, 262180, , %  "rename: `n`t" filePath "\" filename "`nto:`n`t" filePath "\" Newfilename
	IfMsgBox, Yes
		FileMove, % filePath "\" filename, % filePath "\" Newfilename
}
return
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

25 Nov 2018, 07:53

AlphaBravo wrote:
24 Nov 2018, 23:50
this code will loop through all selected files in windows explorer and display a yes/no question before renaming the file for you.

Code: Select all

oWord := {"à":"a", "ç":"c"}		; add more letter pairs as you want
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
return

^b::
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename
	Newfilename := StrReplace(Newfilename, " ", "_") 
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 
	MsgBox, 262180, , %  "rename: `n`t" filePath "\" filename "`nto:`n`t" filePath "\" Newfilename
	IfMsgBox, Yes
		FileMove, % filePath "\" filename, % filePath "\" Newfilename
}
return
Thank you very much for this! I have edited in the other letter pairs I needed and the respective upper case counterparts. I tested it and it seemed to work OK.

The message box was a little annoying, so I removed the lines that included it. From what I could test, it seems to be working OK. Could you please take a look and check if I didn't mess anything up? Thanks in advance!

Code: Select all

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "ê":"e", "í":"i", "ó":"o", "õ":"o", "ô":"o", "ú":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "Í":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ú":"U", "Ç":"C"}
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
return

^b::
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 
		FileMove, % filePath "\" filename, % filePath "\" Newfilename
}
return
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

25 Nov 2018, 12:05

looks good, for readability purposes only you may want to adjust indentation as such

Code: Select all

	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 
	FileMove, % filePath "\" filename, % filePath "\" Newfilename
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

25 Nov 2018, 16:21

Thanks again!
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

26 Nov 2018, 05:54

Hello there again.

The script was working perfectly on my personal computer, but when I tried it at my computer at work (the one in which I'm supposed to make all the changes), it simply did not work. Actually, it works occasionally (like once every 20 tries) and when it does, it creates new files with the correct names but keeps the old ones with the original names. Refreshing with F5 gets rid of the old files. In one instance it appended the prefix, but some of the special characters did not get auto replaced.

I thought the fact that the files were on a server could have anything to do with it, but even after copying them to my machine's hard drive, it did not solve the problem.

Here's the exact code I'm using. Could there be anything wrong with it? I'm running it on a laptop with Windows 10 Pro in it:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "ê":"e", "í":"i", "ó":"o", "õ":"o", "ô":"o", "ú":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "Í":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ú":"U", "Ç":"C"}
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
return

^B::
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 
	FileMove, % filePath "\" filename, % filePath "\" Newfilename
}
return
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

26 Nov 2018, 15:46

in private message, I suggested adding the following to top of script (copied from the forum.)

Code: Select all

full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
	try
	{
		if A_IsCompiled
			Run *RunAs "%A_ScriptFullPath%" /restart
		else
			Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
	}
	ExitApp
}
@Avastgard is still unable to get it to work.
also suggested

Code: Select all

sel.item(A_Index-1).name := Newfilename
instead of file move.
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

26 Nov 2018, 16:37

Running the script like displayed below triggers a prompt for Admin login and password. Since I don't have it, the script cannot go on:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
	try
	{
		if A_IsCompiled
			Run *RunAs "%A_ScriptFullPath%" /restart
		else
			Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
	}
	ExitApp
}

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "ê":"e", "í":"i", "ó":"o", "õ":"o", "ô":"o", "ú":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "Í":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ú":"U", "Ç":"C"}
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
return

^b::
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename	
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord	
		Newfilename := StrReplace(Newfilename, k, v) 	
	FileMove, % filePath "\" filename, % filePath "\" Newfilename
}
return
Removing the section before the hotkey and changing the last line as suggested by AlphaBravo works partially (yey!), but only once. Trying to select another file and then hitting the hotkey triggers a message saying that one of the files that were renamed before cannot be found and the script is halted. One important thing to mention is that the file not found is mentioned with its name before it was renamed (no wonder it can't be found). So the script below works, but I have to reload it after every rename:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "ê":"e", "í":"i", "ó":"o", "õ":"o", "ô":"o", "ú":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "Í":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ú":"U", "Ç":"C"}
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
return

^b::
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename	
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord	
		Newfilename := StrReplace(Newfilename, k, v) 	
	sel.item(A_Index-1).name := Newfilename
}
return
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files  Topic is solved

26 Nov 2018, 16:45

try now:

Code: Select all

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "ê":"e", "í":"i", "ó":"o", "õ":"o", "ô":"o", "ú":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "Í":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ú":"U", "Ç":"C"}
return

^b::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename	
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord	
		Newfilename := StrReplace(Newfilename, k, v) 	
	sel.item(A_Index-1).name := Newfilename
}
shellWindows  := ""
return
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

26 Nov 2018, 20:29

Works flawlessly on my home computer as the first script did. I will try tomorrow on my office's laptop and report back. Thanks for all the help!
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

27 Nov 2018, 05:23

AlphaBravo wrote:
26 Nov 2018, 16:45
try now:

Code: Select all

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "ê":"e", "í":"i", "ó":"o", "õ":"o", "ô":"o", "ú":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "Í":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ú":"U", "Ç":"C"}
return

^b::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename	
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord	
		Newfilename := StrReplace(Newfilename, k, v) 	
	sel.item(A_Index-1).name := Newfilename
}
shellWindows  := ""
return
Yes! It works perfectly! Thank you very much, you saved me dozens of hours of tedious work!
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

27 Nov 2018, 08:37

Great!
Avastgard wrote:
24 Nov 2018, 17:26
as not all the files in the folder will need to be renamed
you know you could automate what gets renamed as well, depending on name/size/time created etc, just give us more details.
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

27 Nov 2018, 09:09

Thank you for being so helpful, but this script, as I see it, is perfect for the task I have to do. Since other people will also have to rename files, I think it's best to leave to their judgment which ones they will apply the changes to (by selecting them), because the criteria by which files will be renamed will be rather subjective.

One thing I should say, especially for other people who may use this code in the future, is that after doing some more testing in its current state, I noticed that it will rename whatever file you have selected, and in case you have more than one file selected in different folders, it will rename whatever file you selected first. So you'll want to make sure you have no other files selected in different folders before using this. To be safe, I'd even close other folders, if that's possible. Took me a while to realize why I couldn't rename a file only to find out later that I had added dozens of prefixes to another file in a different folder that I had selected first.

Again, thanks for the huge help, AlphaBravo!
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

30 Nov 2018, 12:12

Hello there, again.

The script AlphaBravo posted above has been working great.

However, I've come across a situation that I tried to make the script solve, but I haven't been able to. Basically, our database has several files with corrupted names including the � character. It almost always indicates a special character that could not be recognized by the cloud service we used and was replaced by �. Later when somebody downloaded the file, the � stayed there. So I edited the code to make it replace the "�" with "_". But there are some instances in which there are two �� characters, and that almos always means that there were "çã" in its place. Since I want regular letters without marks, I wanted the script to replace double "��" with "ca" but at the same time replace single "�" with "_". Is that possible? I haven't been able to make one work without overriding the other one, as you can see in the script below (look for the �'s to see what I changed from the previous version of the script).

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "è":"e", "ê":"e", "í":"i", "ì":"i", "ó":"o", "õ":"o", "ô":"o", "ò":"o", "ú":"u", "ù":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E", "Ê":"E", "È":"E", "Í":"I", "Ì":"I", "Ó":"O", "Õ":"O", "Ô":"O", "Ò":"O", "Ú":"U", "Ù":"U", "Ç":"C", "��":"ca"}	; THE LAST PAIR OF LETTERS ARE THE ONES I INCLUDED
return

F7::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	filePath := sel.item(A_Index-1).Path
	SplitPath, filePath, filename, filepath
	Newfilename := "Prefix_" filename	;	
	Newfilename := StrReplace(Newfilename, " ", "_")
	Newfilename := StrReplace(Newfilename, "�", "_")	;<========== THIS IS THE LINE I INCLUDED
	for k, v in oWord	
		Newfilename := StrReplace(Newfilename, k, v) 	
	sel.item(A_Index-1).name := Newfilename
}
shellWindows  := ""
return
Also, since I'm already posting, there are several instances of files with prefixes that need to be erased. Would it be possible to make the script erase certain words from the beginning of the file, be it by assigning the word to be erased, be it by specifying a number of characters to be erased? Since adaptabilty is a major concer for me (people who are not programming-savvy at all are using the script), the best scenario is one in which they could just press a hotkey to erase the first character on the file's name, so they could repeat it as necessary without having to edit one by one.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

30 Nov 2018, 19:50

re-arranging your code should do the trick, replace all instances of "��" first then "�" after.
I also modified the code a little bit, no need for "path"

Code: Select all

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "è":"e", "ê":"e"
		, "í":"i", "ì":"i", "ó":"o", "õ":"o", "ô":"o", "ò":"o", "ú":"u"
		, "ù":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E"
		, "Ê":"E", "È":"E", "Í":"I", "Ì":"I", "Ó":"O", "Õ":"O", "Ô":"O"
		, "Ò":"O", "Ú":"U", "Ù":"U", "Ç":"C", "��":"ca"}				; THE LAST PAIR OF LETTERS ARE THE ONES I INCLUDED
return

F7::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	Newfilename := "Prefix_" sel.item(A_Index-1).name
	Newfilename := StrReplace(Newfilename, " ", "_")
	for k, v in oWord	
		Newfilename := StrReplace(Newfilename, k, v) 	
	Newfilename := StrReplace(Newfilename, "�", "_")	;<========== THIS IS THE LINE I INCLUDED
	sel.item(A_Index-1).name := Newfilename
}
shellWindows  := ""
return

as for the second part of your question, a slight modification to the above code should do it

Code: Select all

oPrefix := ["Prefix1", "Prefix2", "Prefix3", "Prefix4"]   ; list prefixes to be removed here.
return

F8::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	Newfilename := sel.item(A_Index-1).name
	for k, v in oPrefix	
		Newfilename := RegExReplace(Newfilename, "i)^" v)
	sel.item(A_Index-1).name := Newfilename
}
shellWindows  := ""
return
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

12 Dec 2018, 21:38

Thank you for being so helpful with this script! It's been working great! Sorry it took me such a long time to reply...

After using the first script (renamer) for a while, I noticed it would be better to separate the features of adding a prefix and the "cleaning" of the text, since many file names already have the correct prefix and would only need to have the spaces and special characters removed.

I tried to adapt the script you posted on your previous message, but since it requires far more knowledge than I have in Autohotkey, I obviously have no idea what I'm doing and don't even know where to start.

I know that at this point I'm abusing your goodwill, but if you could at least give me some insight with which I could work to make the script have the functions I need, I would really appreciate that.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

12 Dec 2018, 21:59

I added a condition to add "Prefix_" only if it doesn't already exists.
abuse!!! :o , no such thing here :)

Code: Select all

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "è":"e", "ê":"e"
		, "í":"i", "ì":"i", "ó":"o", "õ":"o", "ô":"o", "ò":"o", "ú":"u"
		, "ù":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E"
		, "Ê":"E", "È":"E", "Í":"I", "Ì":"I", "Ó":"O", "Õ":"O", "Ô":"O"
		, "Ò":"O", "Ú":"U", "Ù":"U", "Ç":"C", "��":"ca"}
return

F7::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	if !RegExMatch(sel.item(A_Index-1).name, "i)^Prefix_")			; if file name does not begin with "Prefix_", remove "i)" for case sensitive
		Newfilename := "Prefix_" sel.item(A_Index-1).name			; add "Prefix_" to file name 
	Newfilename := StrReplace(Newfilename, " ", "_")				; replace white space with underscore "_"
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 				; replace letter pairs
	Newfilename := StrReplace(Newfilename, "�", "_")				; replace single "�" with "_"
	sel.item(A_Index-1).name := Newfilename							; set new file name
}
shellWindows  := ""
return
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

13 Dec 2018, 21:34

Thank you very much! That's a nice way to prevent multiple prefixes from being appended.
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Script to rename files

11 Jan 2019, 09:36

Hello there!

Sorry to resurrect this thread again, but after a while using this script, I started noticing some inconsistent behavior that I hadn't seen before. I compared the code I am actually using with the one you posted here, but I couldn't spot any differences that could cause the problems I am seeing.

Issue 1)
The problem happens when I try to apply the renamer to a file that already has the exact same Prefix I wish to append. On the last version of the script, you added a check to see if the prefix was already there to prevent it from being appended twice. From what I remember, it was working fine, but I hadn't tried to rename a file that already had the intended prefix for a while until now. When I tried doing it again, I am now receiving an error message saying that there was a problem on line 25 and asking if I want to stop or continue running the script. Here's line 25:

Code: Select all

sel.item(A_Index-1).name := Newfilename
Was that what you intended when you added the check to see if the prefix to be appended was already there? I don't remember seeing this message in the past, but I don't know if I just didn't run into any instances in which the prefix was already appended. In my mind, the script would simply not append the prefix and move on to the next file.


Issue 2)
This one is a little trickier because I can't always reproduce it. There are some cases in which I run the script and, instead of renaming the file as expected, it actually renames the file exactly as another file in the folder, so much so that windows prompts me to name it as "Filename (1)" because there already is another file in the folder with the same name. This is particularly worrying, since it could ruin the database with incorrectly named files if people using the script don't know what's going on. This is further confirmed by the fact that sometimes windows even asks me if I want to change the file's extension.

Here is the actual version I am using. I added some characters to be replaced in oWord and made some comments in the code for my coworkers to remember where they have to edit when changing the prefix, but otherwise the code is unaltered, as far as I can tell:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "è":"e", "ê":"e"
		, "í":"i", "ì":"i", "ó":"o", "õ":"o", "ô":"o", "ò":"o", "ú":"u"
		, "ù":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E"
		, "Ê":"E", "È":"E", "Í":"I", "Ì":"I", "Ó":"O", "Õ":"O", "Ô":"O"
		, "Ò":"O", "Ú":"U", "Ù":"U", "Ç":"C", "��":"ca", "_-_":"_", "__":"_"}
return

F7::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	if !RegExMatch(sel.item(A_Index-1).name, "^Longchamp_")	;<========== INSERIR AQUI PREFIXO (NOME DO CLIENTE SEGUIDO DE UNDERLINE "_")
		Newfilename := "Longchamp_" sel.item(A_Index-1).name	;<========== INSERIR AQUI PREFIXO (NOME DO CLIENTE SEGUIDO DE UNDERLINE "_" 
	Newfilename := StrReplace(Newfilename, " ", "_")				
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 				
	Newfilename := StrReplace(Newfilename, "�", "_")				
	sel.item(A_Index-1).name := Newfilename							
}
shellWindows  := ""
return
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Script to rename files

12 Jan 2019, 02:51

hello again, I see the issue, modified the script a little bit

Code: Select all

oWord := {"à":"a", "á":"a", "ã":"a", "â":"a", "é":"e", "è":"e", "ê":"e"
		, "í":"i", "ì":"i", "ó":"o", "õ":"o", "ô":"o", "ò":"o", "ú":"u"
		, "ù":"u", "ç":"c", "À":"A", "Á":"A", "Ã":"A", "Â":"A", "É":"E"
		, "Ê":"E", "È":"E", "Í":"I", "Ì":"I", "Ó":"O", "Õ":"O", "Ô":"O"
		, "Ò":"O", "Ú":"U", "Ù":"U", "Ç":"C", "��":"ca"}
return

F7::
shellWindows := ComObjCreate("Shell.Application").Windows()
win := shellWindows.Item(0)
sel := win.Document.SelectedItems()
loop % sel.Count
{
	Newfilename := ""												; initialize Newfilename
	if !RegExMatch(sel.item(A_Index-1).name, "i)^Prefix_")			; if file name does not begin with "Prefix_", remove "i)" for case sensitive
		Newfilename := "Prefix_" sel.item(A_Index-1).name			; add "Prefix_" to file name 
	Newfilename := StrReplace(Newfilename, " ", "_")				; replace white space with underscore "_"
	for k, v in oWord
		Newfilename := StrReplace(Newfilename, k, v) 				; replace letter pairs
	Newfilename := StrReplace(Newfilename, "�", "_")				; replace single "�" with "_"
	if NewfileName
		sel.item(A_Index-1).name := Newfilename						; set new file name
}
shellWindows  := ""
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], marypoppins_1, ShatterCoder and 142 guests