Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOS BAT file equivalent to Unix basename command?

Tags:

batch-file

dos

Is there an easy way to get to the basename (file name without extension) of a DOS file name using the DOS BAT command language?

I agree: format c:\ is probably a good start, followed by a bootable Linux CD (assuming these antique machines have a CD reader - not a given). But let's pretend that we only have DOS... (That means: not Windows - not even Windows 3.1, let alone Windows 95, 98, NT, ME, XP, Vista, 7, etc.)

like image 356
Jonathan Leffler Avatar asked Aug 08 '10 02:08

Jonathan Leffler


People also ask

What is the equivalent of a BAT file in Linux?

Batch file equivalent in linux is shell script (. sh). You can use gedit, vim or any other text editor available to create one.

Do BAT files work on Unix?

bat or other system startup file or you can make the change using your Windows GUI. Unlike shell scripts on Unix systems, batch files are run by typing their names without their extensions. So to run a batch file named mapdrive. bat, you would type only "mapdrive".

What does basename mean in Unix?

A basename is the name of a directory in a Unix pathname that occurs after the last slash. It is also the name of a standard utility on Unix-like systems that returns the basename when given a Unix pathname.


2 Answers

For command-line

for /F %i in ("c:\foo\bar.txt") do @echo %~ni 

For .bat Files

for /F %%i in ("c:\foo\bar.txt") do @echo %%~ni 

output: bar

If the path contains a space, add in "delims=" like so:

for /F "delims=" %i in ("c:\foo\bar baz.txt") do @echo %~ni 

output: bar baz

(Further Reading: http://www.computerhope.com/forhlp.htm )

like image 66
hobodave Avatar answered Nov 15 '22 22:11

hobodave


To expand on hobodave's and ars's answers, here's the relevant snippet of help from the for command:

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:      %~I         - expands %I removing any surrounding quotes (")     %~fI        - expands %I to a fully qualified path name     %~dI        - expands %I to a drive letter only     %~pI        - expands %I to a path only     %~nI        - expands %I to a file name only     %~xI        - expands %I to a file extension only     %~sI        - expanded path contains short names only     %~aI        - expands %I to file attributes of file     %~tI        - expands %I to date/time of file     %~zI        - expands %I to size of file     %~$PATH:I   - searches the directories listed in the PATH                    environment variable and expands %I to the                    fully qualified name of the first one found.                    If the environment variable name is not                    defined or the file is not found by the                    search, then this modifier expands to the                    empty string  The modifiers can be combined to get compound results:      %~dpI       - expands %I to a drive letter and path only     %~nxI       - expands %I to a file name and extension only     %~fsI       - expands %I to a full path name with short names only     %~dp$PATH:I - searches the directories listed in the PATH                    environment variable for %I and expands to the                    drive letter and path of the first one found.     %~ftzaI     - expands %I to a DIR like output line  In the above examples %I and PATH can be replaced by other valid values.  The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive. 
like image 42
Michael Burr Avatar answered Nov 15 '22 22:11

Michael Burr