使用 EnableDelayedExpansion 时变量修饰符的正确语法

2024-01-08

我希望能够执行 %~p 来仅查看文件的路径。但在循环中使用它时我无法获得正确的语法(使用 EnableDelayedExpansion)

这是我的代码:

@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

我有办法做到这一点吗?


以下是如何获取和替换文件路径。

@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

解释

主要的变化是使用~dp变量的修饰符,使其仅获取不带文件名的完整文件路径。

您对 dir1 替换的使用是正确的,但您的文件名替换是错误的。通过使用~dp修饰符我们可以完全跳过文件名替换部分。

The pushd之前只被调用一次for循环开始设置工作目录。

演练:

  • 将工作目录设置为dir1
  • 循环遍历该目录和子目录中的所有文件。
  • 对于每个文件,仅获取文件路径C:\Path\opt\sub not C:\path\opt\sub\file.ext
  • 更换C:\path\opt with C:\opt
  • 显示结果。
  • popd会将工作目录恢复到之前的状态pushd.

这是所有变量修饰符的列表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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 EnableDelayedExpansion 时变量修饰符的正确语法 的相关文章

  • 在 Windows cmd 中,如何在不引用完整路径的情况下运行当前目录中的可执行文件(而不是 %PATH% 中同名的可执行文件)? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在尝试运行可执行文件foobar来自目录 但 Windows 也恰好有一个名为的可执行文件 或命令 foobar 在 UNIX 中 我
  • 通过消除嵌套的 for 循环来改进此代码

    R 包corrplot除其他内容外 还包含这个漂亮的功能 cor mtest lt function mat conf level 0 95 mat lt as matrix mat n lt ncol mat p mat lt lowCI
  • Chrome 扩展 - 消息传递到批处理文件

    我使用了示例项目https developer chrome com extensions samples https developer chrome com extensions samples我能够运行 python 本机应用程序 有
  • 适用于 Python 的 GitLab CI 共享 Windows 运行器

    我在 GitLab 中有一个 python 项目仓库 我看到 GitLab 共享了可用的测试版 Windows 运行程序 请参阅this https about gitlab com blog 2020 01 21 windows shar
  • 为什么允许在 for 循环中使用列表订阅? [复制]

    这个问题在这里已经有答案了 Python 如何接受以下构造 l 1 2 3 4 for i l i in enumerate l print l i 似乎没有任何抱怨 愉快地打印出来1 2 3 4 这是如何被允许的以及它到底有什么作用 Th
  • 当系统上没有留下任何可执行文件时,如何卸载 Windows 服务?

    当系统上没有留下任何可执行文件时 如何卸载 Windows 服务 我跑不了installutil u因为系统上没有留下可执行文件 我仍然可以在服务控制台中看到该服务的条目 出现这种状态的原因可能是因为 msi 包中存在问题 没有正确删除服务
  • 为什么在使用 Javascript 变量之前不需要先声明它?

    在 Javascript 中我们不需要声明变量var使用关键字之前 我们可以立即做类似的事情myCount 12 or myStr Hello 其中 myCount myStr 之前未声明 任何此类用法都会在 全局 范围内声明并初始化变量
  • Java 7 默认语言环境

    我刚刚安装了 jre7 我很惊讶地发现我的默认区域设置现在是 en US 对于jre6 它是de CH 与jre7有什么不同 默认区域设置不再是操作系统之一吗 顺便说一句 我使用的是Windows7 谢谢你的回答 编辑 我已经看到了语言环境
  • Apache 子进程已退出,状态为 255

    经过大量的搜索 尝试 修复 等待和哭泣 在我放弃之前 我想为这个错误抓住最后的机会 我们正在奔跑Microsoft Windows Server 2012 Apache 2 4 6 Win64 OpenSSL 1 0 1e PHP 5 5
  • 使用 for 循环创建一系列元组

    我已经搜索过 但找不到答案 尽管我确信它已经存在了 我对 python 很陌生 但我以前用其他语言做过这种事情 我正在以行形式读取数据文件 我想将每行数据存储在它自己的元组中 以便在 for 循环之外访问 tup i inLine wher
  • Lua 上的 For 循环

    我的作业是如何执行 for 循环 我已经从数字上弄清楚了 但无法从名称上弄清楚 我想创建一个 for 循环来运行名称列表 以下是我到目前为止所拥有的 names John Joe Steve for names 1 3 do print n
  • Windows 10:任务栏中的图标大小错误

    从 Windows 10 开始 任务栏中的图标大小似乎为 24x24px 如果 Windows 选择图标文件中的 24px 版本进行显示 那么这不会有问题 但它选择了 32px 版本并将其缩小 这会导致丑陋的伪像 尤其是当你的图标中有直线时
  • 如何在 Windows 上应用差异补丁?

    有很多程序可以创建差异补丁 但我在尝试应用一个程序时遇到了很大的困难 我正在尝试分发补丁 但用户向我询问了如何应用该补丁 于是我尝试自己弄清楚 结果发现我毫无头绪 而且我能找到的大多数工具都是命令行的 我可以处理命令行 但是如果没有一个漂亮
  • msvcrt.getch() 每次都会检测到空格

    我正在编写一个简单的Python代码 它应该检测我的击键 但由于某种原因 在每次击键后检测空格 代码 import msvcrt print press escape to quit text while 1 char msvcrt get
  • forrt1:严重(170):程序异常 - 堆栈溢出

    并提前感谢您的帮助 我已经编译了一个程序 不是我编写的 它在 Mac 上运行得很好 但是当我尝试在 Windows 上执行该程序时 在程序开始执行后不久 我收到以下错误消息 forrt1 严重 170 程序异常 堆栈溢出 我不是 ifort
  • Web 应用程序的带宽和流量模拟器?

    您能否建议如何创建一个测试环境来模拟 Web 应用程序中的各种类型的带宽和流量 或者也许是一个针对本地主机执行此操作的开源程序 我认为在编写网络应用程序时这是一个非常重要的主题 但这不是一个常见的主题 我能想象创建这种环境的唯一方法是在本地
  • 无法在 Windows 服务器上使 SVN 预提交脚本失败

    我正在编写一个 SVN pre commit bat 文件 该文件调用 Python 脚本来查询我们的问题跟踪系统 以确定用户提供的问题跟踪 ID 是否处于正确的状态 例如 打开 状态 并与正确的关联项目 SVN 服务器运行 Windows
  • C 变量声明的效率 [重复]

    这个问题在这里已经有答案了 例如 在 C 中声明一个变量需要多长时间int x or unsigned long long var 我想知道它是否会让我的代码在类似的事情中更快 for conditions int var 0 code 这
  • 当我的进程被终止时到底会发生什么?

    我有一个包含本机代码和托管代码的混合进程 在 Windows Server 2003 上运行 当我从进程资源管理器中终止进程时 它会进入 100 cpu 的状态 并在消失之前保持这种状态一段时间 有时甚至 10 分钟 在此期间我无法 杀死
  • perfmon 性能计数器是否基于与 xperf 使用的 ETW 事件“幕后”相同的东西?

    我最近开始熟悉 perfmon 和 xperf Perfmon 使用性能计数器 xperf 使用 ETW Windows 事件跟踪 Perfmon 具有提供数据的对象 而 xperf 使用 提供者 组 作为这个领域的新手 我想问是否有人可以

随机推荐