Link to home
Start Free TrialLog in
Avatar of ram_einstein
ram_einstein

asked on

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
Avatar of brianadkins
brianadkins
Flag of United States of America image


set FILENAME=%~nx1

ASKER CERTIFIED SOLUTION
Avatar of brianadkins
brianadkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ram_einstein
ram_einstein

ASKER

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

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
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