DOS BAT文件相当于Unix basename命令?

时间:2010-08-08 02:30:36

标签: batch-file dos

有没有一种简单的方法可以使用DOS BAT命令语言获取DOS文件名的基本名称(没有扩展名的文件名)?

我同意:format c:\可能是一个好的开始,其次是可启动的Linux CD(假设这些古董机器有一个CD阅读器 - 不是给定的)。但是让我们假装我们只有DOS ......(这意味着:不是Windows - 甚至不是Windows 3.1,更不用说Windows 95,98,NT,ME,XP,Vista,7等)。

8 个答案:

答案 0 :(得分:45)

对于命令行

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

输出:bar

适用于.bat文件

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

输出:bar

P.S。如果路径包含空格,请使用@ciantic's version

(进一步阅读: http://www.computerhope.com/forhlp.htm

答案 1 :(得分:30)

要扩展hobodave'sars's个答案,以下是for命令的相关帮助摘要:

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.

答案 2 :(得分:3)

此外,MKS Toolkit还有一个basename util ...

http://www.mkssoftware.com/docs/man1/basename.1.asp

答案 3 :(得分:3)

我明白答案“你不能”不够好。因此,如果它真的/有/在Real Mode DOS中完成,那么获取4DOS命令shell和附带的程序并使用它而不是COMMAND.COM

http://www.4dos.info/v4dos.htm

我多年没有使用它,但它总是/远/好于COMMAND.COM如果你搜索这个页面: http://www.4dos.info/4batfaq.htm 对于“basename”,如果使用4DOS /而不是COMMAND.COM,你会看到有一个真实模式DOS的问题答案

我能想到的唯一其他解决方案路径是查找或编写执行basename的Real Mode .exe。

COMMAND.COM,即使在DOS 6.22及其所有支持的外部命令中,也始终是一个令人难以置信的脚本/批处理文件系统。请注意,MinGW32命令行编程将无济于事。所有这些都是针对保护模式(32位Windows)。如果你在DOS中尝试它,你会得到一个神秘的“这个程序不能在DOS模式下运行。”或类似的东西。

答案 4 :(得分:3)

根据hobodave接受的答案,您可以使用该命令在批处理文件中设置变量:

for /F %%i in ("%1") do @set FN=%%~nxi

它使用第一个命令行参数%1,并将变量FN设置为等于basename(例如file.txt)。

答案 5 :(得分:2)

FOR循环命令中,您可以使用%%~n

答案 6 :(得分:2)

DOS 6.22不支持批处理语句的%~nI。这是我在原始问题中提出的问题的解决方法,关于isql的4.10 bcheck实用程序只需要一个基本名称,而在isql 2.10 bcheck实用程序中使用star.star作为参数。我创建了以下QBASIC程序来解决bcheck 4.10现在只需要一个基本名称来工作:

BatFile$ = "CHKFILE.BAT"
        IF INSTR(COMMAND$, "?") > 0 THEN
          PRINT
          PRINT "This program generates a batch file to check Informix files"
          PRINT "  -b  BBBB.BAT    this option is used to change the batch file name"
          PRINT "                  by default the batch file name is CHKFILE.BAT"
          PRINT
          SYSTEM
        END IF
        IF INSTR(COMMAND$, "-B") > 0 THEN
          BatFile$ = LTRIM$(MID$(COMMAND$, INSTR(COMMAND$, "-B") + 2)) + "  "
          BatFile$ = LEFT$(BatFile$, INSTR(BatFile$, " ") - 1)
        END IF

        OPEN BatFile$ FOR OUTPUT AS #2


        filename$ = DIR$("*.dat")
        IF LEN(filename$) = 0 THEN SYSTEM
        DO WHILE LEN(filename$) > 0
          PRINT #2, "bcheck -y", filename$
          filename$ = DIR$
        LOOP
        CLOSE
        SYSTEM

OR,可以编写ACE程序从systables.dirpath和PRINT“BCHECK -y”中提取基本名称,systables.dirpath

答案 7 :(得分:0)

完整解决方案(即使路径包含空格)

.bat 文件

中的

Content-Type: text/plain; charset=ascii
命令提示中的

使用 for /F "delims=" %%i in (%FILE_path%) do @echo "%%~ni" 而不是 %

thanks to @Ciantic