Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file Copy using %1 for drag and drop

Tags:

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 
like image 554
John Johnson Avatar asked Feb 09 '13 09:02

John Johnson


People also ask

What does %1 do in batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

What is %% P in batch file?

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.


1 Answers

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?

like image 102
jeb Avatar answered Sep 18 '22 18:09

jeb