缺少"!"字符串时EnableDelayedExpansion [英] Missing "!" string when EnableDelayedExpansion

查看:249
本文介绍了缺少"!"字符串时EnableDelayedExpansion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在剧本启用DelayedExpansion,它不会回声出的字符串的文件名。例如:


  

原始结果
  File01-TEXT!.TXT


  
  

回声出结果
  File01-TEXT.TXT


我想这是因为 SETLOCAL EnableDelayedExpansion ,但我不能删除,因为我需要它。

 关闭@echo
SETLOCAL EnableDelayedExpansionCDC:\\文件
对于一个%%中(* .TXT)做(    REM这里的问题...
    回声%%一    设置海峡= %%一个
    设置new_str = STR:0,3!
)回声%new_string%暂停> NUL


解决方案

根据实际code,你可以禁用延迟扩建工程,使其在需要访问修改的内容,然后再禁用

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion    CDC:\\文件
    对于一个%%中(* .TXT)做(
        设置海峡= %%一个        REM选项1
        回声文件:一个%%
        SETLOCAL enabledelayedexpansion
        回声子:STR:0,3〜!
        ENDLOCAL        REM选项2 - 捕捉变化值不延迟扩展上下文中使用
        SETLOCAL enabledelayedexpansion
        在%% B(STR:!〜0,3),做(
            enlocal
            呼应%%一个 - %%一B
        )
    )

When I enabled DelayedExpansion in the script, it doesn't echo out the "!" string in the file name. For instance:

Original
File01-TEXT!.txt

Echo out
File01-TEXT.txt

I guess it's because of the setlocal EnableDelayedExpansion, but I can't remove because I need it.

@echo off
setlocal EnableDelayedExpansion

cd "C:\Files"
for %%a in (*.txt) do (

    REM Here's the problem...
    echo %%a

    set "str=%%a"
    set new_str=!str:0,3!
)

echo %new_string%

pause >nul

解决方案

Depending on the real code, you can work with delayed expansion disabled, enable it where access to modified content is needed and then disable again

@echo off
    setlocal enableextensions disabledelayedexpansion

    cd "C:\Files"
    for %%a in (*.txt) do (
        set "str=%%a"

        rem Option 1 
        echo file: %%a
        setlocal enabledelayedexpansion
        echo substring: !str:~0,3!
        endlocal 

        rem Option 2 - capture changed value to use inside non delayed expansion context
        setlocal enabledelayedexpansion
        for %%b in ("!str:~0,3!") do (
            enlocal
            echo %%a -- %%~b
        )
    )

这篇关于缺少"!"字符串时EnableDelayedExpansion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆