Batch file to know it's own name.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lost_penguin
Posts: 2
Joined: 03 Sep 2011 11:07

Batch file to know it's own name.

#1 Post by lost_penguin » 03 Sep 2011 11:22

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#2 Post by Ed Dyreen » 03 Sep 2011 12:27

'

Code: Select all

@echo off

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

call /?
pause

exit /b 0

lost_penguin
Posts: 2
Joined: 03 Sep 2011 11:07

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

#3 Post by lost_penguin » 03 Sep 2011 13:32

Thanks a ton Ed.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

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

#4 Post by nitt » 03 Sep 2011 13:41

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.

Post Reply