Cynosure.X International

:

Add Comment | Related Links | TrackBack
Related Content

basename.bat (GPL)

This "basename.bat" is a Windows batch program that performs the same function as as the UNIX "basename" command. And that function is to strip the parent directory prefix from specified path. You can call this script from your own custom batch script to extract the file name.

"basename.bat" takes advantage of the SetNumberOfArguments.bat (GPL) script that is available on this web site. So to use "basename.bat", you'll have to download SetNumberOfArguments.bat, if you don't already have it.

"basename.bat" is distributed as executable source code under the GNU General Public License. Please see the license agreement elsewhere on this site.

Usage

basename.bat "path"

Example

  > basename "\Users\Medusa\Documents\My Games\Dawn of War 2"
Dawn of War 2

Attached File: basename.bat (581 B)

Chieh Cheng
Mon, 23 Feb 2009 09:55:09 +0000

I have improved "basename.bat" to support the ability to strip a suffix from the name. This improvement brings "basename.bat" more inline with what the UNIX version does.

This version of "basename.bat" takes advantage of the following batch files that are available on this site. You must download these batch files as well, if you don't already have them.

SetNumberOfArguments.bat
SetVariableToOutput.bat
StrLen.cmd

Usage

basename.bat "path" [ "suffix" ]

Print NAME with any leading directory components removed. If specified, also remove a trailing SUFFIX.

Examples

  C:\>basename "Puppet\mainwindow.statenull"
mainwindow.statenull

C:\>basename "Puppet\mainwindow.statenull" .statenull
mainwindow

C:\>basename "Puppet\mainwindow.statenull" null
mainwindow.state

C:\>basename "Puppet\mainwindow.statenull" statenull
mainwindow.

C:\>basename "Puppet\mainwindow.statenull" state
mainwindow.statenull

Attached File: 1 - basename.bat (1 KB)

Chieh Cheng
Mon, 26 Apr 2010 20:37:16 +0000

You're basename.bat seems to be having problems if a file has a ) inside its name, for folders it works ok.
Try these examples:
C:\>basename "C:\ABC\xyz.ext"
xyz.ext

C:\>basename "C:\ABC(CBA)\xyz.ext"
xyz.ext

C:\>basename "C:\ABC\xyz(zyx).ext"
.ext was unexpected at this time.

C:\>basename "C:\ABC\xyz(zyx).ext"
.ext was unexpected at this time.

Paul
Sat, 09 Apr 2011 14:24:59 +0000

Thanks for the scripts, been using them for quite a while :P

But there seems to be a simpler solution to some of your scripts:
If you want to use these in .cmd/.bat files use %% instead of %

with extention:
for %I in ("C:\ABC(CBA)\xyz(zyx).ext") do echo %~nxI
xyz(zyx).ext

without extention:
for %I in ("C:\ABC(CBA)\xyz(zyx).ext") do echo %~nI
xyz(zyx)

extention only:
for %I in ("C:\ABC(CBA)\xyz(zyx).ext") do echo %~xI
.ext

drive letter & dirname
for %I in ("C:\ABC(CBA)\xyz(zyx).ext") do echo %~dpI
C:\ABC(CBA)\

Paul
Sat, 09 Apr 2011 14:43:55 +0000

Thanks Paul, I'll take a look at your examples and the bug when I have a chance.

Chieh Cheng
Mon, 11 Apr 2011 05:51:01 +0000

Paul, I looked at the problem and at your potential solutions. I was tempted to replace the entire content of basename.bat with your examples using "for" statements. However, I decided not to, because the "without extension" example will not match the UNIX implementation of "basename". I wanted to keep the Windows version of "basename.bat" as close to the UNIX version as possible.

Therefore, I fixed the problematic areas in the code. It turns out that Windows is interpreting the parentheses as part of its language syntax. So, sadly, I fixed it by reverting back to the ancient school of unstructured batch programming. The fixed version is attached below.

This problem also affected "StrLen.cmd", which "basename.bat" uses. I fixed that script as well. You'll have to download both scripts to eliminate the problem completely.

Attached File: 2 - basename.bat (2 KB)

Chieh Cheng
Tue, 26 Apr 2011 21:35:50 +0000

I discovered that "basename.bat" cannot handle file names with '!' in them. So I have fixed that problem in this latest version (see attached).

Attached File: 3 - basename.bat (2 KB)

Chieh Cheng
Fri, 13 Jun 2014 12:53:02 +0400

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

Add Comment

Spam Control | * indicates required field
Your Name: *
E-mail:
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 0 - 1?
Click button only once, please!

TrackBack

Send Ping | TrackBack URL | Spam Control

No TrackBacks yet. TrackBack can be used to link this thread to your weblog, or link your weblog to this thread. In addition, TrackBack can be used as a form of remote commenting. Rather than posting the comment directly on this thread, you can posts it on your own weblog. Then have your weblog sends a TrackBack ping to the TrackBack URL, so that your post would show up here.

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

16 Users Online

Hacking Digital Cameras
Fun for Photographers


Amazon Associate

Copyright © 1996 - 2017. All Rights Reserved.