有没有一种简单的方法,可以使用DOS BAT命令语言轻松获取DOS文件名的基本名称(不带扩展名)?
我同意:format c:\
可能是一个很好的开始,然后是可启动的Linux CD(假设这些古董机器有CD读取器-不是必须的)。但让我们假装我们只有DOS......(也就是说:不是Windows-甚至不是Windows 3.1,更不用说Windows 95、98、NT、ME、XP、Vista、7等)
有没有一种简单的方法,可以使用DOS BAT命令语言轻松获取DOS文件名的基本名称(不带扩展名)?
我同意:format c:\
可能是一个很好的开始,然后是可启动的Linux CD(假设这些古董机器有CD读取器-不是必须的)。但让我们假装我们只有DOS......(也就是说:不是Windows-甚至不是Windows 3.1,更不用说Windows 95、98、NT、ME、XP、Vista、7等)
对于命令行
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
for /F %i in ("c:\foo\bar space.txt") do @echo %~ni
输出 "bar",如何获取 "bar space.txt"? - Cianticfor /F "delims=" %i in ("c:\foo\bar space.txt") do @echo %~ni
的结果是 "bar space"。 - Ciantic@echo %%~ni
,请改为写入 set MY_VAR=%%~ni
。 - Itay为了进一步解释hobodave和ars的答案,这里是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.
我知道“你不能”这个答案不够好。所以,如果真的必须在实模式DOS下完成,那么获取4DOS命令壳和相关程序并使用它,而不是COMMAND.COM。
http://www.4dos.info/v4dos.htm
我已经多年没有使用它了,但它总是比COMMAND.COM好得多。如果您在此页面上搜索“basename”: http://www.4dos.info/4batfaq.htm 您将看到,在使用4DOS而不是COMMAND.COM时,有一个实模式DOS答案来解决您的问题。在.bat文件中
for /F "delims=" %%i in (%FILE_path%) do @echo "%%~ni"
%
代替%%
。(感谢 @Ciantic)
基于hobodave的接受答案,以下是如何在批处理文件中使用命令来设置变量:
for /F %%i in ("%1") do @set FN=%%~nxi
DOS 6.22不支持在for批处理语句中使用%~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中提取文件名,并打印出"BCHECK -y "和systables.dirpath。
basename
命令可以去除扩展名:basename /some/where/file.txt .txt
会返回file
,这是期望得到的结果。所需要去除的扩展名是已知的。 - Jonathan Leffler