Extract filename from filepath

Hi,

I have a batch file in my sendto folder (it's called batch.bat) that's called with a parameter by right-clicking on any file>sendto>batch. Note that the parameter sent is the "filepath" not "filename". The batch looks like this:

copy %1 c:\windows
(which translates to)
copy c:\filename.exe c:\windows

if I right-click on a file named filename.exe>sendto>batch
The parameter %1 passed is "c:\filename.exe". Now the next line involves renaming the file which is where I have a problem.
rename %1 filename.bak will not work because this will rename c:\filename.txt to backup.bak while my intention is to rename c:\windows\filename.txt to backup.bak.

How do I achieve this? Obviously by extracting the filename and putting it in a variablelike this:

copy %1 c:\windows
;define %filename%
rename c:\windows\%filename% backup.bak

The problem is I don't know how to extract the filename. I would greatly appreciate any help.

Thanks in advance,

ram_einstein
LVL 2
ram_einsteinAsked:
Who is Participating?
 
brianadkinsConnect With a Mentor Commented:

IF %1 is your input

%~d1 = drive
%~p1 = path
%~n1 = name
%~x1 = extension

In your case...

%1 should be the ame as %~dpnx1

-Brian
0
 
brianadkinsCommented:

set FILENAME=%~nx1

0
 
ram_einsteinAuthor Commented:
Thanks a lot! I just have a small favour to ask of you: could you recommend a site where I can learn dos variables, for and set command?

Regards,

ram_einstein
0
 
brianadkinsCommented:

I think that Experts Exchange is THE best place to learn this stuff... I try to spend a few minutes a day reading questions and answers that are not mine and I learn a lot that way.

Here are some batch file bookmarks I also use occasionally:
http://labmice.techtarget.com/articles/batchcmds.htm
http://www.fpschultze.de/bss.htm
http://gearbox.maem.umr.edu/batch/nt.specific.html
http://www.uwasa.fi/~ts/http/http2.html#batch
http://www.ericphelps.com/batch/samples/samples.htm

Finally, using the online DOS help is pretty easy...  just type /? after any command

for example:

for /?
set /?

Both of those contain a lot of information about variables, including the answer to the question you posted here.

Good luck,

-Brian
0
 
ram_einsteinAuthor Commented:
Thanks a lot for that comment Brian. I'll try to learn everything on EE and go thru those links as well.

Reagrds,

ram_einstein
0
All Courses

From novice to tech pro — start learning today.