Need help changing a simple DOS script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Need help changing a simple DOS script

#1 Post by OM3 » 20 Jan 2017 19:42

I want to know how I strip file names and get JUST the file name from the path and execute code on that.

I have this batch file:

Code: Select all

"F:\dir 1\dir 2\myprogram.exe" %*


What I do is place in SendTo folder, then I can select multiple files and have the code run.

Problem is this code is run:

Code: Select all

"F:\dir 3\dir 2\dir 1\myprogram.exe" "F:\dir 4\dir 5\dir 6\file 1.txt" "F:\dir 4\dir 5\dir 6\file 2.txt" "F:\dir 4\dir 5\dir 6\file 3.txt"


Whereas I need this to be run:

Code: Select all

"F:\dir 3\dir 2\dir 1\myprogram.exe" "file 1.txt" "file 2.txt" "file 3.txt"


Having the full path is causing me problems.

How do I strip the paths and leave JUST the file name?
IMPORTANT: my file will have spaces in them - I so I need to get the paces as well if they exist.

Not sure where to start with this.

Any help would be great.

Thanks.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Need help changing a simple DOS script

#2 Post by Compo » 20 Jan 2017 20:09

If you don't pass the full paths to your executable, how is it going to know where they are?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help changing a simple DOS script

#3 Post by aGerman » 21 Jan 2017 06:20

Create a shortcut to myprogram.exe in the SendTo folder and you're done. No need for a one-line batch code.

Steffen

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Need help changing a simple DOS script

#4 Post by Sounak@9434 » 21 Jan 2017 08:30

I'm not quite sure what you want to do or what you want to achive but if you just want to strip away the filepath leaving only filename this code may help.(untested)

Code: Select all

setlocal enabledelayedexpansion

set strip=0
set "file=%filename_with_path%" & rem change the %filename_with_path% with which file you want
for /l %%a in (1,1,50) do if not !strip! equ 1 (
 set "tmpv=!file:~-%%a!"
 set "filename=!tmpv:\=!"
 if not "!tmpv!"=="!tmpt!" set strip=1
)
echo(!filename!


Edit: Added a few quotation marks(") to support string with spaces.

Sounak

Post Reply