【问题标题】:batch start path\this file's file name批处理启动路径\这个文件的文件名
【发布时间】:2014-11-24 04:55:46
【问题描述】:

标题有点模糊,我不知道如何措辞。

我该怎么做。 运行文件的起始路径\文件名。

有点像 start path\%0 这有点像我想要做的,但如果我这样做,它会想出类似下面的东西。

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

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

对不起,如果我解释得不好。

【问题讨论】:

    标签: windows batch-file cmd


    【解决方案1】:

    你可以用一些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
    

    【讨论】:

    • 谢谢。对于此代码:@echo off @setlocal enableextensions enabledelayedexpansion for %%f in (%0) do set basename=%%~nf set UsPr=%UserProfile% copy %0 "%UsPr%" start "%UsPr%\%basename%.bat" 如果在禁用 CMD 的计算机上使用它,它将不起作用,当使用 start 命令打开第二个批处理文件时,它会出现一个错误,说 cmd 被禁用,我怎么能得到围绕这个?以及如何阻止它无限次打开自身,直到您的计算机崩溃/冻结?
    • @Neb9,如果cmd 被禁用,所有赌注都将关闭。您肯定不能运行任何命令脚本。而且,如果您想停止无限回归,只需添加一个检查以确保 \arbitrary-path\qq.cmd%0 不同。
    • 对不起,我在批处理方面没有经验,我该如何添加您描述的支票?如果 cmd 被禁用,批处理可以工作,但是您不能使用 start 命令从批处理文件启动任何批处理文件。
    • 这是我做的检查,但它不起作用? @setlocal enableextensions enabledelayedexpansion for %%f in (%0) do set basename=%%~nf set UsPr=%UserProfile% copy %0 "%UsPr%" if %0=="%UsPr%\%basename%.bat" goto :1 goto :2 :1 command exit :2 command start "%UsPr%\%basename%.bat" 我可能应该为此创建一个新线程。
    猜你喜欢
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多