24

I tried to make a drag-and-drop batch file.

I have the problem that a file exists but the batch file couldn't find it...

I want to copy .png files (like pict_2013020808172137243.png) to another folder and rename it.

In the path are symbols like _ and spaces, also I don't know how to make multi-drag-and-drop to make the same function (rename and add to .zip).

I tried this but with no result :(

@ECHO OFF
ECHO %1
COPY "%1" "%CD%\test\" /Y /S 
REN "%CD%\mob\*.png" "%CD%\test\test.png"
7za u -tzip "%appdata%\.virto\pack.zip" "test" -r
phuclv
  • 37,963
  • 15
  • 156
  • 475
John Johnson
  • 243
  • 1
  • 2
  • 4

1 Answers1

50

Drag & drop is badly implemented for batch files.
The names are quoted, if a space is present, but not if a special character is found, like &,;^

For spaces only in your filenames, you need to change your code only a bit.

@ECHO OFF
ECHO "%~1"
COPY "%~1" "%CD%\test\" /Y /S 
MOVE "%CD%\mob\*.png" "%CD%\test\test.png"
7za u -tzip "%appdata%\.virto\pack.zip" "test" -r

%~1 expands always to an unquoted version, so can always quote them in a safe way.

"c:\Docs and sets" -> %~1 -> c:\Docs and sets -> "%~1" -> "c:\Docs and sets"
c:\Programs -> %~1 -> c:\Programs -> "%~1" -> "c:\Programs"

For more details read Drag and drop batch file for multiple files?

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thank You! That helps me a lot :) – John Johnson Feb 10 '13 at 10:30
  • *`Drag & drop is badly implemented for batch files.`* -- I read this before and remembered it, so I was able to find your answer. Does this statement apply to `.bat` and `.cmd` in the same way? I mean if it is an [explorer issue](http://stackoverflow.com/a/5192427/2932052), why is it only a problem for batch scripts? – Wolf Feb 07 '17 at 11:13
  • I checked that it's working correctly with `.vbs` scripts and `.exe` files (and of course failing with `.cmd`). Really confusing, explorer seems to be *smart enough* but "unwilling" sometimes. Does it -- in your opinion -- make sense to ask why? – Wolf Feb 07 '17 at 11:42
  • 2
    @Wolf That's because only the cmd.exe tries to interpret the `&` characters. .exe and .vbs don't try to interpret the arguments – jeb Feb 07 '17 at 12:15
  • You mean explorer "saves" the special symbols for a cmd script just in a too naive way? Okay, that means that explorer misses that it deals with a collection of files (or folders), not commands. – Wolf Feb 07 '17 at 12:22
  • 1
    @Wolf Yes, for the explorer they are only files, but cmd.exe interprets files with `&` as command seperator. And the explorer doesn't know that it should enclose those filenames also with quotes. It's bad design of the explorer – jeb Feb 07 '17 at 12:33
  • @jeb thanks for explaining. Now I understand your above statement. This knowledge seems not to be wide-spread yet: [`[batch-file]&[drag-and-drop]`](https://stackoverflow.com/questions/tagged/batch-file+drag-and-drop) ;-) – Wolf Feb 07 '17 at 12:55
  • See jeb's answer here: https://stackoverflow.com/a/5192427/4172159 This revised answer works with files with & and ! characters. – JG1212 Mar 16 '21 at 16:21
  • @jeb, how does the users original code work? Should not the `REN` command fail because you cannot specify a path for the new name of the file? – Squashman Mar 26 '21 at 21:01
  • @Squashman You are right, obviously I didn't tested it – jeb Mar 29 '21 at 06:24