topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday October 13, 2024, 5:35 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: AutoHotkey script to replace spaces in file names with underscores  (Read 20325 times)

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
I vaguely remember coming across an autohotkey script on this (?) forum for replacing spaces with underscores in file names. If this rings a bell for anyone, could you point me in the right direction? I wasn't able to find it via search. Maybe it all happened in a dream?   :-\

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,205
    • View Profile
    • Donate to Member
Just maybe this one?  IDEA: Standardize File Name With AHK Dialog Boxes

Does it have to be AutoHotKey?  Most of the free-standing mass file renamers will do this, if it isn't in your favourite file manager's mass renaming feature already.

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
Just maybe this one?  IDEA: Standardize File Name With AHK Dialog Boxes

Thanks for the suggestion. It looks a bit more complicated than what I was looking for.

Does it have to be AutoHotKey?  Most of the free-standing mass file renamers will do this, if it isn't in your favourite file manager's mass renaming feature already.

Yes, I already use AutoHotkey, so that would be the simplest solution.

Upon second thought, this doesn't really need to be a special operation for file names specifically, a generic script would do. All I want to be able to do is copy a document's title and use it as its new file name by using a special paste operation that replaces the spaces in the title (file name) with underscores. But it's fine if it also works in any other context, not only within the save dialog box.

Also, it's not for mass renaming but just fixing individual file names. Basically I'd download articles in PDF formats, copy the titles from within the document, and paste it into the save dialog box with a special command to replace spaces (and perhaps any special disallowed characters that need be, mainly apostrophes) with underscores.

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • View Profile
    • Donate to Member

@dr_andus: i have a script that does that, see if you can use it.

Code: Autohotkey [Select]
  1.  
  2. SetWorkingDir, %A_ScriptDir%
  3.  
  4. ; Select the text and press Alt+v to convert space -> underscore
  5. ;#d::
  6. !v::
  7. Send, ^c
  8. {
  9.     TrayTip,, The attempt to copy text onto the clipboard failed.
  10.     Return
  11. }
  12. StringReplace, Clipboard, Clipboard, %A_SPACE%, _, UseErrorLevel  
  13. If ErrorLevel  
  14. {
  15.   TrayTip,%ErrorLevel%,%Clipboard%,,1
  16. }
  17. Else                                  
  18.   TrayTip,,%ErrorLevel%,,1            
  19. Return

usage:
1. run the script
2. select some text
3. press Alt+v

« Last Edit: May 21, 2014, 11:11 PM by lanux128 »

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
lanux128, many thanks for the script! I'm having a bit of trouble with it though. As I'm using an Apple keyboard, there isn't a Win key. So I changed WIN+D to ALT+V. After that I was able to have one successful instance, but after that it wouldn't do it, even when I reload the script.

This makes me wonder if some bits of this script interfere with some other bits in my file. I don't know anything about AutoHotkey, but for example I already have this elsewhere in the file:

#Persistent  ; Keep the script running until the user exits it.
#SingleInstance force
#MaxHotkeysPerInterval 200

So I'm wondering if that somehow clashes with your

#Persistent
#SingleInstance, Force
#NoEnv

I've got loads of other scripts in the file (even for remapping my Apple keyboard's keys).

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • View Profile
    • Donate to Member
lanux128, many thanks for the script! I'm having a bit of trouble with it though. As I'm using an Apple keyboard, there isn't a Win key. So I changed WIN+D to ALT+V. After that I was able to have one successful instance, but after that it wouldn't do it, even when I reload the script.

This makes me wonder if some bits of this script interfere with some other bits in my file. I don't know anything about AutoHotkey, but for example I already have this elsewhere in the file:

are you running the script by itself or you copy-pasted in to your main script? i have added more error-trapping in case copying to clipboard somehow fails so please check out the new script from the above link.

btw, what OS are you running this on? it works fine here on Win 7 Pro (x64).

NB: currently there are a few AHK forks. fyi i am using this one, previously known as AHK_L.

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
i have added more error-trapping in case copying to clipboard somehow fails so please check out the new script from the above link.

lanux128, thank you for your efforts.

are you running the script by itself or you copy-pasted in to your main script?

I tried it both ways. Unfortunately even after restarting the system I wasn't able to deploy the script at all. There must be some conflict with some of my other scripts, I presume.

btw, what OS are you running this on? it works fine here on Win 7 Pro (x64).

NB: currently there are a few AHK forks. fyi i am using this one, previously known as AHK_L.

I'm also using Win7 (x64) and AHK_L.

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
I actually managed to run your script once, after I suspended all my other scripts. However, when I re-enabled some of them, it stopped running. And when I disabled them again, your script still wouldn't run again. So there is something that only allows it to run once, even if the interfering scripts are disabled again.

I thought I managed to narrow it down to this script as the interfering one, but I'm not entirely sure:

;-----------------------
; ALT+W to count words  |
;-----------------------

!W:: ; ALT + W to activate script
ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable of your choice.
Clipboard := 
Send ^c
; Send {Left}
ClipWait, 2
StringReplace, clipboard, clipboard, ', x, All
ClipWait, 2
StringReplace, clipboard, clipboard, -, x, All
RegExReplace( Clipboard, "\w+", "", Count ) ; PhiLho
Clipboard := ClipSaved

; To have a ToolTip disappear after a certain amount of time
; without having to use Sleep (which stops the current thread):
#Persistent
ToolTip, Word Count: %Count%
SetTimer, RemoveToolTip, 5000
return

RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
Return

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,205
    • View Profile
    • Donate to Member
Do you use a clipboard manager like ArsClip or CHS?  If so, you could use their search-and-replace feature to do this.  Or, try Clippy or Text Monkey, which I mentioned in a previous thread: Explote a clip from the clipboard

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
Do you use a clipboard manager like ArsClip or CHS?  If so, you could use their search-and-replace feature to do this.  Or, try Clippy or Text Monkey, which I mentioned in a previous thread: Explote a clip from the clipboard

Thanks for the suggestion. However I'd really prefer to use AutoHotkey for this, so that the whole operation can be carried out by a series of keyboard shortcuts in one place, rather than having to switch to another software window and then back again.

lanux128's script really does what I want, it's just that I'm running into a conflict with some of my other AutoHotkey scripts somehow...

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,205
    • View Profile
    • Donate to Member
I'd really prefer to use AutoHotkey for this, so that the whole operation can be carried out by a series of keyboard shortcuts in one place, rather than having to switch to another software window and then back again.
You don't need to switch windows with (at least) Clippy or ArsClip.  You copy the text to the clipboard in the usual way, press a hotkey you've already set for that particular action, and press Ctrl+V to paste the modified text back again.  However, it's more a low-drag solution from my point of view as I routinely run ArsClip anyway.  Before it sprouted the search-and-replace feature I used Clippy, which has a wide range of other text munging features.
« Last Edit: May 22, 2014, 04:34 PM by rjbull, Reason: added \"switch windows.\" »

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • View Profile
    • Donate to Member
@dr_andus: i cannot replicate the problem you face. i too run several AHK scripts (5 raw, 4 compiled) with a couple of clipboard extenders but the script i posted had no problems.

what happens if you copy-paste my script in to the Alt+W one? do you get a tray balloon like in the image below?

Spoiler
AutoHotkey_script_to_replace_spaces_in_file_names_with_underscores_2014_05_23_001.png



at this point, i suspect it has something to do with Win7's UAC which suppresses the script silently, instead of elevating the process.

i wonder if someone could try the script and post some feedback. @rjbull, do you fancy? :)
« Last Edit: May 23, 2014, 01:46 AM by lanux128 »

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,963
    • View Profile
    • Donate to Member
I had a quick go, got an error message showing file paths with underscores added - but there is no change to focused files.
No change if I also run the Count_words script.
I see that these paths are also copied to the clipboard.

Screenshot - 2014-05-23 , 10_03_33.png

Win7 x64 / UAC on highest setting / AHK_L
Tom

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
Thanks for all your help. I have UAC switched off. I've never seen that tray balloon pop up, but then I only managed to execute the script twice so far, and after both instances things got messed up.

I'm starting to think that the problem may have to do with some conflict with my Apple keyboard remapping. For example, I get different behaviour with clipboard related shortcuts depending on the types of shortcut keys I use. Also, when I tried to set up a hotkey for the ArsClip macro and hit Command, which is mapped as Win, it showed up as Win+CTRL. So I suspect a lot more is happening when I press certain keys than I'm able to see.

Anyway, here is my keyboard remapping script, in case anyone might be able to spot the culprit in there:

;---------------------------------
; Apple Keyboard remapping       |
;---------------------------------

; Function Keys

+F3::Send {PrintScreen}

+F7:
IfWinExist, ahk_class iTunes
ControlSend, ahk_parent, ^{LEFT} ; < previous
return

+F8::
IfWinExist, ahk_class iTunes
ControlSend, ahk_parent, {SPACE} ; play/pause toggle
return

+F9::
IfWinExist, ahk_class iTunes
ControlSend, ahk_parent, ^{RIGHT} ; > next
return

+F10::Send {Volume_Mute}
+F11::Send {Volume_Down 3}
+F12::Send {Volume_Up}

#Left::SendInput {Home}
#Right::SendInput {End}
#+Left::sendInput +{Home}
#+Right::SendInput +{End}
!Left::SendInput ^{Left}
!Right::SendInput ^{Right}
!+Left::SendInput ^+{Left}
!+Right::SendInput ^+{Right}

^Backspace::SendInput {Del}

; copy, paste, save, select all etc
#c::^c
#x::^x
#v::^v

#s::^s
#a::^a
#z::^z
#b::^b
#i::^i

; Keyboard Characters

^3::Send {Raw}#
^2::Send {Raw}@
^'::Send {Raw}"

I also have this at the top of my main AutoHotkey script (I don't know what it does. I inherited it from someone):

SetTimer,UPDATEDSCRIPT,1000
UPDATEDSCRIPT:
FileGetAttrib,attribs,%A_ScriptFullPath%
IfInString,attribs,A
{
FileSetAttrib,-A,%A_ScriptFullPath%
SplashTextOn,,,Updated script,
Sleep,500
Reload
}
return

#Persistent  ; Keep the script running until the user exits it.
#SingleInstance force
#MaxHotkeysPerInterval 200

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
As for ArsClip, I'm not sure what I'm doing wrong, but I tried to set up a macro for replacing spaces with underscores, but the keyboard shortcuts would not trigger most of the time, and if some combinations did, it would just replace the copied text with the macro text: " REPLACE="_"]

arsclip.png

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • View Profile
    • Donate to Member
I had a quick go, got an error message showing file paths with underscores added - but there is no change to focused files.
No change if I also run the Count_words script.
I see that these paths are also copied to the clipboard.
 (see attachment in previous post)
Win7 x64 / UAC on highest setting / AHK_L
@tomos: thanks for testing out. :-* don't worry, the changed text is only stored in clipboard. in any case, you can press Ctrl+v to paste the new contents.

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • View Profile
    • Donate to Member
Thanks for all your help. I have UAC switched off. I've never seen that tray balloon pop up, but then I only managed to execute the script twice so far, and after both instances things got messed up.

I'm starting to think that the problem may have to do with some conflict with my Apple keyboard remapping. For example, I get different behaviour with clipboard related shortcuts depending on the types of shortcut keys I use. Also, when I tried to set up a hotkey for the ArsClip macro and hit Command, which is mapped as Win, it showed up as Win+CTRL. So I suspect a lot more is happening when I press certain keys than I'm able to see.

since the script worked on tomos' PC, we can isolate the Apple keyboard and see how AHK recognises the key presses. right-click on a running script and select 'Open'. from the window that appears, go to View|Hotkey and their methods. now you can see how your hotkeys are registered. it should look like this then post it here.

http://img42.imageshack.us/img42/8509/p6wt.gif
AutoHotkey script to replace spaces in file names with underscores



Anyway, here is my keyboard remapping script, in case anyone might be able to spot the culprit in there:

i don't think there is anything wrong here.

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,963
    • View Profile
    • Donate to Member
I had a quick go, got an error message showing file paths with underscores added - but there is no change to focused files.
No change if I also run the Count_words script.
I see that these paths are also copied to the clipboard.
 (see attachment in previous post)
Win7 x64 / UAC on highest setting / AHK_L
@tomos: thanks for testing out. :-* don't worry, the changed text is only stored in clipboard. in any case, you can press Ctrl+v to paste the new contents.

ah okay - I misunderstood - I thought it was intended to actually rename the files.
Tom

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
Thanks, lanux128, here it is (though it doesn't say much to me  :)

AutoHotkey.png


dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
In the meantime a kind soul who had seen the above had emailed me a script that does work within my particular setup, so my problem is solved. Thank you very much for all your help.

Here is the script, in case anyone else runs into this problem:

;----------------------------------------------
; CTRL+SHIFT+Z Replace spaces with underscores |
;----------------------------------------------

^+z::
clipboard=
Send, ^x
StringReplace, clipboard, clipboard, %A_Space%, _, All
Send, ^v
Return

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,205
    • View Profile
    • Donate to Member
Just for the record, and knowing you now have what you want, here's my ArsClip clip for that, using ArsClip 4.16 build 5250-62227; that's the latest beta T18 dated May-18-14:
AC.png

dr_andus

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 851
    • View Profile
    • Dr Andus's toolbox
    • Donate to Member
Thanks rjbull. I was able to run it in the clip preview window, but for some reason I'm not able to assign a global hotkey to it (perhaps due to my particular keyboard mapping). It just gives me an access violation error, whenever I try to use a global hotkey to trigger it. But never mind, the AutoHotkey script does the trick, I was just curious to see how it would work in ArsClip.