Simplest loop to perform actions on x dragged files?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simplest loop to perform actions on x dragged files?

#16 Post by foxidrive » 31 Mar 2014 02:09

The only issue is the first echo line - the others work

Code: Select all

@echo off
echo D:\somefiles\peter&mary.txt

echo "D:\somefiles\peter&mary.txt"
echo "D:\somefiles\peter+(mary).txt"
pause

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Simplest loop to perform actions on x dragged files?

#17 Post by jeb » 31 Mar 2014 04:21

pstein wrote:If there is really a problem: Can I somehow tell DOS to automatically mask at first such critical chars to


Yes, there is really a problem, as
D:\somefiles\peter&mary.txt
will not be quoted, when calling the batch file.
But then the command line will be parsed incorrectly, so it's impossible to catch the filename with %1 or %*.
And after your batch file finished you get an error message that "mary.txt" isn't a valid command.

The only solution for this is to access the file names via !cmdcmdline! and to end your batch file alsways with a hard EXIT.

The other problem occurs only when you have files with ;,= as it's get tricky to split the cmdcmdline into the valid filenames.

If you have two files in a directory
Peter&Mary,Miller.txt
John, Smith.txt

Your batch is called with something like this

Code: Select all

myDragDrop.bat C:\Peter&Mary,Miller.txt "John, Smith.txt"

So in cmdcmdline is the text C:\Peter&Mary,Miller.txt "John, Smith.txt"

But how to split it now?
You can't split at every space, as you would get three names
C:\Peter&Mary,Miller.txt
"John, Smith.txt"

But even splitting with the preserving of spaces inside the FOR logic fails, as then Mary,Miller will be split.

So you need a clever algorithm, to escape all problematic characters, or the other solution is to append always content inside of quotes.

jeb

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Simplest loop to perform actions on x dragged files?

#18 Post by Squashman » 31 Mar 2014 06:22

pstein wrote:I prefer not to use your tricky SHIFT and GOTO solution but keep it in short, more human readable ol' style.

It is not Tricky at all. Their a are a couple of different ways to implement the code using Shift and Goto, but this is pretty much the acceptable way to do it. All my batch files that use Drag and Drop that I wrote for my users at work do the same thing.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest loop to perform actions on x dragged files?

#19 Post by aGerman » 31 Mar 2014 11:05

@Peter
I wished everything was that simple as GOTO loops. I would rather use a technique that is as bulletproof as possible than praying that the known issues will never occur :wink:
So forgive me if I'll try to act on jebs advice. Maybe we could leave a well-functioning code in your thread.

@jeb
Thanks for pointing to the SO thread. That's how I tried to adjust it. It worked well in my tests but maybe you or somebody else could have a look at.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

setlocal EnableDelayedExpansion
set "params=!cmdcmdline:~,-1!"
set "params=!params:*" =!"
if "!params!"=="" (endlocal &goto end)
endlocal&set "params=%params:"=""%"
set "params=%params:^=^^%"
set "params=%params:&=^&%"
set "params=%params: =^ ^ %"
set params=%params:""="%
set "params=%params:"=""Q%"
set "params=%params:  ="S"S%"
set "params=%params:^ ^ = %"
set "params=%params:""="%"

setlocal EnableDelayedExpansion
set "params=!params:"Q=!"

for %%i in ("!params:"S"S=" "!") do (
  if "!!"=="" endlocal
  if not exist "%%~i\" (
    set "file=%%~i"
    call :proc
  )
)

:end
echo finished
pause
exit


:proc
echo process "%file%" here
goto :eof

Regards
aGerman

EDIT Test for the presence of any parameter added.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Simplest loop to perform actions on x dragged files?

#20 Post by penpen » 31 Mar 2014 13:52

Although this is a dfferent task (you don't drag a file onto a batch; instead you choose an context menue item), this may help you:
http://www.dostips.com/forum/viewtopic.php?f=3&t=4866
Advantages: You don't need to parse the filenames per hand; less command line length problems.
Disadvantage: You need registry access to setup the context menue item.

penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Simplest loop to perform actions on x dragged files?

#21 Post by Squashman » 31 Mar 2014 14:06

I have people who use my Drag and Drop batch files by putting them into their SendTo Context Menu. Is this different then making a direct Context menu? Putting the batch file in the Sendto looks to have the same problem with the Ampersand.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simplest loop to perform actions on x dragged files?

#22 Post by foxidrive » 31 Mar 2014 21:23

I also thought that Send To and dragging uses the same code, and receives the filenames/folders in the same way.

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Simplest loop to perform actions on x dragged files?

#23 Post by pstein » 01 Apr 2014 00:49

Sorry I still have problems to find the problem.

Assume I have this three files:

peter&(mary).txt
(peter)&,mary,.txt
(peter&;,mary).txt

Now I drag them (under 64bit Win7) onto a batch script with the following code (I found meanwhile):

---
echo Parms=%*
echo.
echo Now looping

FOR %%A IN (%*) DO (
Echo Current parm=%%A
)
Echo.
Echo End script
pause
-------------

....then everything is fine. No crash. No half detected parms. All three were double quoted.
Let me know a DOS batch code which will crash when I drag the three files mentioned above.

Peter

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simplest loop to perform actions on x dragged files?

#24 Post by foxidrive » 01 Apr 2014 01:02

pstein wrote:Sorry I still have problems to find the problem.

Assume I have this three files:

peter&(mary).txt
(peter)&,mary,.txt
(peter&;,mary).txt

Now I drag them (under 64bit Win7) onto a batch script with the following code (I found meanwhile):
....then everything is fine. No crash. No half detected parms. All three were double quoted.
Let me know a DOS batch code which will crash when I drag the three files mentioned above.


Create those files in a folder called c:\music\test\myfiles\ and then try them.

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Simplest loop to perform actions on x dragged files?

#25 Post by pstein » 01 Apr 2014 02:29

Ok, I see.

So one (the most important) fact is that "my" simple approach works but only if the files are in or below the directory of the processing batch script. Dragging them from a folder outside the batch script dir will fail.
So not filenames but file pathes matters.

Very interesting.

One more idea to avoid this last, working, but long-winded handling code from aGerman:

Maybe there is a 3rd party helper tool which prepares the passed parameters (=quotes even the first parameter).

set myparms=prepparms.exe %*
....

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simplest loop to perform actions on x dragged files?

#26 Post by foxidrive » 01 Apr 2014 02:45

pstein wrote:So not filenames but file pathes matters.


They both matter, but not so much where they are dragged from.

Essentially if the path or filename has no spaces in it then it will not be quoted by Windows, and because the full path\filename is dragged
then & characters anywhere in the path or filename will cause the normal drag and drop to fail.

Maybe some others characters like , = ; ( ) cause issues in batch code parsing too, when unquoted.

If your path or filename has spaces anywhere in it then it WILL be quoted and this isn't an issue.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest loop to perform actions on x dragged files?

#27 Post by aGerman » 01 Apr 2014 10:35

One more idea to avoid this last, working, but long-winded handling code from aGerman:

Maybe there is a 3rd party helper tool which prepares the passed parameters

Don't worry about the size of the code. All used commands are internal functions of the (already running) cmd.exe. Calling any 3rd party could decrease the performance of the code since an additionally called process takes time to load.

Regards
aGerman

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Simplest loop to perform actions on x dragged files?

#28 Post by Matt Williamson » 01 Apr 2014 10:52

What is the point of this?

aGerman wrote:

Code: Select all

@echo off &setlocal DisableDelayedExpansion

setlocal EnableDelayedExpansion

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest loop to perform actions on x dragged files?

#29 Post by aGerman » 01 Apr 2014 11:17

Looks weird but isn't. The first SETLOCAL initializes a sub environment with delayed variable expansion disabled. That's the base environment for the entire Batch process. This could be necessary if one enabled the delayed expansion by default (it's a registry setting). The next SETLOCAL opens a new (sub-)sub environment with delayed expansion enabled. The related ENDLOCAL let the Batch process return to the initial sub envionment.

Regards
aGerman

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Simplest loop to perform actions on x dragged files?

#30 Post by Matt Williamson » 01 Apr 2014 11:28

Ahh. Thanks for the explanation. That's the first time I've seen that technique used like that.

Post Reply