Page 1 of 1

Need help changing a simple DOS script

Posted: 20 Jan 2017 19:42
by OM3
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.

Re: Need help changing a simple DOS script

Posted: 20 Jan 2017 20:09
by Compo
If you don't pass the full paths to your executable, how is it going to know where they are?

Re: Need help changing a simple DOS script

Posted: 21 Jan 2017 06:20
by aGerman
Create a shortcut to myprogram.exe in the SendTo folder and you're done. No need for a one-line batch code.

Steffen

Re: Need help changing a simple DOS script

Posted: 21 Jan 2017 08:30
by Sounak@9434
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