We help IT Professionals succeed at work.

Batch File: "Drag and Drop" multiple files to batch file

Hi there,

How can I make this batch file work?

I want to drag and drop multiple files to the batch file, then display them.

The display order is not important.

Thanks,
Rene

@ECHO OFF

REM CREATING ARRAY
FOR /L %%A IN (1,1,100) DO (
   IF "%%A" EQU "" GOTO Display
   SET [File.%%A]=%~%%A
)

:Display
REM DISPLAYING ARRAY
FOR /F "tokens=2 delims==" %%A IN ('SET [File.') DO ECHO %%A
PAUSE
EXIT

Open in new window

Comment
Watch Question

Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Out of time but you should be able to do this looking at %* and/or the SHIFT command for getting access to multiple parms.

~bp
SILVER EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Something like this?
@echo off
setlocal enabledelayedexpansion
set /a ArgCount = 0
:Loop
  if "%~1"=="" goto EndLoop
  set /a ArgCount += 1
  set [File.!ArgCount!]=%~1
  shift
goto Loop
:EndLoop
for /l %%i in (1, 1, %ArgCount%) do (
   echo ![File.%%i]!
)
pause

Open in new window

Steve KnightIT Consultancy
SILVER EXPERT
Commented:
Damn you're too quick around here for me... yes I was about to say reference %1 or %~1 and use SHIFT between each.

You could also use %* but would be messy due to spaces in strings etc. of course.

Have used this in a few batches in the past and works OK.  Not sure what the line length limit is though so probably best to check file exists or whatever before operating on it in case it has hit a limit.


Steve

Author

Commented:
Woohoo!!! I now understand SHIFT :)
Thanks oBdA!

I will also award points to Steve for advise and Bill as part of his answer was used and may be an inspiration to oBdA's answer. Thanks guys!

Cheers,
Rene
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
For me it would take 84 filenames mainly 8.3 but 85 and it says "go away".  Should sufficent I asssume?

Steve
SILVER EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
For the sake of completeness: %* works just as well, it's even a bit shorter; just a matter of personal preference:
@echo off
setlocal enabledelayedexpansion
set /a ArgCount = 0
for %%a in (%*) do (
  set /a ArgCount += 1
  set [File.!ArgCount!]=%%~a
)
for /l %%i in (1, 1, %ArgCount%) do (
   echo ![File.%%i]!
)

Open in new window

Author

Commented:
More than ennuf! I will be processing not more then 40 files at a time.

Assuming the limmit is due to the string size of the arguments, I made some few tests. I found that the path\file size does affect the quantity of files being pulled out; something between 8043 and 8172.

Cheers,
Rene

Author

Commented:
@oBdA: Cool!
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Thanks, glad that got solved, I just didn't have time to do the full write up before...

~bp

Author

Commented:
Don't worry Bill. Thanks for your contribution :)