Deferred extensions and exclamation marks in rows

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

So, I found that for this I only need to add "setlocal enable delayed expansion" to some parts of the code and disable it on the other, but I can not find the right places.

The code:

@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 

Part of the call causes another small (~ 5 lines) batch file that the sqlplus server checks if "! Filename!" really

EDIT: All code works fine and does what it should, if not! in the name of a folder.

+4
source share
2 answers

The problem is expanding parameter c set filename=%%O. An %%Oexclamation mark still remains, but when slow expansion is enabled, the strokes are deleted.
The conclusion is simple, slow expansion should be disabled when expanding the FOR parameter.

?
.

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, ,

set "_filename=!%1!"

, CALL , ..

+3

( ), %var% , (.. ) !var! , (.. ).

call - , , , . , , .

, setlocal, , endlocal setlocal.


OK - , delayedexpansion.

delayedexpansion . (%%O) !filename! %%O. :valid set , !vars!, , , .

@ECHO OFF
setlocal 

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 %%O
    set Validation=

    echo %%O| 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: %%O
    )
)
goto Finish
:valid 

set Folder_path=%Location_Folder%\%filename%
set New_Folder_path=%Destination_folder%\%filename%
echo %Validation%| FINDSTR /c:"1" >NUL
    if ERRORLEVEL 1 (
        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%
        rem redundant instruction : goto Finish
    )
:Finish
exit /b 
+1

Source: https://habr.com/ru/post/1671970/


All Articles