Programmatically save files om Remote Desktop copied on local machine into clipboard?

Magnus_Bleken
Magnus_Bleken used Ask the Experts™
on
How can I programmatically save files to server-disk when Connected to Remote Desktop when the files are on local machine copied into the clipboard?

I don't mean "open Explorer and hit CTRL+V" or use \\tsclient\c....
When doing this locally I use OpenClipBoard, GetClipboardData and DragQueryFile... but this doesn't work when using it on Win10/2008R2 Remote desktop "server" when files are copied on local disk onto clipboard..

Any suggestions?
Last resort is finding a way to open Windows Explorer in a new directory and hitting CTRL+V/using API to paste it.. I'll take suggestions here too :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
As long as within your Remote Desktop connection - on the Local Resources tab and within the More option you have the Check on the C:   (meaning Share Drives) you can save any file to the local workstation using the "tsclient" as the pseudo-UNC.

For example:  
 \\tsclient\C\Temp 

Open in new window

    points to the local workstation's C drive and its TEMP subdirectory.

WIth that in mind your VFP application can do something like:  
COPY FILE FileOnRDPWorkstation.xxx TO "\\tsclient\C\Temp\FileOnLocalWorkstation.xxx" 

Open in new window



Good Luck

Author

Commented:
Thanks for Your reply, jrbbldr but that is not what I need.
I cannot use normal file copy (\\tsclient or another UNC). Nor can I use FTP or any other conventional file copy like robocopy/xcopy.
The user is connected to his local computer and puts files in the clipboard (selecting them and hitting CTRL+C).
Then he goes over to his Remote Desktop and that is where I need them.. programmatically.
If the user opens a folder and hits CTRL+V then the files are pastes there.. But I need to do this in code so it Works seemlessly in my Application :)

Commented:
OK, here is what I have found....

When you do a COPY of one or more filenames from a Windows Directory, the files themselves are not copied to the Clipboard (maybe you already knew that).

I just did a trial and found that after doing that, the contents of the Clipboard was a list of the fully pathed filenames that were 'Copied'  (NOTE:  I used the FreeClipboardViewer 3.0 tool   -   http://www.freeclipboardviewer.com/  )
For example:
C:\Temp\Oakley File-1.dat
C:\Temp\Oakley File-2.dat

Open in new window


So your VFP code would need to Read the Clipboard contents and acquire the list of files.
( I have yet to find that answer, but I will continue looking and perhaps someone else will have that part of the puzzle. )

Then, using that list, do as I have suggested above with the:
COPY FILE FileOnRDPWorkstation.xxx TO "\\tsclient\C\Temp\FileOnLocalWorkstation.xxx" 

Open in new window


Good Luck
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

So it seems you need to programatically control the RDS session... Similar as if somebody else could control your regular Windows session... Do you think it is secure?

jrbbldr showed one way which requires to access shared folder on the remote computer.

Of course, you can do it opposite way - make your local drives visible in RDS session and then you may simply copy whatever you need by running program on the remote computer. More info: https://www.raymond.cc/blog/transfering-files-from-local-computer-to-connected-remote-desktop-session/

Author

Commented:
Thanks both of you.
jrbbldr: this could work fine when files is copied from local or mapped volume, mot when copied from UNC.. But I will try this :)

pcelba: I don't want to Control the RDS session, I only want to open an Explorer-Window and hit CTRL+V With .visible=FALSE in the background.. programmatically... When user is running their app using RemoteApp then they won't be able to start other programs like Explorer to browse your local disks :)

This is a very specific routine I want to make :)

Commented:
One question...

I assume that the user on the Remote Desktop will be doing the File Copy to Clipboard directly in Windows and outside of any VFP application.

How will your VFP application 'know' when this is done and thereby will attempt to copy the files to the local workstation?

Good Luck

Commented:
Well I have found the following:

Retrieve List of Files from Clipboard
https://www.berezniker.com/content/pages/visual-foxpro/retrieve-list-files-clipboard

I have not tried it, so Good Luck with it.
But the question I asked above still stands - How will the VFP application 'know' when there is a list of files to Copy/Move?

If the user has to do some action to 'tell' the VFP application to initiate the Copy/Move, then why allow them to do it 'outside' of the application at all.  
They would be better to select the list of files within the application and the whole job would be easier.

Good Luck
To open an Explorer-Window and hit CTRL+V in RDS session from local computer is nothing else than RDS session control. Any program having System privileges can do it. If you know how to propagate physical key press into RDS session window then you have the solution. It will just need elevated rights. Everything must be done at much lower level than OpenClipBoard, GetClipboardData and DragQueryFile etc.  But I would do it different way.

RDS session can see your local discs or folders (and printers) when you connect so copy your files to some local folder which is visible for RDS and another program running in RDS can do the rest.

Author

Commented:
I've requested that this question be deleted for the following reason:

Made a workaround and nobody obviously understood my question
You were asking for "Any suggestions"... I would like to know what is the "workaround" in such case?
Perhaps post your workaround ?
That will Have two benefits -
- We might better understand your question
- Others who have trouble transferring files from/to RDS (eg me), might be interested in the solution
Workaroud :)
LOCAL loShell, loFSO, loNS
  TRY
    lcMappe = gcTemp + 'MAB_TempPaste'
    IF DIRECTORY((lcMappe), 1)
      loFSO = CREATEOBJECT("Scripting.FileSystemObject")
      loFSO.GetFolder(lcMappe).DELETE(.T.)
      RELEASE loFSO
    ENDIF

    IF !DIRECTORY((lcMappe), 1)
      MKDIR (lcMappe)
    ENDIF

    loShell = CREATEOBJECT("Shell.Application")
    loNS = loShell.NameSpace(lcMappe)
    FOR lnCount = 1 TO loNS.SELF.Verbs.COUNT
      WITH loNS.SELF.Verbs.ITEM(lnCount)
        lcVerb = ALLTRIM(UPPER(.NAME))
        lcVerb = CHRTRAN(lcVerb, '&', '')
        IF INLIST(lcVerb, 'PASTE', 'LIM INN')
          .Doit()
        ENDIF
      ENDWITH
    ENDFOR

    RELEASE loNS
    RELEASE loShell
  ENDTRY

Open in new window

I don't see how could the code paste the file from local computer into RDS session folder but the question discussion is not the  candidate for deletion.

Please select ID: 41494961 as the answer.

Author

Commented:
The last codegroup pastes as verb and works fine
Yes, you just have to execute the code in RDS not on local machine which is OK in fact.

Commented:
But you still have not said how the code 'knows' when it needs to be run.

If the user on the RDS has to manually launch the code after having copied the filenames to the clipboard, you might as well have had the user do the whole thing within the application thereby making the whole task much easier - most likely eliminating the use of the clipboard entirely.

Good Luck

Author

Commented:
My intentions with this was to have a button in a CRM-module called "Paste files from clipboard" with a shourtcut ALT+V - then I could also map CTRL+V to this button. This is running on a RDS/terminal server using RemoteAPP (the user then only sees the application not the rest of the desktop on the server.
The user browses their local computer and selected som files/folders.. Copies them onto the clipboard and switches back to the application running on the terminal server.

So my workaround is as follows, and the only thing I could find to do it for now. (I can not use \\tsclient as the files might be on a local UNC path, I can not use http/ftp or other protocols to retrieve the files as they are only available through the clipboard).

Invoke Scripting.FileSystemObject to delete temp-folder with files and sub-folders:
    lcMappe = gcTemp + 'MAB_TempPaste'
    IF DIRECTORY((lcMappe), 1)
      loFSO = CREATEOBJECT("Scripting.FileSystemObject")
      loFSO.GetFolder(lcMappe).DELETE(.T.)
      RELEASE loFSO
    ENDIF

    IF !DIRECTORY((lcMappe), 1)
      MKDIR (lcMappe)
    ENDIF

Open in new window


Invoke Shell.Application to walk through the possible verbs on the folder and DO/RUN when "PASTE" ("Lim inn" in Norwegian) is found. I had to walk through them like this instead of using .DoVerb('PASTE') as this would not always work even with & in the right place.
    loShell = CREATEOBJECT("Shell.Application")
    loNS = loShell.NameSpace(lcMappe)
    FOR lnCount = 1 TO loNS.SELF.Verbs.COUNT
      WITH loNS.SELF.Verbs.ITEM(lnCount)
        lcVerb = ALLTRIM(UPPER(.NAME))
        lcVerb = CHRTRAN(lcVerb, '&', '')
        IF INLIST(lcVerb, 'PASTE', 'LIM INN')
          .Doit()
        ENDIF
      ENDWITH
    ENDFOR

Open in new window


This does work and are therefor a solution/workaround for my question.
More sophisticated solution would be to make local disks/folders visible in RDS and allow the direct file selection in the remote app.

BTW, this workaround does not mean "programmatically save files to server-disk" because files are saved on local disk :-).

Author

Commented:
No that is not correct because a local UNC will never be available to the RDS unless it is mapped.
That is _one_ of the reasons why I need to use the clipboard.
And no.. files are saved on server-disk when you hit CTRL+V/PASTE when you are connected to RemoteAPP or RDS - try it

Just to point this out one more time. The Application is running on the RDS/terminal server.. It is NOT ran on local computer.. It's running using RemoteAPP so the user only sees the Application - not the servers full desktop. So CTRL+V will paste files to "the application's chosen folder" which is on the server.

BTW! We also have the possibility for the user to browse local folders (\\tsclient) of course.. But when you use a computer, keyboard shortcuts will always be faster
Yes, I understand it.

I have just been pointing to the wording. "Server" means server from the local workstation point of view.  If some program runs on the server like your RemoteAPP then it uses local disks (C:, D:, E:, ...).  They are on the server but RemoteAPP is also running on the server so it does not access these disks as server disks over the network (most probably) but these disks are local disks for the RemoteAPP. Local disks on the client workstation would have been "redirected" not "mapped" disks for RemoteAPP if you would allow it in your RDS session settings.

Author

Commented:
Ok, to clarify - define:
Local computer (let's call it "PC")
Terminal server/RDS (let's call it "TS")

The user is using PC and puts some files into the clipboard (could be files from PC's C: or PC's UNC-available path.
The user then switches over to a RemoteAPP running on TS and wants to paste the files onto this app - could be like to attach the files to a customer or something.
TS then needs to be able to paste PC's files onto TS's available disks.
What I do with this workaround is to create a local temp-path on TS and then TS pastes the files there (from the clipboard redirected using RDS). RDS then copies the files from local clipboard/paths onto TS's temp-folder which I specified.
My Application (running as RemoteAPP on TS) then scans through this temp-folder and adds all files, scans through sub-folders and add files there etc etc.
Then the temp-folder (on TS) is deleted (from the RemoteAPP). The files are not deleted on PC as I only use verb 'PASTE' from RemoteAPP (running on TS) with TS' folders.

I do not I can make myself any clearer
This is not exactly what you want, but there are a lot of people who would be over the moon if they knew this -

File Transfer using RDP
http://geekswithblogs.net/DesigningCode/archive/2010/04/19/file-transfer-using-rdp.aspx

I just tested it and it is great.
Perhaps you could programmatically (on the server) pull files that are in a local folder ?

Author

Commented:
Rob-Down-Under: thank you. I'm aware of this and we use this when the user hits a button called "Browse" in our system.. But we would also like to be able to paste what's in the clipboard. I always uses clipboard or drag'n'drop myself.. :)

Commented:
My intentions with this was to have a button in a CRM-module called "Paste files from clipboard" with a shourtcut ALT+V - then I could also map CTRL+V to this button.

OK, that makes what you are trying to do more clear.

BUT why involve the Clipboard at all?

Why not have the CRM module open its own Explorer window and allow the user to select the files right there.
Then the CRM application itself would 'know' which files where selected and where they were located.
With that 'knowledge' IT could Copy/Move the files itself and not involve the Clipboard in any way.

Good Luck

Author

Commented:
jrbblr: because, as I have stated multiple times, we already have a browser-button already. But when you run the application ("MAB") from a RDS/terminalserver (RemoteAPP) you don't have access to your local computer's (PC) UNC (other computers/servers in PC's network).
If you try to browse, from RemoteAPP, to your local computer (which is on LAN while RemoteAPP is in another network/country/whatever), you only have Access to your local drives/mapped drives (if you have redirected the drives at all).
So if you have files in \\localfileserver\share\directory .. then you will not be able to BROWSE to them from RemoteAPP..
So you select them in PC's Explorer and click CTRL+C to put them in your clipboard.. RemoteAPP then pastes them in a temp-folder in RemoteAPP's local temp (which is the server-disk) and imports the files from there.

There are not many other solutions, and again: I can not use RDP drive redirection as this do not redirect UNC - and if you have the file on your desktop it would be easier to put it in clipboard instead of navigating to \\tsclient, c, user, username, desktop and then selecting the file.. Nor can I use FTP, http or any other protocol because the user don't necessary have this.
@Magnus_Bleken: You've created good work around for the conditions given and your post ID: 41494961 should be selected as the solution. This question should not be deleted.

Author

Commented:
Workaround

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial