使用EnableDelayedExpansion时,修改变量的语法是正确的 [英] Correct syntax for variable modifiers when using EnableDelayedExpansion

查看:155
本文介绍了使用EnableDelayedExpansion时,修改变量的语法是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够做到%〜p只能看到文件的路径。但是我不能在循环中使用正确的语法(使用EnableDelayedExpansion)

这是我的代码:

<$






$ b $ set

setlocal EnableDelayedExpansion
for(f)/ f %% F in('dir / a:-d / s / b / r%dir1%')do(
set file_path = %% F
setfile_name = %%〜nF
setnew_path =!file_path:%dir1%=%dir2%!
setnew_path =!new_path :!file_name!=%empty%//这个命令有问题
echo!new_path!

endlocal

有什么办法可以做到这一点? 解决方案

并替换文件路径。



$ set $ set $ set $ set b

$ dir1 =%TMP%\opt
setdir2 = c:\opt

pushd%dir1%&& ('dir / a:-d / b / r / s')do(
setfile_path = %%〜dpF
setnew_path =!file_path:% dir1%=%dir2%!
echo!new_path!
)& popd

endlocal
exit / b 0



说明 h3>

主要的变化是使用变量的〜dp 修饰符,它使得它只获得完整的文件路径没有文件名。



您使用的dir1替换是正确的,这是您的文件名替换是不好的。通过使用〜dp 修饰符,我们可以完全跳过文件名替换部分。
$ b 循环开始设置工作目录

<设置工作目录为 dir1 $ $ $ $ $ $ $ $ <$> b $ b

  • 循环访问该目录中的所有文件和子目录。
  • 对于每个文件,只需获取文件路径 C:\Path\opt \sub 不是 C:\path\opt\sub\file.ext

  • C:\path\opt 替换为 C:\opt

  • 显示结果

  • popd 会将工作目录恢复到 pushd



  • 以下是中所有变量修饰符的列表/? help text。

     另外,FOR变量引用已经得到增强。 
    您现在可以使用以下可选语法:

    %〜I - 展开%我删除任何周围的引号()
    %〜fI - 将%I扩展为完全限定路径名
    %〜dI - 仅将%I扩展为驱动器号
    %〜pI - 将%I扩展为仅路径
    %〜nI - 将%I扩展为文件名
    %〜xI - 仅将%I扩展为文件扩展名
    %〜sI - 扩展路径仅包含短名称
    %〜aI - 将%I扩展为文件的文件属性
    % 〜tI - 将%I扩展为文件的日期/时间
    %〜zI - 将%I扩展为文件大小
    %〜$ PATH:I - 搜索PATH
    环境中列出的目录变量并将%I扩展到第一个找到的
    全限定名称
    如果环境变量名称不是
    定义的或者
    搜索没有找到该文件,那么这个修饰符扩展到
    空字符串

    修饰符可以合并得到复合结果:

    %〜dpI - 将%I扩展为驱动器号和路径
    %〜nxI - 扩展%I仅指定文件名和扩展名
    %〜fsI - 将%I扩展为仅包含短名称的完整路径名
    %〜dp $ PATH:I - 搜索PATH $ b中列出的目录$ b环境变量为%I,并展开到
    驱动器号和找到的第一个路径。
    %〜ftzaI - 将%I扩展为一个DIR,如输出行

    在上面的例子中,%I和PATH可以被其他有效的
    值替换。 %〜语法由一个有效的FOR变量名称终止。
    采用大写字母变量名称(例如%I)使其更具可读性,
    可避免与大小写不敏感的修饰符混淆。


    I want to be able to do %~p to only see the path of the file. But I cant get the correct syntax when using it in a loop (With EnableDelayedExpansion)

    Here's my code:

    @echo off
    set "dir1=%TMP%\opt"
    set "dir2=c:\opt"
    set "empty="
    
    setlocal EnableDelayedExpansion
    for /f %%F in ('dir  /a:-d /s /b /r "%dir1%"') do (
      set "file_path=%%F"
      set "file_name=%%~nF"
      set "new_path=!file_path:%dir1%=%dir2%!"
      set "new_path=!new_path:!file_name!=%empty%" //This command is problematic
      echo !new_path!
    )
    endlocal
    

    Any way for me to do this?

    解决方案

    Here is how to get and replace just the file path.

    @echo off
    setlocal EnableDelayedExpansion
    
    set "dir1=%TMP%\opt"
    set "dir2=c:\opt"
    
    pushd "%dir1%" && for /f %%F in ('dir /a:-d /b /r /s') do (
        set "file_path=%%~dpF"
        set "new_path=!file_path:%dir1%=%dir2%!"
        echo !new_path!
    ) & popd
    
    endlocal
    exit /b 0
    

    Explanation

    The main change is the use of the ~dp modifiers for the variable which causes it to just get the full file path without the file name.

    Your usage of the dir1 replacement was correct, it was your file name replacement that was bad. By using the ~dp modifier we can skip the file name replacement part completely.

    The pushd is only called once before the for loop begins to set the working directory.

    Walkthrough:

    • Set the working directory to dir1
    • Loop through all the files in that directory and subdirectories.
    • For each file get just the file path C:\Path\opt\sub not C:\path\opt\sub\file.ext
    • Replace the C:\path\opt with C:\opt
    • Display the result.
    • popd will restore the working directory to what it was before the pushd.

    Here is a list of all the variable modifiers from the for /? help text.

    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.
    

    这篇关于使用EnableDelayedExpansion时,修改变量的语法是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆