PowerShell: cm_CopyURINamesToClip.ps1 URI URL encode

English support forum

Moderators: white, Hacker, petermad, Stefan2

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

PowerShell: cm_CopyURINamesToClip.ps1 URI URL encode

Post by *Stefan2 »

Based on a request

to URL encode selected file and paste to clipboard.

Also prefix the string by "smb:"

Gauner wrote: Ich möchte mit List2Clip gern den URI-Identifier der markierten Dateien erzeugen, mit vorangestelltem smb: und URL-kodierten Zeichen.

Habe jetzt folgende Zeile gemacht:
%COMMANDER_PATH%\plugins\Lst2Clip.exe /U /P:"smb:" /R:""="/" /R:"+"="%2B" /R:" "="+" /R:"?"="%3F" /R:"="="%3D" /R:"%"="%%" /R:"&"="%26"
/R:"Ä"="%C3%84" /R:"Ö"="%C3%96" /R:"Ü"="%C3%9C" /R:"ß"="%C3%9F" /R:"ä"="%C3%A4" /R:"ö"="%C3%B6" /R:"ü"="%C3%BC" /R:"ẞ"="%E1%BA%9E"

Here is an solution utilizing PowerShell.

Please try and report back how it works for you.


Save the code to TC folder as file named f.ex.: cm_CopyURINamesToClip.ps1


-

Create an button or user defined command (usercmd.ini)

Command = PowerShell -NoProfile -NoExit -ExecutionPolicy Bypass
or just
Command = PowerShell

Parameter = "D:\rive\path\to\cm_CopyURINamesToClip.ps1" """%P%N"""
or more portable
Parameter = "%COMMANDER_PATH%\CMDs\cm_CopyURINamesToClip.ps1" """%P%N"""
(Yes, three double-quotes before and after %P%N !!!)

-

Select a file, click the button, and paste URI string where you want.

EXAMPLES:

Code: Select all

            C:\ToCo9\plugins\lst2clip\liesmich-lst2clip_1_02.htm.TXT
C%3A%5CToCo9%5Cplugins%5Clst2clip%5Cliesmich-lst2clip_1_02.htm.TXT

                                    X:\In_Arbeit\OceanViewer_1.2.3\Workinstruction.TXT
%5C%5CShare.Company.World.net%5CWork%5CIn_Arbeit%5OceanViewer_1.2.3%5CWorkinstruction.TXT
-


The PoSh code to save:

cm_CopyURINamesToClip.ps1

Code: Select all

# cm_CopyURINamesToClip.ps1

# Stefan, 2017.06.16a
# Found at: https://ghisler.ch/board/viewtopic.php?p=329427#329427

# Total Commander Button code:
# Command : PowerShell -NoProfile -NoExit -ExecutionPolicy Bypass
# Argument:             "D:\rive\path\to\cm_CopyURINamesToClip.ps1" """%P%N"""
# Argument: "%COMMANDER_PATH%\TOOLs\CMDs\cm_CopyURINamesToClip.ps1" """%P%N"""
# (Yes, three double-quotes before and after %P%N !!!)

$Prefix = "smb:"
$Suffix = ""


$curITEM = $args[0]    # due to TC's """%P%N""" ==> current selected item in TC
$currDRV = Split-Path -qualifier $curITEM
$locDISK = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$currDRV'"
$outPATH = $curITEM.Replace($currDRV, $locDISK.ProviderName)
If ($outPATH[1] -ne '\'){$outPATH = $curITEM} # on error use input as output


# To replace " " with %20 and / with %2F and so on, converts all odd characters in the supplied string:
$outPATH = [uri]::EscapeDataString($outPATH)

# If you want to do escaping for URLs only, This will leave, e.g., slashes (/) or equal signs (=) as they are.
#$outPATH = [uri]::EscapeUriString($outPATH)


$outPATH  = $Prefix + $outPATH + $Suffix


#Set to clipboard:
$outPATH | clip.exe



HTH? :D

- - -

Find me:
cm_Copy em_Copy Copy Name To at Clip Clipboard
cm_Kopie em_Kopie Kopie Name ins in das zum Clip Clipboard
Last edited by Stefan2 on 2017-06-18, 11:32 UTC, edited 2 times in total.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

PowerShell: cm_CopyBASENamesToClip.ps1 w/o Extension

Post by *Stefan2 »

There are some build-in commands also:

cm_CopyFullNamesToClip
cm_CopyNamesToClip
cm_CopyNetNamesToClip
cm_CopySrcPathToClip
cm_CopyToClipboard
cm_CopyTrgPathToClip

Often missed are commands to copy without the extension.

Here is a PoSh code to accomplish that:

cm_CopyBASENamesToClip.ps1 (script file to call from a TC-button, read the instruction below)

Code: Select all

# cm_CopyBASENamesToClip.ps1
# Command to copy file BASE name without the extension to clipboard.

# Stefan, 2017.06.16b (b= changed GCI to GI
# Found at: https://ghisler.ch/board/viewtopic.php?p=329427#329427

# Total Commander Button code:
#
# Command : PowerShell -NoProfile -NoExit -ExecutionPolicy Bypass
# or just
# Command : PowerShell 
#
#
# Parameters:                             "D:\rive\path\to\cm_CopyBASENamesToClip.ps1" """%L"""
# or more portable
# Parameters: "%COMMANDER_PATH%\TOOLs\CMDs\cm_CopyBASENamesToClip.ps1" """%L"""
#
#
# (Yes, three double-quotes before and after %P%N !!! for the TC-parser, or use one single qoute like '%L')
# Use TCs %L-parameter to get "D:\rive\path\the File name" -or- %F to get "the File name" only.

# ### ### ### ### ### ### ###

# ##The code

# ###Collection of all selected items, due to TC's """%L"""
$selItems = $args[0]   


# ## Remove extensions from file and folder names:
$out = Get-Content $selItems | ForEach{ If($_.IndexOf(".") -gt 0){$_.SubString(0,$_.LastIndexOf("."))}else{$_}}


# ### The same, but do not remove "extension" on folder name (this is ONE long line):
#$out=Get-Content $selItems | ForEach{If((GI $_).PSIsContainer){$_}else{  
 If($_.IndexOf('.') -gt 0){$_.SubString(0,$_.LastIndexOf('.'))}else{$_}}}|clip




# ## Bonus run: Remove trailing backslash from folder names:
#$out = $out | ForEach{If($_.LastIndexOf("\")+1 -eq $_.Length){$_.SubString(0, $_.Length-1)}else{$_}}





#Set to clipboard:
$out| clip.exe

Remove '-NoExit' to let the "DOS-Box" close after run.




- - -



The same code without comments to just use in the Parameter box of an button:


Use TCs %L-parameter to get "D:\rive\path\the File name" -or- %F to get "the File name" only.



# ### You could also use this 'all in one button'-code:
# Command : PowerShell
# Parameters: GC '%L'|%%{If($_.IndexOf('.') -gt 0){$_.SubString(0,$_.LastIndexOf('.'))}else{$_}}|clip



# ### The same, but do not remove "extension" on folder name (this is ONE long line):
# Command : PowerShell
# Parameters: GC '%L'|%%{If((GI $_).PSIsContainer){$_}else{If($_.IndexOf('.') -gt 0){$_.SubString(0,$_.LastIndexOf('.'))}else{$_}}}|clip





HTH? :D
Last edited by Stefan2 on 2017-09-24, 11:03 UTC, edited 2 times in total.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

VBScript: cm_CopyNetIPToClip CopyWithIPAddress mapped drive

Post by *Stefan2 »

viewtopic.php?p=305334#305334
Stefan2 wrote:
gogis wrote:Was ich mir wünsche ist ein Button,
welcher mir alle selektierten Dateien/Verzeichnisse
mit vollständigem Netzwerkpfad (mit IP-Adresse vorneweg),
in die Zwischenablage übernimmt.

Zum Beispiel so.


Purpose:
Copy selected name to clipboard, change "Drive letter:" to "IP address\share"



Example:
If "\\10.11.12.13\share_SW" is mapped as drive W:
cm_CopyFullNamesToClip: W:\TOOLs\Notepad2\Notepad2.exe
cm_CopyNetNamesToClip: \\server.name.tld\share_SW\TOOLs\Notepad2\Notepad2.exe
cm_CopyNetIPAddrToClip: \\10.11.12.13\share_SW\TOOLs\Notepad2\Notepad2.exe <<<< Script result

Changed "W:" to "\\10.11.12.13\share_SW"


TC Button:
CMD: "D:\rive\path\to\this\script.vbs"
PARAM: "%L"
USAGE: Select your files, execute this script... done.



Code: Select all

'// ===========================================================
'// VBScript for Total Commander, by Stefan
'// cm_CopyNetIPToClip.VBS, v00.1, 2016-02-19
'// Forum: http://ghisler.ch/board/viewtopic.php?t=20044 (IP-Adresse anstatt Rechnername in Netzwerkumgebung)
'// Purpose: Copy selected name to clipboard, change "Drive letter:" to "IP address\share"
'//     Example: If "\\10.11.12.13\share_SW" is mapped as drive W:
'//         cm_CopyFullNamesToClip:  W:\TOOLs\Notepad2\Notepad2.exe
'//         cm_CopyNetNamesToClip:   \\server.name.tld\share_SW\TOOLs\Notepad2\Notepad2.exe
'//         Script result: 
'//         cm_CopyNetIPToClip:      \\10.11.12.13\share_SW\TOOLs\Notepad2\Notepad2.exe
'//
'// TC Button:
'//         CMD:   "D:\rive\path\to\this\script.vbs"
'//         PARAM: "%L"
'// USAGE: Select your files, execute this script... done.
'// ===========================================================
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSO = CreateObject("WScript.Shell")
Set NET = CreateObject("WScript.Network")
'// =========================================================== Read TCs temp list:
sTCtempList = Wscript.arguments.Item(0) ' The TC temp file due the "%L"
If  FSO.FileExists(sTCtempList) Then
    Set oTextStream = FSO.OpenTextFile(sTCtempList,1) 'FORREADING = 1
    sFileContent = oTextStream.ReadAll
    oTextStream.Close
    aLineArray = split(sFileContent,vbCRLF)
    '// =================== Get first line to do basic things one time only:
    sFirstLine = aLineArray(0)
    If(InStr(sFirstLine,":")) Then sDriveLetter = Left(sFirstLine,2)
    sShareName  = GetUNCName(sDriveLetter)
    sServerName = Split(sShareName, "")(2) 
    sIPAddress  = GetServerIP(sServerName)
    sShareWithIP = replace(sShareName, sServerName, sIPAddress)
    '// =================== For each line in TCs temp list, Do your work:
    For i=0 To  UBound(aLineArray) -1
        sFullName  = aLineArray(i)
        'sTemp = sTemp & sFullName & vbCRLF ' test output original string
        sOUT = sOUT & replace(sFullName,sDriveLetter,sShareWithIP) & vbCRLF
    Next
Else
    MsgBox "Input file  sTCtempList  not found."
End If
'// =========================================================== Output the result:
sOUT = Left(sOUT, Len(sOUT)-2) '//removing trailing line break:
' SetClipboard sTemp & vbCR & sOUT ' test output
SetClipboard sOUT
'// =========================================================== HELPER FUNCTIONs
Function GetUNCName(sDriveLetter)
    On Error Resume Next
    Set cDriveColl = NET.EnumNetworkDrives()
    GetUNCName = sDriveLetter 
    For iDrive = 0 To cDriveColl.Count - 1 Step 2
        If cDriveColl.Item(iDrive) = sDriveLetter Then 
           GetUNCName = cDriveColl.Item(iDrive+1) 
         End If
    Next
End Function
Function GetServerIP(sServerName)
    On Error Resume Next
	Set oExec = WSO.Exec("ping -n 1 " & sServerName)
	sStdOutRes = oExec.StdOut.ReadLine ' commands' first line is blank 
	sStdOutRes = oExec.StdOut.ReadLine
	If(sStdOutRes<>"") Then
        iPosS = InStr(sStdOutRes,"[")
        iPosE = InStr(sStdOutRes,"]")
        GetServerIP = mid(sStdOutRes,iPosS+1, iPosE-iPosS -1)
        If(GetServerIP="") Then GetServerIP = "Error-2"
    Else
        GetServerIP = "Error-1"
    End If
End Function
Function SetClipboard(stringToClipboard)
    On Error Resume Next
    Set oIE = CreateObject("InternetExplorer.Application")
    oIE.Navigate("about:blank")
    oIE.document.parentwindow.clipboardData.SetData "text", stringToClipboard
    oIE.Quit
End Function
'// =========================================================== Results examples:
'// RESULTs:
'// RESULT of sTemp:
'// W:\TOOLs\Notepad2\Notepad2.exe
'// W:\TOOLs\Notepad2\Notepad2.ini
'// RESULT of sOUT:
'// \\10.11.12.13\share_SW\TOOLs\Notepad2\Notepad2.exe
'// \\10.11.12.13\share_SW\TOOLs\Notepad2\Notepad2.ini
'// =========================================================== END



HTH? :D
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

PowerShell: cm_CopyModifiedNameToClip

Post by *Stefan2 »

PowerShell: cm_CopyModifiedNameToClip

Modify names of selected files and copy result to clipboard.

- - -

File with parent folder only:

FROM:
C:\folder\folder1\file.ext

TO:
\folder1\file.ext

USE TC Button Setting:
CMD : PowerShell
PARA: TYPE '%L' | Foreach{$_ -Replace('^.+(\\.+?\\.+)$','$1')} |clip

- - -

Change "\" to "/":

FROM:
C:\folder\folder1\file.ext

TO:
C:/folder/folder1/file.ext

USE TC Button Setting:
CMD : PowerShell
PARA: TYPE '%L' | Foreach{$_ -Replace('\\','/')} | clip

-or-
TYPE '%L' | Foreach{$_ -Replace [regex]::escape('\'), '/'} | clip

- - -

A way to copy a file's (file:///...) path to the clipboard?

This is an example of the format I'm looking for: file:///c:/WINDOWS/WindowsUpdate.log


FROM:
c:\WINDOWS\WindowsUpdate.log
TO:
file:///c:/WINDOWS/WindowsUpdate.log

USE TC Button Setting:
CMD : PowerShell
PARA: TYPE '%L' | Foreach{"""file:///""" + $_ -Replace('\\','/')} | clip

-or-
PARA: TYPE '%L' | Foreach{'file:///' + $_ -Replace('\\','/')} | clip

- - -

Remove last sign, remove trailing backslash

FROM:
C:\folder\folder1\file.ext
C:\folder\folder2\

TO:
C:/folder/folder1/file.ext
C:\folder\folder2

USE TC Button Setting:
CMD: powershell
PARA: TYPE "%L" | Foreach{ $_ -Replace '\\$' }|clip


- - -


- - -


- - -


- - -
.

HTH?
Last edited by Stefan2 on 2017-06-18, 11:26 UTC, edited 1 time in total.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

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

Post by *Stefan2 »

-reserved-
Post Reply