批处理if语句在for循环中不起作用 [英] Batch if statement not working in for loop
问题描述
我很难让if语句在for中工作 环形.任何帮助将不胜感激.
I'm having a hard time getting the if statement to work within the for loop. Any help would be appreciated.
for %%i in (*.txt) do (
set filename=%%i
set filenametrunc=!filename:~0,28!
If %filename:~0,28%==ProjectLoad_Refresh_%today% (
Set fileName=%filenametrunc%
)
)
推荐答案
我真的不明白为什么不使用"ProjectLoad_Refresh_%today%*.txt"
作为通配符模式而不是仅使用*.txt
,但是此代码应该可以工作:
I do not really understand why not using "ProjectLoad_Refresh_%today%*.txt"
as wildcard pattern instead of just *.txt
, but this code should work:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Today=2017-08-11"
for %%I in (*.txt) do (
set "FileName=%%I"
set "TruncatedName=!FileName:~0,28!"
if "!TruncatedName!" == "ProjectLoad_Refresh_%Today%" set "FileName=!TruncatedName!"
)
endlocal
有一个例外:文件名包含1个或多个感叹号.
There is one exception: A file name contains 1 or more exclamation marks.
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
-
echo /?
-
endlocal /?
-
for /?
-
if /?
-
set /?
-
setlocal /?
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?
这篇关于批处理if语句在for循环中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!