字符串中的延迟扩展和感叹号 [英] Delayed expansion and exclamation marks in strings

查看:64
本文介绍了字符串中的延迟扩展和感叹号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我对批处理脚本还很陌生,我的代码中有这个问题,我在for循环的代码中使用setlocal enabledelayedexpansion,For循环遍历特定目录中的文件夹名称,但是问题在于某些名称可能包含!"(感叹号),如果是这种情况,它们将不计入!filename!"中.当代码创建新目录时,它不包含!".同样,当xcopy尝试从原始文件夹复制文件时,找不到该文件夹​​,因为变量!filename!"与原始文件不同(没有感叹号).

Ok, so I'm still pretty new to batch scripting and I have this problem with my code that I'm using setlocal enabledelayedexpansion in my code for the for loop, the For loop goes through folder names in a specific directory, but the problem is that some of the names may include "!"(exclamation marks) and if that is the case they are not counted for in the "!filename!" and when the code creates a new directory it does not include the "!". Also when the xcopy tries to copy the files from the original folder, the folder is not found, because the variable "!filename!" is not the same as the original(does not have the exclamation point).

因此,我发现为此只需要在代码的某些部分添加"setlocal enable延迟扩展",然后在其他部分将其关闭,但我似乎找不到正确的位置.

So I found that for this I need to only add "setlocal enable delayed expansion" to some parts of the code and turn it off at other, but I cant seem to find the right places.

代码:

@ECHO OFF
setlocal enabledelayedexpansion

SET Location_Folder=V:
SET Destination_folder=V:\Nonimportable

SET Check_file_validation=V:\Nonimportable\result.txt

SET Excluded_folder=Nonimportable
set "lineNr=12"

For /f "tokens=*" %%O in ('dir /b /a:d "%Location_Folder%"') do (
    set filename=%%O

    call D:\somefolder\otherfolder\batscriptname.bat !filename!
    set Validation=

    echo !filename!| FINDSTR /i /c:"%Excluded_folder%" >NUL
    IF ERRORLEVEL 1 (

        for /F "skip=12 delims=" %%a in (%Check_file_validation%) do if not defined Validation (
                set Validation=%%a
                call :valid
        )
    ) else (
        echo This folder name is excluded: !filename!
    )
)
goto Finish
:valid 

echo !Validation!| FINDSTR /c:"1" >NUL
    if ERRORLEVEL 1 (

        set Folder_path=%Location_Folder%\!filename!
        set New_Folder_path=%Destination_folder%\!filename!

        mkdir "!New_Folder_path!"

        echo D | xcopy /o /y /q /s /v "!Folder_path!" "!New_Folder_path!"

        rmdir /s /q "!Folder_path!"
    ) else (

        echo Folder is valid !filename!
        goto Finish
    )
:Finish
exit /b 

调用"部分调用另一个小的(〜5行)批处理文件,该文件检查sqlplus服务器是否为!filename!".有效

The Call part calls another small (~5lines) batch file that checks the sqlplus server if the "!filename!" is valid

除非有!",否则整个代码都可以正常工作,并且应该执行应有的工作.以某个文件夹的名称.

The whole code works fine and does what it should, unless there is a "!" in the name of some folder.

推荐答案

问题是set filename=%%O中的参数扩展. 在%%O中仍然是感叹号,但是启用延迟扩展后,刘海就会掉落.
结论很简单,扩展FOR参数时必须禁用延迟扩展.

The problem is the parameter expansion in set filename=%%O. In %%O is still the exclamation mark, but when delayed expansion is enabled, the bangs are dropped.
The conclusion is simple, delayed expansion have to be disabled when you expand a FOR parameter.

但是,当您还需要延迟扩展时?
您只需切换模式即可.

But when you also need delayed expansion?
You simply toggle the mode.

setlocal DisableDelayedExpansion
For /f "tokens=*" %%O in ('dir /b /a:d "%Location_Folder%"') do (
    set "filename=%%O" -- Here the DelayedExpansion have to be disabled
    setlocal EnableDelayedExpansion

    call D:\somefolder\otherfolder\batscriptname.bat filename
    set "Validation="
     ...
    endlocal
)

另请参见我对CALL myBat.bat filename而不是CALL myBat.bat !filename!的修改.
您不应该将内容与CALL一起使用,而最好通过引用使用变量,并且在您的函数中将内容取为

See also my modification of the CALL myBat.bat filename instead of CALL myBat.bat !filename!.
You shouldn't use content with CALL, better use a variable by reference and in your function take the content by

set "_filename=!%1!"

这是因为CALL本身在空格,尖号等方面有一些讨厌的行为

It's because CALL itself has some nasty behaviour with spaces, carets, etc

这篇关于字符串中的延迟扩展和感叹号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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