Using clipboard to paste text as file name: replace newline by space

Here you can propose new features, make suggestions etc.

Moderators: white, Hacker, petermad, Stefan2

User avatar
Stefan2
Power Member
Power Member
Posts: 4159
Joined: 2007-09-13, 22:20 UTC
Location: Europa

PowerShell: clipboard paste text as file name: replace newline by space

Post by *Stefan2 »

funkymonk wrote: 2023-07-25, 00:17 UTC When using the content of the clipboard as a file name
(especially during in-place rename (Shift+F6)
or in dialogs where file names can be entered),
TC truncates the content of the clipboard at the first newline char.
I would prefer to replace newlines by a spaces.


In the meantime we can use such a button (or usercmd called from a keyboard shortcut)


FROM text in clipboard:
When using the "content" of the clipboard as a file name
      (especially C:\> during? in-place rename (Shift+F6)

or in dialogs where file names can be entered),
TC truncates the content of the clipboard at the first newline char.

I would prefer to replace newlines by a spaces.



TO single line in clipboard:
When using the content of the clipboard as a file name (especially C during in-place rename (Shift+F



USE:
One could use this powershell one-liner (one long line):
$C=((Get-Clipboard) -replace('[:\\/><?|"]','') -join ' ')-replace('\s+',' ');if($C.length -gt 100){Set-Clipboard ($C.substring(0,100))}else{Set-Clipboard $C}



Copy&Paste buttoncode:
8<------------------->8------------------------>8
TOTALCMD#BAR#DATA
powershell -windowstyle hidden
$C=((Get-Clipboard) -replace('[:\\/><?|"]','') -join ' ')-replace('\s+',' ');if($C.length -gt 100){Set-Clipboard ($C.substring(0,100))}else{Set-Clipboard $C}
wcmicons.dll,67
PoSh: clean-up the clipboard

1
-1
8<------------------->8------------------------>8



This needs PoSh v5 or newer, or a own/other clipboard handling commandlet
PS C:\Temp> host
Name : ConsoleHost
Version : 5.1.22000.2003




hope this works for others too...






Explanations:
$C=((Get-Clipboard) >>>> get clipboard content and store in variable $C
-replace('[:\\/><?|"]','') >>>> replace signs not valid in a file name by nothing (or to underscore if wanted: ,'_' )
-join ' ') >>>> join the single lines by an space (removing the line break signs \r\n)
-replace('\s+',' '); >>>> reduce multiple spaces to one
if($C.length -gt 100){Set-Clipboard ($C.substring(0,100))} >>>> shorten to max wanted return length = 100 signs
else{Set-Clipboard $C} >>>> if shorter anyway, just use that




Add a keyboard shortcut to an command:

usercmd.ini
[em_MyCommandName]
cmd=powershell -windowstyle hidden
param=$C=((Get-Clip . . . . . . . . Set-Clipboard $C}
iconic=1


To assign an keyboard shortcut key (hotkey) to an command, like CTRL+SHIFT+V, utilize
Configuration > Options > Misc. >> Redefine hotkeys (Keyboard remapping)

More about this all at >>> https://ghisler.ch/board/viewtopic.php?p=344777#p344777







Update:
need to write the wanted max length only once by using the $M var:
USE (one long line):
$C=((Get-Clipboard) -replace('[:\\/><?|"]','') -join ' ')-replace('\s+',' ');$M=100;if($C.length -gt $M){Set-Clipboard ($C.substring(0,$M))}else{Set-Clipboard $C}

$M=100 >>>> set max wanted length to return to 100 signs



Alternative , also add extension:
TO:
When using the content of the clipboard as a file name (especially C during in-place rename (Shift+F.txt
USE (one long line):
$C=((Get-Clipboard) -replace('[:\\/><?|"]','') -join ' ')-replace('\s+',' ');$M=100;if($C.length -gt $M){$R=($C.substring(0,$M))}else{$R=$C};$R=($R -replace('(\.+$|\s+$)'))+'.txt';Set-Clipboard $R


Once again adjusted in detail:
$C=((Get-Clipboard) -replace('[:\\/><?|"]','') -join ' ')-replace('\s+',' ');$M=100;if($C.length -gt $M){$R=($C.substring(0,$M))}else{$R=$C};$R=($R -replace('([.\s]+$)'))+'.txt';Set-Clipboard $R




 
User avatar
petermad
Power Member
Power Member
Posts: 14809
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Using clipboard to paste text as file name: replace newline by space

Post by *petermad »

2Fla$her
Fla$her wrote:Det er ikke at trykke på genvejstasten, der håndteres, men indsætte tekst.
It is not pressing the hotkey that is handled, but inserting text.
It was not the "but" in that sentence (which is perfectly understandable English) I was referring to, it was the "but" in your first posting in this topic that was ambiguous to me:
Fla$her wrote:modern browsers in the address bar don't handle Ctrl+V, but inserting text into the field, which can be done through the context menu with the "Paste" item.
It is like writing: "The milkman doesn't handle the milk bottles, but spilling milk on the street" - it does not make sense - to me.


BTW - correct Danish translation would be; "Det er ikke trykket på genvejstasten, der håndteres, men indsættelsen af tekst"
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Post Reply