GVKun编程网logo

意外的Windows批处理行为 – 赋值后variables重置 – 在FILE和SET中使用FOR%f获取文件date

16

在本文中,您将会了解到关于意外的Windows批处理行为–赋值后variables重置–在FILE和SET中使用FOR%f获取文件date的新资讯,并给出一些关于batch-file–在Windows

在本文中,您将会了解到关于意外的Windows批处理行为 – 赋值后variables重置 – 在FILE和SET中使用FOR%f获取文件date的新资讯,并给出一些关于batch-file – 在Windows批处理文件中回显精确字符串?、batch-file – 在Windows批处理文件中转义用户输入、batch-file – 如何使用Windows批处理文件列出所有空目录?、batch-file – 如何使用通配符文件名作为命令行参数调用Windows批处理脚本的实用技巧。

本文目录一览:

意外的Windows批处理行为 – 赋值后variables重置 – 在FILE和SET中使用FOR%f获取文件date

意外的Windows批处理行为 – 赋值后variables重置 – 在FILE和SET中使用FOR%f获取文件date

我试图比较一个文件的创builddate到今天的date,并有一个工作的解决scheme:

FOR %%f IN (%ARCHIVE_FILE%) DO SET FileDate=%%~tf IF [%FileDate:~0,-6%] == [%CURRENT_DATE%] goto finish

由于某种原因,在Windows机器上部署这个批处理脚本的时候(两台不同的Windows机器上),FileDatevariables被设置为预期的结果,例如“dd / mm / yy mm:ss” ,然后重置为空。

把:

echo %FileDate% pause

在两行之间显示FileDate被正确分配,然后直接分配给“”。 我无法find答案,并想知道是否有一些稀有的大师们可能对某些事情有所了解:/。

将参数从Windows批处理脚本传递到PowerShell脚本

如何在没有访问registry的情况下仅返回环境variables中的用户path?

Windows:获取和存储variables中的机器名称

在哪里设置path

C Linux stat()以nsec精度获取atime / mtime

谢谢你的帮助。

编辑:

移动到forfiles解决scheme,我将添加任何人寻找替代:

forfiles /p %ARCHIVE_DIR% /m %FILE_NAME% /d 0 && ( goto finish ) || ( goto start )

这应该就够了,我还没有在我需要的机器上检查它。

找出上述初始解决scheme为什么会失败,这还是很好的,哈哈。

使用Chef确保在Windows上删除用户环境variables,但不是系统variables

从Windows获取环境variables

哪些操作系统/平台实现等待变形优化?

在shell脚本中设置环境variables以在远程服务器上运行

在Windows上通过node.js执行git cmd失败,出错

我想你的代码和你发布的那个部分是不一样的。

这些类型的问题通常出现在由括号包围的命令块中。

这是批解析器的扩展阶段的一个问题,因为在执行块之前会发生百分比扩展。

所以你应该切换到延迟扩展

setlocal EnableDelayedExpansion FOR %%f IN (%ARCHIVE_FILE%) DO ( SET "FileDate=%%~tf" echo Doesn't work %FileDate% echo But this works !FileDate! IF "!FileDate:~0,-6!" == "%CURRENT_DATE%" goto finish ) :finish

batch-file – 在Windows批处理文件中回显精确字符串?

batch-file – 在Windows批处理文件中回显精确字符串?

我正在尝试回显存储在变量中的字符串,但它似乎需要大量的转义.我正在尝试使用以下代码:

setlocal EnableDelayedExpansion
@echo off
set "grass=@##&!$^&%**(&)"
echo !grass!

我想逐字回显变量草,所以我在输出中看到@ ##&!$^&%**(&).应该做什么?谢谢!

解决方法

回声!草!将始终逐字回显当前值,而无需任何转义.你的问题是,价值不是你想象的那样!当您尝试设置值时,会发生此问题.

设置值的正确转义序列是

set "grass=@##&^!$^^&%%**(&)"

现在来解释一下.你需要的信息埋藏在How does the Windows Command Interpreter (CMD.EXE) parse scripts?年.但它有点难以理解.

你有两个问题:

1)每次解析该行时,%必须以%%的形式进行转义.引号的存在与否都没有区别.延迟扩张状态也没有区别.

set pct=%
 :: pct is undefined

 set pct=%%
 :: pct=%

 call set pct=%%
 :: pct is undefined because the line is parsed twice due to CALL

 call set pct=%%%%
 :: pct=%

2)A!文字必须被转义为^!每当它被解析器的延迟扩展阶段解析.如果一行包含!在延迟扩展期间的任何地方,然后^文字必须转义为^^.但是对于解析器的特殊字符阶段,^也必须被引用或转义为^^.由于CALL会使任何^字符加倍,这可能会进一步复杂化. (抱歉,描述问题非常困难,解析器很复杂!)

setlocal disableDelayedExpansion

set test=^^
:: test=^

set "test=^"
:: test=^

call set test=^^
:: test=^
:: 1st pass - ^^ becomes ^
:: CALL doubles ^,so we are back to ^^
:: 2nd pass - ^^ becomes ^

call set "test=^"
:: test=^^ because of CALL doubling. There is nothing that can prevent this.

set "test=^...!"
:: test=^...!
:: ! has no impact on ^ when delayed expansion is disabled

setlocal enableDelayedExpansion

set "test=^"
:: test=^
:: There is no ! on the line,so no need to escape the quoted ^.

set "test=^!"
:: test=!

set test=^^!
:: test=!
:: ! must be escaped,and then the unquoted escape must be escaped

set var=hello
set "test=!var! ^^ ^!"
:: test=hello ^ !
:: quoted ^ literal must be escaped because ! appears in line

set test=!var! ^^^^ ^^!
:: test=hello ^ !
:: The unquoted escape for the ^ literal must itself be escaped
:: The same is true for the ! literal

call set test=!var! ^^^^ ^^!
:: test=hello ^ !
:: Delayed expansion phase occurs in the 1st pass only
:: CALL doubling protects the unquoted ^ literal in the 2nd pass

call set "test=!var! ^^ ^!"
:: test=hello ^^ !
:: Again,there is no way to prevent the doubling of the quoted ^ literal
:: when it is passed through CALL

batch-file – 在Windows批处理文件中转义用户输入

batch-file – 在Windows批处理文件中转义用户输入

我有一个 Windows批处理文件,接受密码作为用户输入:

SET /P PASSWORD=Password:

此密码可能包含需要转义的字符!然后使用CALL将PASSWORD变量传递给其他批处理文件

CALL Foo.Bat %PASSWORD%

如何确保特殊字符作为参数进行转义和正确传递?例如,如果用户输入!%“£$”我希望%1在Foo.bat中为!%“£$”.

解决方法

这是一个很好的挑战,但这是先进的批量技术.
我会在这里使用更简单的方法,使用延迟扩展而不发送内容,只发送变量名称.

即使使用特殊字符,这也绝对安全.

call foo.bat password

Foo.bat —————–

Setlocal EnableDelayedExpansion
Echo !password!

编辑:解决原始问题,
这是用内容而不是变量名来解决它的方法

在通过CALL将内容发送到第二批文件之前,有必要准备内容.
很难使用像CALL这样的东西foo.bat%preparedVariable%
使用CALL foo.bat!preparedVariable似乎更好!
但即便如此,我仍未能通过CALL阶段加倍注意.

但后来我发现了一种在CALL阶段之后使用百分比扩展的简单方法.

@echo off

setlocal disableDelayedExpansion
rem set /p "complex=Complex Input "
set "complex=xx! & "!^&"ab^^ " ^^^^cd%%"

setlocal EnableDelayedExpansion

call :prepareForCallBatch complex PreparedParam
echo Send   =!PreparedParam!#
set complex
echo(
call ShowParam.bat %%PreparedParam%%
exit /b

:: Prepare special characters &|<>"^ for a batch call
:prepareForCallBatch
set "temp=!%~1!"

set "temp=!temp:^=^^!"
set "temp=!temp:&=^&!"
set "temp=!temp:|=^|!"
set "temp=!temp:<=^<!"
set "temp=!temp:>=^>!"
set "temp=!temp:"=^^"!"
set "%~2=!temp!"
exit /b

要在ShowParam.bat中查看真实参数,我使用类似的东西
ShowParam.bat

@echo off
setlocal
set prompt=
@echo on
REM # %* #

batch-file – 如何使用Windows批处理文件列出所有空目录?

batch-file – 如何使用Windows批处理文件列出所有空目录?

我想创建一个Windows批处理文件,其中列出了用户指定的根目录下的所有空子目录.

有人可以帮忙吗?

@echo off
for /d /r %1 %%A in (.) do (
  dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)

上述解决方案忽略了隐藏文件夹.我也被告知使用FOR的/ D和/ R选项都会被窃听,尽管我从来没有遇到任何问题.

@echo off
dir /a /b %1 2>nul | findstr "^" >nul || echo %%~fA
for /f "eol=: delims=" %%A in ('dir /s /ad /b %1') do (
  dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)

避免FOR / D / R的第二个解决方案将包括隐藏文件夹.但我相信如果文件夹名称包含Unicode,它可能会失败.

batch-file – 如何使用通配符文件名作为命令行参数调用Windows批处理脚本

batch-file – 如何使用通配符文件名作为命令行参数调用Windows批处理脚本

我有一个小的 Windows批处理脚本,除了打印所有命令行参数(proba.bat)什么都不做

@echo off
:loop
  if "%~1"=="" goto cont
  echo %1
  shift & goto loop
:cont

我希望这个脚本会打印所有.mp4文件,如果我这样调用它:

proba *.mp4

但它只是简单地打印* .mp4.
这在linux上会很容易,但在这里我无法使用它.我究竟做错了什么?

谢谢

解决方法

这模拟了您要做的事情:

@echo off
for %%a in ("%~1") do echo "%%a"

今天关于意外的Windows批处理行为 – 赋值后variables重置 – 在FILE和SET中使用FOR%f获取文件date的介绍到此结束,谢谢您的阅读,有关batch-file – 在Windows批处理文件中回显精确字符串?、batch-file – 在Windows批处理文件中转义用户输入、batch-file – 如何使用Windows批处理文件列出所有空目录?、batch-file – 如何使用通配符文件名作为命令行参数调用Windows批处理脚本等更多相关知识的信息可以在本站进行查询。

本文标签: