0

I'm trying to create a list of all of the files that are dragged and dropped onto a script, separated by semicolons.

set b =
FOR %%a IN (%*) do (
    set "b=%b%%%a;"
) > test.tmp
echo b
pause

This is what I have so far, but the script is not displaying the list of files. What am I doing wrong?

karel
  • 5,489
  • 46
  • 45
  • 50
mikki
  • 43
  • 3
  • `set b =` sets a variable named `b` + _space_. Anyway, the problem is lack of [delayed expansion](http://ss64.com/nt/delayedexpansion.html) in the loop; then you are missing the `%`-signs when reading a variable in `echo %b%`... – aschipfl Mar 19 '19 at 11:56

3 Answers3

0

based on this :

sample code in the file F:\printArguments.bat may be like that:

FOR %%a IN (%*) do (
    CALL:PrintTheArgument %%a
)
pause
GOTO:EOF

:PrintTheArgument

echo. %~1

GOTO:EOF

You can test it for example like that:

F:\printArguments.bat "edek" "z" "fabryki" "kredek"
CAD Developer
  • 1,532
  • 2
  • 21
  • 27
0

To echo the full paths and filename of the files you are to drag/drop, without double quotes:

@echo off
(for %%a in (%*) do (
   echo %%~a;
  )
)> test.tmp
type test.tmp
pause

To include double quotes and full path:

@echo off
(for %%a in (%*) do (
   echo %%a;
  )
)> test.tmp
type test.tmp
pause

to echo filename only, without path:

@echo off
(for %%a in (%*) do (
   echo %%~nxa;
  )
)> test.tmp
type test.tmp
pause

and to echo filename only, excluding path or extension:

@echo off
(for %%a in (%*) do (
   echo %%~na;
  )
)> test.tmp
type test.tmp
pause

type test.tmp is just to show you the content of the file after written, you can remove it if you do not need it.

I suggest you also read the help for for command:

  • for /?

Lastly, if you really want to set a variable (though not needed) you need to enabldelayedexpansion or call echo %%b%% by doubling the %

Gerhard
  • 22,678
  • 7
  • 27
  • 43
0
@echo off
setlocal enabledelayedexpansion

set "b="

for %%a in (%*) do (
    set "b=!b!%%a;"
)

if defined b (
    > test.tmp echo(!b:~0,-1!
)

pause

To append to b in the for loop, delayed expansion may be needed. Use ! instead of % for delayed expansioned varaibles. To trim the trailing ;, first check if the variable is defined, then trim the last character by using -1 in !b:~0,-1!, which gets all characters from position zero to the 2nd last character.

Note: set b = is variable name of b with a space.

View set /? about how to get substrings from strings.

michael_heath
  • 5,262
  • 2
  • 12
  • 22