Copy filenames w/out path using AHK? (Solved)

Any other tech-related topics
Post Reply
Message
Author
shnbwmn
Posts: 265
Joined: Sat Jul 11, 2015 12:59 am

Copy filenames w/out path using AHK? (Solved)

#1 Post by shnbwmn »

Here's my current AHK script for copying filenames w/ paths (found somewhere online):

Code: Select all

; Copy filenames w/ path
#V:: 		;Win-V to paste text only; formatted version remains in clipboard
OriginalClipboardContents = %ClipBoardAll%
ClipBoard = %ClipBoard%                             ; Convert to text
Send ^v 						
Sleep 250                                           ; Don't change clipboard while it is pasted
ClipBoard := OriginalClipboardContents              ; Restore original clipboard contents
OriginalClipboardContents =                         ; Free memory
Return 	
It works like a charm. When I do Win+V, I get output like this:

Code: Select all

X:\Dir\Subdir\MyFolder\MyFile1.txt
X:\Dir\Subdir\MyFolder\MyFile2.txt
X:\Dir\Subdir\MyFolder\MyFile3.txt
X:\Dir\Subdir\MyFolder\MyFile4.txt
While this is useful, I'm looking for a way to paste only the filenames:

Code: Select all

MyFile1.txt
MyFile2.txt
MyFile3.txt
MyFile4.txt
Is there anyone here knowledgeable in AHK (seen some of you around!) that can work out a solution? All the scripts I've found copy the whole path or return errors. I'm aware that there are clipboard managers and other tools, however it's just so darn convenient to be able to press a hotkey instead of running another program for this simple task.
Last edited by shnbwmn on Thu Jul 14, 2016 9:05 am, edited 1 time in total.

User avatar
lintalist
Posts: 436
Joined: Sat Apr 19, 2014 12:52 am
Contact:

Re: Copy filenames w/out path using AHK?

#2 Post by lintalist »

Try this, winkey-w - because I use StrSplit function you need AutoHotkey v1.1.13+
If you don't have it or can't upgrade comment the strsplit line and uncomment the substr alternative below it. Should work.

Code: Select all

; Copy filenames no-path
#w::       ;Win-v to paste text only; formatted version remains in clipboard
OriginalClipboardContents = %ClipBoardAll%
ClipBoard = %ClipBoard%                             ; Convert to text
SplitPath, % StrSplit(clipboard,"`n").1, , OutDir ; comment this for old ahk
; SplitPath, % SubStr(clipboard,1,InStr(Clipboard,"`n")-1), , OutDir ; and use this for old ahk
StringReplace, clipboard,clipboard,%outdir%\,,All
Send ^v                   
Sleep 250                                           ; Don't change clipboard while it is pasted
ClipBoard := OriginalClipboardContents              ; Restore original clipboard contents
OriginalClipboardContents =                         ; Free memory
Return

shnbwmn
Posts: 265
Joined: Sat Jul 11, 2015 12:59 am

Re: Copy filenames w/out path using AHK?

#3 Post by shnbwmn »

Tried your code and it works! Thank you very much. :D

Marc
Posts: 165
Joined: Sun May 15, 2011 6:06 pm

Re: Copy filenames w/out path using AHK? (Solved)

#4 Post by Marc »

What about using notepad replace function to replace the path "X:\Dir\Subdir\MyFolder\" with nothing ""?
by selecting replace all.

shnbwmn
Posts: 265
Joined: Sat Jul 11, 2015 12:59 am

Re: Copy filenames w/out path using AHK? (Solved)

#5 Post by shnbwmn »

Marc wrote:What about using notepad replace function to replace the path "X:\Dir\Subdir\MyFolder\" with nothing ""?
by selecting replace all.
Indeed, that's what I had to do when I wanted only the filenames, although I just alt-dragged (column selection) over the unneeded parts and deleted. But it's extra work for something that can be solved at the source of the problem (the script).

Post Reply