Newbie scripter - drag and drop an image file onto a batch file

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Newbie scripter - drag and drop an image file onto a batch file

Post by MigrationUser »

03 Apr 2008 13:53
DG55


I'm completely new to scripting for Windows, so bare with me!

I want to do what seems to be a relatively easy batch file. Here's what it needs to do...

- Need to be able to drag and drop an image (png) file onto the batch file.
- Image file to be copied in the same directory appending '_new' to the end of the file before the extension.
- Newly created file to have an external exe script run on it (this exe file will be located in the same directory or package as the batch file).

Any ideas how to do this? I've given this a go but having problems with copying the file and adding the text before the extension. Being able to drag and drop images from any directory on the computer seems to add another level of complexity.

Cheers.

Last edited by DG55 (03 Apr 2008 13:53)

----------------------------

#2 03 Apr 2008 18:31
bluesxman


This should do the trick:
* * UNTESTED * *

Code: Select all

@echo off

set exe=%~dp0\YourScriptGoesHere.exe

for %%a in (%*) do (
    if /i "%%~xa" EQU ".png" (
        copy /y "%%~a" "%%~dpna_NEW%%~xa"
        "%exe%" "%%~dpna_NEW%%~xa"
    )
)

pause
cmd | *sh | ruby | chef

----------------------------

#3 04 Apr 2008 00:43
DG55


smile Great, this is fantastic. Thanks for the help.

Does it start getting more complicated if I want to be able to drag multiple files onto the batch file?

----------------------------

#4 04 Apr 2008 12:14
bluesxman


Nope, I wrote it so it can handle as many as you'd care to drop. Please note, however, that there is an upper limit in the amount of data that Windows will pass to a script via dropping. If you start getting a weird error dialogue from Windows (I forget what it says exactly) then you probably dropped too many files. Doing so shouldn't hurt anything, it just won't run, so try dropping less files.

With a little tweaking, one could make it accept the dropping of a directory, as well as files, which should mean there's no limit (that I know of) to what it would process.

cmd | *sh | ruby | chef

----------------------------

#5 04 Apr 2008 15:06
DG55


Great. Is there any way of just displaying an error message (in the UI) saying that you have dropped too many files?

Also, is there a way of hiding the CMD dialogue whilst its running?

I'm also looking to contain all the data (batch file and exe program) into one big file so its all in one place, I'd also want to apply a .ico icon file to show up instead of the default window icon. Do you know how to do this, or have any links where it describes how to do this easily?

Thanks.

----------------------------

#6 04 Apr 2008 21:34
bluesxman


No can do on the friendly dialogue. Windows just barfs when you try to pass too long a string to a script by dropping files, CMD doesn't even get a look in. You'll probably know the message when you see it, but the error given doesn't remotely indicate what the problem is.

There are a couple of things one could do to "hide" the CMD window. The most straight forward way will also enable us to address your "icon" query too.

Simply create a shortcut to the script in your preferred location, modify the Properties and set the "Run" option to "Minimized". Then use the "Change Icon" button to select an alternative icon.

Then drop your files on the newly crafted shortcut instead of onto the script itself.

If you want to actually hide the window properly, as in remove its button from the Taskbar, you're going to have to make use of a 3rd party tool such as nircmd. But I'm assuming that since you wanted to change the icon that that's not what you were after, otherwise what'd be the point of changing the icon? smile

Last edited by bluesxman (04 Apr 2008 21:52)

cmd | *sh | ruby | chef

----------------------------

#7 05 Apr 2008 11:44
DG55


Ok great, thanks.

But how would I package all these files together to form one file which can be run from anywhere? I'm just looking for this to be a single file, and don't really want to mess around with installing anything since it isn't really worth it.

Cheers.

----------------------------

#8 05 Apr 2008 12:40
bspus
DG55 wrote:

Ok great, thanks.

But how would I package all these files together to form one file which can be run from anywhere? I'm just looking for this to be a single file, and don't really want to mess around with installing anything since it isn't really worth it.

Cheers.
Try to find a program called "Quick Batch file Compiler"

It allows compiling batch files into exes. You can also add any icon to the exe. By specifying the exe to be a ghost application, you can also hide the command window.

The only reason I suggest this particular batch file compiler, which is not free is that it allows you to attach other small files on the exe as well, which you can call from the compiled batch as if they were in a folder path relative to that batch file.
Apart from that, all the other things can be done with free batch file compilers like bat2exe

----------------------------

#9 07 Apr 2008 09:57
DG55


Fantastic program, thanks. Exactly what I want.

Only one problem which is bugging me though, it wont take a file icon which contains a 256x256 Vista version. It just brings up an error about an invalid pixel format. Doh! Oh well, doesnt matter for the moment I suppose.

Great stuff, thanks for all your help.
Post Reply