【问题标题】:DOS BAT file equivalent to Unix basename command?DOS BAT 文件等同于 Unix basename 命令?
【发布时间】:2011-03-26 20:39:08
【问题描述】:

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

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

【问题讨论】:

  • basename 保留文件扩展名。 IE。 `/home/user/Desktop/test.txt' 变成了 'test.txt'。这是您想要的功能,还是您只是想要“测试”?
  • @eldarerathis:Unix basename 命令可以删除扩展名:basename /some/where/file.txt .txt 产生 file,这是所需的结果。有问题的扩展是已知的。
  • 啊,好吧。那么,这就解决了。
  • @Johannes - 如果您在 DOS 中留下空话,我会接受您的编辑。它是 AFAIAC 的重要组成部分;它是关于 DOS,而不是 Windows。
  • 哎呀,对不起,误读了 :-( 我道歉。可能是我现在对整个 DOS 问题有点过分热心了。虽然我想知道你为什么接受那个答案,因为它显然赢了不能在 DOS 下工作。

标签: batch-file dos


【解决方案1】:

用于命令行

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

对于 .bat 文件

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

输出:bar

如果路径包含空格,则添加"delims=",如下所示:

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

输出:bar baz

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

【讨论】:

  • 我试过: FOR %%f IN (*.DAT) DO bcheck -y %%~nf 在我的 FIX.BAT 脚本中,但它不起作用!.. ~n 是 FOR扩展仅适用于 WinXP cmd.exe 命令解释器。我正在运行不支持 ~ 扩展的本机 DOS 6.22 command.com。查看我之前的帖子:stackoverflow.com/questions/3429032/…
  • for /F %i in ("c:\foo\bar space.txt") do @echo %~ni 输出“bar”,我怎样才能得到“bar space.txt”?
  • @Ciantic:通过阅读我的答案中的链接。或者以适当的方式提出问题。
  • 我的问题的答案是for /F "delims=" %i in ("c:\foo\bar space.txt") do @echo %~ni 产生“酒吧空间”
  • @T.Todua 不是@echo %%~ni,而是写set MY_VAR=%%~ni
【解决方案2】:

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

.bat 文件中

for /F "delims=" %%i in (%FILE_path%) do @echo "%%~ni"

命令提示符中使用 % 而不是 %%

(thanks to @Ciantic)

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      我知道“你不能”的答案不够好。所以,如果真的/has/要在Real Mode DOS下完成,那么获取4DOS命令shell和附带的程序并使用它而不是COMMAND.COM

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

      我已经很多年没用过了,但它总是 /far/ 比 COMMAND.COM 好如果你搜索这个页面: http://www.4dos.info/4batfaq.htm 对于“basename”,如果使用 COMMAND.COM 的 4DOS /instead/,您将看到有一个实模式 DOS 回答您的问题

      我能想到的唯一其他解决方案是找到或编写一个执行 basename 的实模式 .exe。

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

      【讨论】:

        【解决方案5】:

        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
        

        或者,可以编写一个 ACE 程序从 systables.dirpath 中提取基本名称并 PRINT "BCHECK -y ",systables.dirpath

        【讨论】:

          【解决方案6】:

          要扩展 hobodave'sars's 答案,这里是来自 for 命令的相关 sn-p 帮助:

          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.
          

          【讨论】:

            【解决方案7】:

            另外,MKS Toolkit 有一个基本名称 util...

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

            【讨论】:

            • 这已经讨论过了……你可以想象,那些还在坚持DOS的人也坚持不做MKS。
            【解决方案8】:

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

            【讨论】:

              猜你喜欢
              • 2018-11-16
              • 1970-01-01
              • 2011-03-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-17
              相关资源
              最近更新 更多