Jump to content

Is there a non-brute force way to separate files on clipboard?


Recommended Posts

I'm developing a script that will accept one or more files either from the command line or from the clipboard. When the list comes in from the command line, there's no issue because they're not only already split into array elements, they're also all already enclosed in quotes (whether needed or not, I think).

If the clipboard is to be used as the source of the file list, the user first simply selects all the files to be processed and copies the selection to the clipboard. But in this case, none of them are enclosed in quotes -- whereas if they were, that would make it quite easy to separate them. I can eventually manage to code up a brute-force solution using a many string function calls and keeping track of all the many substring positions and lengths involved, but I dread the idea.

I considered StringSplit(), but the list on the clipboard merely separates the files by a space; but you certainly can't use a space as a delimiter. I've eventually been able to construct a few regular expressions of fair complexity, but I haven't begun to try that because I'm not at all sure such a RegExp can be constructed for this situation.

So does anyone know of some less arduous approach to separating a list of files with no quotes and nothing but a space to delimit them? Is there a UDF that might help what you're aware of?

Thanks!

Link to comment
Share on other sites

Well, now I'm pleasantly surprised. I was confused because when I looked at the resulting variable from ClipGet(), there were no quotation marks and were only separated with spaces, as I indicated. But now it appears that the contents are newline separated, which is perfect!

Here's what happened: I selected several files and copied the selection to the clipboard using Explorer's Copy Path(s) button, and examined the return from ClipGet() from within the Graphical Debugger. It certainly appeared that the files were separated only by a space, but after using the debugger's Watch tool to copy the value to the clipboard and then paste in my text editor, only then did the terminating newlines show up (which is simply due to the fact that my editor always shows newlines and the debugger doesn't).

So, kind @mikell, your query led me to discover my mistake and see a trivially simple solution. THANKS!

Edited by Mbee
Link to comment
Share on other sites

1 minute ago, mikell said:

The very first time I provide help so easily. Glad I could do so !  :D

I think you're being somewhat too modest, friend 😉 You've got 950 Likes and this is the first time it's been easy for you? With that record, I suspect few questions stumped you very long!

You have my high regard, and thanks again

Link to comment
Share on other sites

@Mbee

Just in case you were wondering if there was a way to get a list of files that were copied to the clipboard (not copy path), the example below shows how it can be done.  It could easily be modified to work with either a text list (like files/folders copied as paths) or actual files/folders that were copied.

 

#include <Constants.au3>
#include <Clipboard.au3>
#include <Array.au3>
#include <WinAPISysWin.au3>
#include <WinAPIMisc.au3>

#cs
    Below is an example of how to display a list of files/folders that were copied
    to the clipboard, not "copy path" but actually highlighted and copied.
    One benefit of doing it this way is that you can confirm that there
    is a file list in the clipboard before proceeding.  $CF_HDROP will only
    be available when there is a list of files in the clipboard.
#ce

display_copied_file_list()

Func display_copied_file_list()
    Const $tagDROPFILES = _
              "struct;"        & _
              "dword offset;"  & _
              $tagPoint & ";"  & _
              "boolean fNC;"   & _
              "boolean fWide;" & _
              "endstruct;"

    Local $tByteBuffer = "", $tDropFiles = "", $tFileArray = ""
    Local $iFileArrayLength = 0

    ;If clipboard does not contain a list of copied files, then exit with msg
    If Not _ClipBoard_IsFormatAvailable ($CF_HDROP) Then Exit MsgBox($MB_ICONERROR, "ERROR", "No files in clipboard")

    ;Get file list data from clipboard (data is binary)
    $xData = _ClipBoard_GetData($CF_HDROP)

    ;Create a byte buffer structure to hold the data and copy the data to it
    $tByteBuffer = DllStructCreate(StringFormat("byte data[%i]", BinaryLen($xData)))
    $tByteBuffer.data = $xData

    ;Create a DROPFILES structure to access clipboard data
    $tDropFiles       = DllStructCreate($tagDROPFILES, DllStructGetPtr($tByteBuffer))

    ;Create a file list array structure to access the file list array
    $iFileArrayLength = BinaryLen($xData) - DllStructGetSize($tDROPFILES)
    $tFileArray       = DllStructCreate(StringFormat("byte data[%i]", $iFileArrayLength), _
                                        DllStructGetPtr($tDropFiles) + $tDropFiles.offset)

    ;Convert file list array struct to an array and display it
    $aFiles = _WinAPI_StructToArray($tFileArray)
    _ArrayDisplay($aFiles, "List of files copied to clipboard")
EndFunc

 

Edited by TheXman
Typos
Link to comment
Share on other sites

Very nice.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...