Page 1 of 1
directory name in Uppercase
Posted: 04 Jan 2013 21:45
by Guy33
Hello people,
I am new here and I was wondering if someone could help me out here. For school I have to create a batchfile that checks if a directory exists, and if not creates one with the name given by the user.
Now It does all that, but it should automaticly convert the directory name to Capitals, and I can't get it to work.
I have found many solutions for this problem, just none of them works (or I can't get it to work

)
@ECHO OFF
echo give directory name
SET /P directory name=
IF NOT EXIST %directory name% GOTO execute program
dir /o /ad /w %directoryname%
SET /p answer=continue (y/n)
IF /i %answer%==j goto stop
:execute program
MD %directory name%
FOR /F %%1 IN (studenten.txt) DO MD %directory name%\%%1
goto end
:stop
CD %directory name%
DIR
:end
I had to translate some of the words like directory name from my native language, the transaltion may be not entirely correct,
but so far the batch file works perfect, its just the capitals I can not make them work.
thanks a lot in advance people
Guy
Re: directory name in Uppercase
Posted: 04 Jan 2013 22:21
by Squashman
Some of your syntax looks a bit off but maybe that is your translation.
The Library has an upper case function.
http://www.dostips.com/DtCodeCmdLib.php ... on.toUpper
Re: directory name in Uppercase
Posted: 05 Jan 2013 02:26
by foxidrive
This should also handle long directory names.
Variables with spaces in are a bad idea and the same applies to labels.
Code: Select all
@ECHO OFF
setlocal enabledelayedexpansion
set "directoryname="
SET /P "directoryname=Enter a directory name: "
IF NOT EXIST "%directoryname%\" GOTO execute_program
dir /o /ad /w "%directoryname%"
SET /p "answer=continue (y/n): "
IF /i "%answer%"=="j" goto stop
:execute_program
FOR /F "delims=" %%a IN (studenten.txt) DO (
set "studentname=%directoryname%\%%a"
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "studentname=!studentname:%%b=%%b!"
)
MD "!studentname!"
)
goto end
:stop
CD "%directoryname%"
DIR
:end
pause
Re: directory name in Uppercase
Posted: 05 Jan 2013 21:47
by Guy33
Hey, thanks for the tip,
but I tried this already and somehow I couldn't get it to work, I am a newbie in DOS
but thank you very much for your help
Guy
Re: directory name in Uppercase
Posted: 05 Jan 2013 21:51
by Guy33
foxidrive wrote:This should also handle long directory names.
Variables with spaces in are a bad idea and the same applies to labels.
Code: Select all
@ECHO OFF
setlocal enabledelayedexpansion
set "directoryname="
SET /P "directoryname=Enter a directory name: "
IF NOT EXIST "%directoryname%\" GOTO execute_program
dir /o /ad /w "%directoryname%"
SET /p "answer=continue (y/n): "
IF /i "%answer%"=="j" goto stop
:execute_program
FOR /F "delims=" %%a IN (studenten.txt) DO (
set "studentname=%directoryname%\%%a"
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "studentname=!studentname:%%b=%%b!"
)
MD "!studentname!"
)
goto end
:stop
CD "%directoryname%"
DIR
:end
pause
Thanks man,
this works just fine now, but I am having trouble understanding exactly how this code works and I find it important that I can reproduce it on my own.
So if it is not to much to ask, could you explain the code and the 'thinkprocess' you followed,
maybe that way, I will be able to do this on my own

but thanks already for the great work
Guy
Re: directory name in Uppercase
Posted: 05 Jan 2013 22:29
by Squashman
It is all done in the set statement. The for loop is just used to iterate through the alphabet one letter at a time.
The token variables are probably confusing you. Here is an example of hard coding it.
Code: Select all
C:\Users\Squashman>set var=AaBbCc
C:\Users\Squashman>set var=%var:A=A%
C:\Users\Squashman>echo %var%
AABbCc
C:\Users\Squashman>
You can keep going by then using the letter B and then the letter C to get them all uppercase.
Re: directory name in Uppercase
Posted: 05 Jan 2013 23:55
by foxidrive
Squashman illustrates it well, but the bit that might confuse you is that the student name variable is made up of the %directoryname% variable plus the student name.
This so that every foldername is made uppercase in one step (%directoryname% and the student folder) and saves having a second loop to make the %directoryname% alone uppercase.
FOR /F "delims=" %%a IN (studenten.txt) DO (
set "studentname=%directoryname%\%%a"
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "studentname=!studentname:%%b=%%b!"
)
MD "!studentname!"
)
So it takes the student name, and appends it to the directory name.
Then the for %%b loop changes each letter to be uppercase (it 'cheats' because the function to alter letters is not case sensitive)
After each alphabet character is processed it creates the student folder with the complete path.
This SET command changes letters: set variable=%variable:abc=ABC%
and in that example it changes abc to ABC and does not use delayed expansion (which is the !variable! syntax)
You can get help on the SET command with: set /?
The help is quite long and very helpful with all the ways SET can be used.
Re: directory name in Uppercase
Posted: 06 Jan 2013 07:08
by Dos_Probie
Great Tutorial Foxi!..and also explaining More Detail with the student name variable.. Thanx

Re: directory name in Uppercase
Posted: 08 Jan 2013 10:42
by Guy33
Thanks a lot guys,
now it makes some sense,
I appreciate the help you guys gave
Thx Guy
Re: directory name in Uppercase
Posted: 02 Jan 2014 08:31
by annelies
what if you only want to change the directory name to uppercase?
not the directories with the student names.
if the structure is. DOS being the top map, and the student maps are in the dos map
DOS
student1
student2
student3
i only want directory DOS in uppercase
Re: directory name in Uppercase
Posted: 02 Jan 2014 20:11
by foxidrive
Your description isn't very clear. You can rename that single folder in Explorer and type in DOS
