使用 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 时变量修饰符的正确语法 的相关文章

  • NodeJS Express Windows 最大连接数设置

    在哪里设置nodejs的最大连接数 用于使用express get 在 Windows 10 中 与linux中的最大文件 描述符 设置有关吗 有该设置的 Windows 版本吗 最好是在nodejs中进行设置 以便在迁移到unix时兼容
  • Flask 会话变量

    我正在用 Flask 编写一个小型网络应用程序 当两个用户 在同一网络下 尝试使用应用程序时 我遇到会话变量问题 这是代码 import os from flask import Flask request render template
  • 常见的 Windows 编译器上有哪些 std::locale 名称可用?

    该标准对于什么构成有效的语言环境名称几乎没有提及 只有传递无效的区域设置名称才会导致std runtime error 哪些语言环境名称可用于常见的 Windows 编译器 例如 MSVC MinGW 和 ICC 好吧 C 和 C 语言环境
  • 卸载以前的版本安装新版本的安装项目

    我创建了一个安装项目并安装在Windows系统中 在安装安装项目之前 我将其设置为 DetectNewInstallerVersion true and RemovePreviousVersion True 我也每次都换版本 但是 如果我重
  • GIT 和 Ruby:如何从 ruby​​ 脚本内部取消设置 GIT_DIR 变量?

    我编写了一个非常简单的 部署 脚本作为我的post update挂钩到我的裸 git 存储库中 变量如下 live domain mydomain com staging domain stage mydomain com git repo
  • 尽管 if 语句,Visual Studio 仍尝试包含 Linux 标头

    我正在尝试创建一个强大的头文件 无需更改即可在 Windows 和 Linux 上进行编译 为此 我的包含内容中有一个 if 语句 如下所示 if defined WINDOWS include
  • Google Cloud SDK 无法安装“.exe;.bat;.com”不是可识别的命令

    我正在尝试在 Windows 10 笔记本电脑上安装 google cloud SDK 我尝试删除安装文件夹中的空格 并将路径添加到我的环境变量中 完整的错误是 Output folder C Users user Documents Do
  • 如何让R使用所有处理器?

    我有一台运行 Windows XP 的四核笔记本电脑 但查看任务管理器 R 似乎一次只使用一个处理器 如何让 R 使用全部四个处理器并加速我的 R 程序 我有一个基本系统 我使用它在 for 循环上并行化我的程序 一旦您了解需要做什么 此方
  • 获取自动热键中的可用屏幕区域

    我正在尝试编写一些简单的 AutoHotkey 脚本来移动窗口 但在获取正确的屏幕尺寸值时遇到问题 我试图获取屏幕上可用区域的大小 通常是全屏分辨率减去任务栏 也许还有任何其他停靠窗口 如 Vista 中的侧边栏 我发现的获取屏幕宽度的方法
  • 如何迭代所有注册表项?

    我正在尝试迭代所有注册表项以查找 包含 并删除 jre1 5 0 14 值 有办法做到吗 下面的代码只是在特定键下找到jre1 5 0 14 我确实想迭代所有的键 顺便说一句 if 子句获取是否等于 jre1 5 0 14 但如果它包含 j
  • 生成尽可能最快的可执行文件

    我有一个非常大的程序 我一直在 Visual Studio 下编译 v6 然后迁移到 2008 我需要可执行文件尽可能快地运行 该程序大部分时间都花在处理各种大小的整数上 并且执行很少的 IO 显然 我会选择最大优化 但似乎可以做很多不属于
  • 将 kinit 与 keytab 文件一起使用时会发生什么

    希望对kinit和keytab文件的使用有更多的了解 例如 如果我已经为某个服务生成了一个密钥表文件 该服务通过以下方式注册到活动目录 ktpass mapuser to someuseraccount ktab k mykeytab a
  • 将 value 转换为 bool 的魔法

    今天我意识到将值转换为 bool 是一种魔法 int value 0x100 unsigned char uc static cast
  • Mac OS X 上的 /proc/self/cmdline / GetCommandLine 等效项是什么?

    如何在不使用 argc argv 的情况下访问 Mac OS X 上的命令行 在 Linux 上 我会简单地阅读 proc self cmdline or use GetCommandLine在 Windows 上 但我找不到 Mac OS
  • JavaScript 中最长的通用前缀

    我正在尝试解决 Leet Code 挑战14 最长公共前缀 https leetcode com problems longest common prefix 编写一个函数来查找字符串数组中最长的公共前缀字符串 如果没有公共前缀 则返回空字
  • 为什么 Git Bash 无法运行我的可执行文件?

    I am on git for windows https github com git for windows 吉特 巴什 我无法在命令行上运行可执行文件 Pedr Abc 07 MINGW64 c dev ls sqlite3 exe
  • C# 获取子窗口句柄

    我正在用 C 启动一个进程 然后使用 SendMessage 将 Windows 消息发送到该进程 通常我将消息发送到 Process MainWindowHandle 但在某些情况下 我可能需要找到子窗口句柄并向那里发送消息 我将如何在
  • 从 smarty 访问 PHP 文件的变量(本地或全局)

    我有一个 php 文件 其中包含一些本地和全局变量 例如 foo 从此文件中调用 smarty 对象 如何在不更改 PHP 文件的情况下从 smarty 脚本访问 foo Thanks 如果你有一个名为 BASE 的常量变量 并且定义如下
  • 为什么同一个curl命令在windows和linux下输出不同的东西?

    为什么同样的curl o file https www link com 命令输出不同的东西 例如 如果我运行命令curl o source txt https www youtube com playlist list PLIx6Fwnp
  • 从 Powershell 脚本安装 Python

    当以管理员身份从 PowerShell 命令行运行以下命令时 可以在 Windows 11 上成功安装 Python c temp python 3 11 4 amd64 exe quiet InstallAllUsers 0 Instal

随机推荐