批处理启动路径\此文件的文件名

时间:2014-11-24 04:55:46

标签: windows batch-file cmd

标题有点模糊,我不确定如何说出来。

我该怎么做 启动路径\正在运行的文件的文件名。

有点像开始路径\%0,这是我想要做的事情,但如果我这样做,它会提出类似下面的东西。

复制C:\ Users \ username \ Desktop \ folder \ C:\ Users \ username \ Desktop \ batch.bat

我只想要C:\ Users \ username \ Desktop \ folder \ batch.bat batch.bat是文件名,如果重命名批处理文件,则必须更改。

很抱歉,如果我解释得很严重。

1 个答案:

答案 0 :(得分:0)

你可以用一些for技巧来做到这一点:

@setlocal enableextensions enabledelayedexpansion
@echo off
echo %0
for %%f in (%0) do set basename=%%~nf
echo Will run \arbitrary-path\%basename%
endlocal

%%~nf为您提供%%f变量的名称,不带路径或扩展名。如果你运行它:

C:\Users\Pax\qq.cmd
你会看到类似的东西:

c:\Users\Pax\qq.cmd
Will run \arbitrary-path\qq

如果在命令窗口中键入for /?,则可以看到所有其他有用的扩展:

    %~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

可以组合修饰符以获得复合结果:

    %~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
相关问题