How to Parse the Quoted Literals in DOS BATCH

1.    특수기호에 대해서

In DOS batch scripting, there are different types of variables and different ways to use them:

1. **Percent (%) Variables**: These are the standard variables used in batch scripting. They are accessed using a single percent sign (`%`). For example:

 

��� ```batch

��� SET myVar=Hello

��� ECHO %myVar%

��� ```

 

��� In this example, `%myVar%` is expanded to the value of the variable `myVar`, which is `Hello`.

2. **Double Percent (!) Variables (Delayed Expansion)**: Delayed expansion allows you to evaluate variables at execution time rather than at parse time. This is useful when you need to work with variables inside loops or conditional statements. Double percent signs (`!!`) are used for delayed expansion. For example:

��� ```batch

��� SET myVar=Hello

��� SET myVar=Goodbye

��� ECHO !myVar!

��� ```

 

��� In this example, `!myVar!` is expanded to the value of the variable `myVar` at execution time, which is `Goodbye`, even though the value was changed after the first `ECHO` statement.

3. **Percent-Enclosed Variables (`%%`)**: In batch scripts, when you use variables within a `for` loop, you need to double the percent signs (`%%`) to distinguish them from regular variables. For example:

��� ```batch

��� FOR %%i IN (1 2 3) DO (

������� ECHO %%i

��� )

��� ```

 

��� In this example, `%%i` is used to represent the loop variable within the `for` loop. If you were using it outside of a loop, you would only need a single percent sign (`%i`).

 

4. **Using Variables Inside of Variables (`%var%`)**: You can use the value of a variable as the name of another variable by enclosing it in percent signs. For example:

��� ```batch

��� SET var1=value1

��� SET var2=var1

��� ECHO %var2%

��� ```

��� In this example, `%var2%` is expanded to the value of `var1`, which is `value1`.

These are some of the common ways variables are used in DOS batch scripting. Understanding these concepts will help you effectively work with variables in your batch scripts.

참고로 특수문자를 확장하지 않도록 하는 방법이 필요할 때는 섹션 3 캐럿(^) 대해서 살펴보라.

2.    사용

@echo off

setlocal enabledelayedexpansion

 

rem Iterate through all subfolders

for /r /d %%i in (*) do (

    rem Check if there are .com files in the current folder

    dir /b "%%i\*.com" >nul 2>nul

    if not errorlevel 1 (

        rem Change .com files to .caa and save original filenames in changes.txt

        pushd "%%i"

        for %%f in (*.com) do (

            set "old_name=%%~nf%%~xf"

            set "new_name=%%~nf.caa"

            ren "%%f" "!new_name!"

            echo Renamed '!old_name!' to '!new_name!' >> changes.txt

        )

        popd

    )

)

 

echo Done renaming files.

 

 

 

 

@echo OFF

SETLOCAL enableextensions enabledelayedexpansion

 

for /r /d %%i in (*) do (

    rem check if file exists

    if exist "%%i\changes.txt" (

        rem move to the directory

        pushd "%%i"

 

        rem tokenizing for one

        for /f "tokens=1 delims=" %%a in (changes.txt) do (

            rem Replace single quotes with a temporary delimiter (^)

            set "line=abc%%axyz"

            set "line=!line:'=^!"

            rem Extract the substring between the temporary delimiters

            for /f "tokens=2, 4 delims=^" %%b in ("!line!") do (

                rem original COM

                set "SUBSTR1=%%b"

                echo !SUBSTR1!

                rem changed file CAA

                set "SUBSTR2=%%c"

                echo !SUBSTR2!

                ren "!SUBSTR2!" "!SUBSTR1!"

            )

        )

        DEL changes.txt

        popd

    )

)

 

 

 

3.    Additional Tips � Why Carrot is used.

The command `set "line=!line:'=^!"` is a technique commonly used in batch scripting to replace occurrences of a specific character within a string variable with another character. Let's break it down:

 

- `set`: This command is used to assign a value to a variable.

- `"line=!line:'=^!"`: This expression modifies the value of the variable named `line`.

� - `!line!`: This retrieves the current value of the variable `line`. The exclamation marks (`!`) indicate delayed expansion, allowing the variable to be evaluated at runtime.

� - `:'=^!`: This is the replacement part of the expression.

��� - `:'`: This indicates that the character to be replaced is a single quote (`'`).

��� - `^!`: This is the replacement character. In this case, it's a caret (`^`), which is used as a temporary replacement for the single quote. The caret is chosen because it's a character that's rarely used in strings and can easily be changed back to the original character later.

 

So, the command `set "line=!line:'=^!"` replaces all occurrences of single quotes (`'`) in the value of the variable `line` with carets (`^`). This is typically done as a preprocessing step to prepare the string for further processing, such as tokenization or splitting using `for /f` loops.