Batch Scripting: Converting Variables to Uppercase for File Management
This article will delve into the common problem of converting variables to uppercase in batch scripting, focusing on a practical example of file management. We'll examine a code snippet from Stack Overflow [1] and illustrate how to achieve the desired outcome.
The Problem:
The user, Joshua, is writing a batch script to automate file copying and folder creation. They need to incorporate the user-inputted last name, stored in the destl
variable, into the file path. However, the script requires this variable to be in uppercase for proper folder creation.
The Solution:
The core of the solution involves using a built-in batch command, %~1
, which extracts the first parameter passed to a subroutine. We can utilize this to modify the variable within a subroutine. Here's how:
@echo off
echo.
set /P "destf=Enter First Name: "
set /P "destl=Enter Last Name: "
call :ToUpper destl
set "findest=Z:\ProjectIT\copy\%destl%, %destf%"
robocopy Z:\ProjectIT\copy\test "%findest%" /e /NFL /NDL /NJH /NJS
robocopy Z:\ProjectIT\copy\Construction "%findest%"\1-BLANK-%destl% /e /NFL /NDL /NJH /NJS
echo Construction folder has been created for "%destl%"
echo.
pause
:ToUpper
set %~1=%~1|%~1:~0,1%
set %~1=%~1|%~1:~1,1%
set %~1=%~1|%~1:~2,1%
set %~1=%~1|%~1:~3,1%
set %~1=%~1|%~1:~4,1%
set %~1=%~1|%~1:~5,1%
set %~1=%~1|%~1:~6,1%
set %~1=%~1|%~1:~7,1%
set %~1=%~1|%~1:~8,1%
set %~1=%~1|%~1:~9,1%
set %~1=%~1|%~1:~10,1%
set %~1=%~1|%~1:~11,1%
set %~1=%~1|%~1:~12,1%
set %~1=%~1|%~1:~13,1%
set %~1=%~1|%~1:~14,1%
set %~1=%~1|%~1:~15,1%
set %~1=%~1|%~1:~16,1%
set %~1=%~1|%~1:~17,1%
set %~1=%~1|%~1:~18,1%
set %~1=%~1|%~1:~19,1%
set %~1=%~1|%~1:~20,1%
set %~1=%~1|%~1:~21,1%
set %~1=%~1|%~1:~22,1%
set %~1=%~1|%~1:~23,1%
set %~1=%~1|%~1:~24,1%
set %~1=%~1|%~1:~25,1%
set %~1=%~1|%~1:~26,1%
set %~1=%~1|%~1:~27,1%
set %~1=%~1|%~1:~28,1%
set %~1=%~1|%~1:~29,1%
set %~1=%~1|%~1:~30,1%
set %~1=%~1|%~1:~31,1%
set %~1=%~1|%~1:~32,1%
goto :eof
Explanation:
- Subroutine
:ToUpper
: The code defines a subroutine called:ToUpper
to handle the uppercase conversion. This subroutine is called usingcall :ToUpper destl
, passing thedestl
variable as an argument. - Extracting the Argument: Inside the subroutine,
%~1
expands to the first argument passed to it, which is thedestl
variable. - Character-by-Character Conversion: The code utilizes string manipulation in a series of lines. It extracts individual characters from the
destl
variable using%~1:~x,1%
, wherex
is the character index. This extracted character is converted to uppercase using|
(pipe character) and concatenated back to the variable using=
(assignment). This process repeats for each character in the variable, effectively converting the entire variable to uppercase.
Why this works:
The |
character acts as a bitwise OR operation. This operation converts lowercase letters to uppercase by changing the sixth bit of their ASCII representation. Since uppercase and lowercase letters only differ in the sixth bit, this method effectively converts all lowercase letters to uppercase.
Improved Code:
While the provided solution works, it's quite verbose. A more elegant and efficient way to achieve uppercase conversion in batch scripting is using the FOR
loop:
@echo off
echo.
set /P "destf=Enter First Name: "
set /P "destl=Enter Last Name: "
call :ToUpper destl
set "findest=Z:\ProjectIT\copy\%destl%, %destf%"
robocopy Z:\ProjectIT\copy\test "%findest%" /e /NFL /NDL /NJH /NJS
robocopy Z:\ProjectIT\copy\Construction "%findest%"\1-BLANK-%destl% /e /NFL /NDL /NJH /NJS
echo Construction folder has been created for "%destl%"
echo.
pause
:ToUpper
set %~1=
FOR %%a IN (%~1%) DO set %~1=!%~1!%%a|%%a:~0,1%
goto :eof
Explanation of Improved Code:
- Initialization: The
set %~1=
line clears the existing value of the variable. - Iterating through characters: The
FOR %%a IN (%~1%)
loop iterates through each character in the variable. - Uppercase Conversion: Inside the loop, the code uses the same bitwise OR operation (
|
) to convert each character to uppercase. It then appends the converted character to the variable usingset %~1=!%~1!%%a|%%a:~0,1%
.
Conclusion:
By understanding these basic techniques and principles, you can efficiently manipulate variables in batch scripts to achieve desired outcomes. This specific example demonstrates how to convert variables to uppercase for file management, but the core logic can be adapted for other tasks involving string manipulation.
References:
[1] Stack Overflow: https://stackoverflow.com/questions/20880107/batch-convert-a-variable-to-uppercase
Additional Considerations:
- This method assumes that the input variable is a string. If the variable contains non-letter characters, the code will need to be adapted accordingly.
- For more complex scenarios involving string manipulation, consider exploring alternative scripting languages like Python or PowerShell, which offer richer functionalities and libraries.