- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在一个复杂的批处理文件中,我想读入带有路径的文件,除其他外,将它们一个接一个地读入一个变量中,用空格分隔。
到目前为止,这与以下代码配合使用效果很好 - 但前提是路径不包含感叹号。
即使使用 setlocal 命令(enabledelayedexpansion/disabledelayedexpansion)我也没有成功处理感叹号。
这里有没有人对这个问题有聪明的想法?
以下示例批处理在当前目录中创建一个文本文件,然后在 for/F 循环中读取它。最后,文本文件中的所有三个路径都应位于变量 %Output% 中。但是带有感叹号。
@echo off
setlocal enabledelayedexpansion
echo This is an example^^! > "textfile.txt"
echo This is a second example^^! >> "textfile.txt"
echo And this line have an ^^! exclamation mark in the middle >> "textfile.txt"
for /F "usebackq tokens=* delims=" %%a in (textfile.txt) do (
set "Record=%%a"
set "Output=!Output!!Record! - "
)
)
echo %Output%
echo !Output!
endlocal
输出是这样的:
This is an example - This is a second example - And this line have an exclamation mark in the middle
但应该是这样的:
This is an example! - This is a second example! - And this line have an ! exclamation mark in the middle
最佳答案
建议不要对处理文件和目录、文本文件中的行、批处理文件本身未定义的字符串或从程序或命令行的执行中捕获的输出使用延迟变量扩展。如果由于某些原因需要在 FOR 循环中使用延迟变量扩展,则应首先将要处理的文件/目录名称、行或字符串分配给环境变量,同时延迟扩展被禁用,然后在 FOR 循环中临时启用延迟扩展。
这是一个批处理文件演示,可以在命令提示符窗口中简单地运行或双击批处理文件。它在临时文件目录中创建了几个用于演示的文件,但在退出前将它们全部删除。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
echo This is an example!> "%TEMP%\TextFile.tmp"
echo This is a second example!>> "%TEMP%\TextFile.tmp"
echo And this line has an exclamation mark ! in the middle.>> "%TEMP%\TextFile.tmp"
set "Output="
(for /F usebackq^ delims^=^ eol^= %%I in ("%TEMP%\TextFile.tmp") do set "Line=%%I" & call :ConcatenateLines) & goto ContinueDemo
:ConcatenateLines
set "Output=%Output% - %Line%" & goto :EOF
:ContinueDemo
cls
echo/
echo All lines concatenated are:
echo/
echo %Output:~3%
set "Output="
del "%TEMP%\TextFile.tmp"
echo File with name ".Linux hidden file!">"%TEMP%\.Linux hidden file!"
echo File with name "A simple test!">"%TEMP%\A simple test!"
echo File with name " 100%% Development & 'Test' (!).tmp">"%TEMP%\ 100%% Development & 'Test(!)'.tmp"
echo/
echo Files with ! are:
echo/
for /F "eol=| tokens=* delims=" %%I in ('dir "%TEMP%\*!*" /A-D /B /ON 2^>nul') do (
set "NameFile=%%I"
set "FileName=%%~nI"
set "FileExtension=%%~xI"
set "FullName=%TEMP%\%%I"
setlocal EnableDelayedExpansion
if defined FileName (
if defined FileExtension (
echo File with ext. !FileExtension:~1!: !NameFile!
) else (
echo Extensionless file: !NameFile!
)
) else echo Extensionless file: !NameFile!
del "!FullName!"
endlocal
)
endlocal
echo/
@setlocal EnableExtensions EnableDelayedExpansion & for /F "tokens=1,2" %%G in ("!CMDCMDLINE!") do @endlocal & if /I "%%~nG" == "cmd" if /I "%%~H" == "/c" set /P "=Press any key to exit the demo . . . "<nul & pause >nul
这个批处理文件的输出是:
All lines concatenated are:
This is an example! - This is a second example! - And this line has an exclamation mark ! in the middle.
Files with ! are:
File with ext. tmp: 100% Development & 'Test(!)'.tmp
Extensionless file: .Linux hidden file!
Extensionless file: A simple test!
带有连接行的文本文件示例使用了从 FOR 循环中调用的子例程来处理文本文件中的行。此处使用的语法是通过使子例程尽可能接近 FOR 命令行来获得最佳性能。如果 FOR 循环必须处理数百甚至数千个项目,这一点很重要。
在将当前处理文件的所有部分分配给环境变量后,示例处理文件名启用和禁用 FOR 循环内的延迟扩展。在处理数千个文件之前减少环境变量列表对于使用此方法获得更好的性能可能很有用。
在 Magoo´s answer 中显示了另一种方法,使用命令 CALL 获取带有引用环境变量的命令行(重新)定义在第二次解析的循环中。我过去也经常使用这种方法,但不要再使用这种方法了,因为它既不安全又效率低下。 call set
导致 cmd.exe
在当前目录和下一个环境变量 PATH
的所有目录中搜索名称为 的文件设置
和环境变量PATHEXT
的文件扩展名。因此,它会在 FOR 循环的每次迭代中导致在后台进行大量文件系统访问,如果偶然出现文件 set.exe
,set。 bat
、set.cmd
等被 cmd.exe
某处发现,由于运行可执行文件或调用批处理文件而不是环境变量的(重新)定义。
我写的以下答案也可能有帮助:
关于for-loop - 如何使用 ! 处理路径名在 for/F 循环中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72155863/
我想了解是什么让 ClearCase 创建这样的路径(不止一个@@):\TUNE\Integration\XmlFiles\PM_Content@@\main\integ_mp1601\4\Commu
我正在构建我的第一个多语言应用程序。通过 Middelware 成功检测到语言并将其附加到 req.lang。下一步,有没有办法也本地化路由路径?这对于 SEO 非常有用。 这里通常的方法是什么? c
如果选择了一个路径,我想向菜单点添加一个类。 如果每个网站都有自己的 .php/.html 文件,那就很容易了,但是一切都在 一个 中规则 .php 文件,所有内容都通过操作进行导航(?action=
我在 Windows 7(最新的 MSMQ 版本)上将 MSMQ.MSMQQueueInfo 与 jscript 一起使用。这是在加入域的计算机上运行的。由于某种我不知道的原因,它只是不接受我给它的
我有很多带空格的文件夹名称,例如“red dog” --> “c:\red dog\” files = dir str = ['cd ', files(3).name] eval(str) 执行返回错
我正在寻找有关如何使用 getopts 显示不带基名的目录名的信息。这应该首先为我计算机中的所有 sh 文件提供基本名称、制表符,然后是所有目录。 例如,如果我这样做:myBashfile.sh -e
我正在尝试自定义由 devise gem 创建的 url: devise_for :users, path: '', path_names: { sign_in: 'login', s
我有以下网址: https://www.example.com/article/f/1/test+article 我需要通过 JavaScript(纯 JavaScript)从 url 中获取“1”部
我们在我们的应用程序中使用了 boost::filesystem。我有一个“完整”路径,它是通过将多个路径连接在一起构建的: #include #include namespace b
Pathname 的 .children 方法返回的文件系统实体的顺序似乎是任意的,或者至少不是按字母顺序排列的。 有没有办法让这些通过文件系统按字母顺序返回,而不是在返回的数组上调用 .sort?
我正在尝试使用 pip 为 python 3 安装 Gelatin,我得到了这个回溯: Traceback (most recent call last): File "", line 1, in
我有一个注册页面的表格。该表单包含一个图像,该图像会根据情况更改其 src。在提交表单时激活的脚本中,如果该图像具有特定的 src,我希望表单调用警报,因此我需要一种检索和比较值的方法。 HTML:
Ruby 的 Dir、File 和 Pathname 类之间有什么区别?它们似乎共享通用方法,如 basename、dirname、glob 和 join。什么时候使用一种比另一种更有优势? 在将其功
我们在我们的应用程序中使用了 boost::filesystem。我有一个“完整”路径,它是通过将多个路径连接在一起构建的: #include #include namespace b
对于以下代码,我不断收到Uncaught TypeError: undefined is not a function: var jay = /^(\/gallery\/P[\w\dåäö\/]+)$
我正在报告从 native 系统 API 收集的一些信息。 (我知道这很糟糕......但我得到了我无法通过其他方式获得的信息,而且如果/当那个时间到来时,我不必更新我的应用程序。) native A
我需要从 Ruby (1.9.3) 中的 DOS (Win32) 路径获取文件的 NT( native )路径。 意思是,我有字符串: dos_path = "C:\Windows\regedit.e
我才刚刚开始使用 ASP.NET MVC,我有一个有点微不足道的问题:似乎每个 Controller 都有一个附加的类似文件夹的路径,所以我的站点变成了 mydomain.net/Home/somet
下面是bunyan logger的__init__函数。 def __init__(self, *args, **kwargs): """Defined default log format
总结 我如何在 C++ 中使用 libarchive 编写一个 zip 文件,这样路径名称将被 UTF-8 编码?使用 UTF-8 路径名时,特殊字符将在使用 OS X/Linux/Windows 8
我是一名优秀的程序员,十分优秀!