0

When we start dragging (either on drag start or after drop) a file from e.g. the desktop called hello.pdf, I need to detect which file is being dragged in Powershell or Batch and copy this file to a different folder e.g. desktop/ps_copies, or at least get the path of the file that's being dragged (in this case hello.pdf) and save it into dragged_file_path.txt

<detect dragged file here and save into $pathOfDraggedFile>
$pathOfDraggedFile = "..."

Copy-Item $pathOfDraggedFile -Destination "C:\Users\X\Desktop\ps_copies"
OR $pathOfDraggedFile | Out-File -FilePath C:\Users\X\Desktop\dragged_file_path.txt

Is something like this even possible while Powershell itself is unfocused?

Holiday
  • 871
  • 1
  • 8
  • 14
  • Read that one already, but I can't be editing the registry manually it all needs to happen in the single ps1 file. – Holiday Jul 06 '21 at 08:31

1 Answers1

1

Here is a sample Batch file that can be started with :

@echo off
Mode 75,3 & color 0A
Title Example Drag and Drop File with a Batch file
Set "LogFile=%~dp0Ddragged_Files_Paths.txt"
Set "Folder_ps_copies=%userprofile%\Desktop\ps_copies"
If Not Exist "%Folder_ps_copies%" MD "%Folder_ps_copies%"
echo(
Set "DraggedFile=%~1"
if  "%DraggedFile%" equ "" (
    echo  Usage : Drag and Drop your PDF file over this script:"%~nx0"  
    Timeout /T 5 /nobreak>nul & Exit
) Else (
    Copy "%DraggedFile%" "%Folder_ps_copies%">nul
    echo "%DraggedFile%">>"%LogFile%" 2>&1
)
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Wow, thank you for the reply. Didn't expect someone to write it with Batch. But the main issue I have is, it can't be dragged onto the prompt, because I need the Batch prompt to be invisible, do it behind the scenes. And I don't know if it's a problem with me, but running the script seems to create the folder, but dragging a file over the prompt doesn't copy the file into ps_copies, and no dragged_Files_Paths.txt appears either – Holiday Jul 06 '21 at 10:16