Page 1 of 1

Batch file to know it's own name.

Posted: 03 Sep 2011 11:22
by lost_penguin
Hi,

I'm a bit new to batch programming.

I am trying to get a batch file to know it's own name at running time.
I can declare a variable with the file's name (like Something.bat) but I will be sending that file via email to many people.

A lot of them will receive the file and they will open it directly from their email clients, which will create a copy of my .bat in a temp folder and probably with some weird name like ~8364455.cmd


My goal is to copy the Something.bat to a local folder (which I've managed to create already) so the user can run that file whenever they need it again and have an option for going deep into their email attachments. The issue is that I don't know a way for finding out what that ~8364455.cmd filename will be so I can use any of the copy commands.

I can't just use the xcopy or some wildcard since it will copy everything in the folder.

One idea I have is to check the folder's content and copy the last modified file in there, if there is no way to accomplish the above.


Sorry if this is a repost.
Thanks in advance.

Re: Batch file to know it's own name.

Posted: 03 Sep 2011 12:27
by Ed Dyreen
'

Code: Select all

@echo off

echo.
echo.Hello, my name is '%0' but why ?
pause

call /?
pause

exit /b 0

Re: Batch file to know it's own name.

Posted: 03 Sep 2011 13:32
by lost_penguin
Thanks a ton Ed.

Re: Batch file to know it's own name.

Posted: 03 Sep 2011 13:41
by nitt
Ed Dyreen wrote:'

Code: Select all

@echo off

echo.
echo.Hello, my name is '%0' but why ?
pause

call /?
pause

exit /b 0


Yeah, it's good to understand that your 0th argument is always the path of the file. This is useful for most programming languages.