directory name in Uppercase

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Guy33
Posts: 4
Joined: 04 Jan 2013 21:09

directory name in Uppercase

#1 Post by Guy33 » 04 Jan 2013 21:45

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: directory name in Uppercase

#2 Post by Squashman » 04 Jan 2013 22:21

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: directory name in Uppercase

#3 Post by foxidrive » 05 Jan 2013 02:26

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

Guy33
Posts: 4
Joined: 04 Jan 2013 21:09

Re: directory name in Uppercase

#4 Post by Guy33 » 05 Jan 2013 21:47

Squashman wrote: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


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

Guy33
Posts: 4
Joined: 04 Jan 2013 21:09

Re: directory name in Uppercase

#5 Post by Guy33 » 05 Jan 2013 21:51

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: directory name in Uppercase

#6 Post by Squashman » 05 Jan 2013 22:29

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: directory name in Uppercase

#7 Post by foxidrive » 05 Jan 2013 23:55

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.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: directory name in Uppercase

#8 Post by Dos_Probie » 06 Jan 2013 07:08

Great Tutorial Foxi!..and also explaining More Detail with the student name variable.. Thanx :D

Guy33
Posts: 4
Joined: 04 Jan 2013 21:09

Re: directory name in Uppercase

#9 Post by Guy33 » 08 Jan 2013 10:42

Thanks a lot guys,

now it makes some sense,
I appreciate the help you guys gave

Thx Guy

annelies
Posts: 1
Joined: 02 Jan 2014 08:27

Re: directory name in Uppercase

#10 Post by annelies » 02 Jan 2014 08:31

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: directory name in Uppercase

#11 Post by foxidrive » 02 Jan 2014 20:11

Your description isn't very clear. You can rename that single folder in Explorer and type in DOS :)

Post Reply