Return File Name in Batch

Asked

Viewed 1,052 times

2

Is it possible to return the name of a file/program by a bat? Type by dragging it to the bat or run by the bat, it returns the name.

  • When you drag a file pro cmd, it already appears the name.

1 answer

4


Yes, this is a simple example, it takes the parameter "set arg1=%1" and then displays the value "echo %arg1%".

or

It takes the file path you dragged "set arg1=%1" and then shows the path "echo %arg1%".

echo off
set arg1=%1
echo %arg1%
pause

Let’s assume that you drag a txt, and want to open in the Notepad for example:

echo off
set arg1=%1
start notepad %arg1%
  • And how would you use this? Could you explain? I was curious to test.

  • You can take this code and put it in a batch file to see how it works, as soon as you drag it, it will appear the way on the screen.

  • It works like this, %1 is the first parameter, so I put it in the arg1 variable and then showed the variable using echo.

  • I mean, save that code in a . Bat and drag anything on top of that executable?

  • Exactly, that’s how it works.

  • I’ve added one more example for you to better understand how it works.

  • 1

    I knew the passage of parameters by batch, but I didn’t know it worked like this. + 1

  • 1

    Thanks! That’s exactly what I needed

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.