WINDOWS COMMAND PROMPT

LESSON – WILDCARDS & RENAMING

 

separator-blank.png

 

GROUP WORK

 

STEP 0 – SETUP

 

·       In windows, unzip this file and copy its contents to the cmdWork folder that you created in the previous lesson.

 

·       Open the command prompt and navigate to the Lesson03Work directory which is inside the cmdWork directory.  This is where we will do our work for now.


STEP 1 – TERMINOLOGY (WILDCARDS)

·       Wildcards are special characters that can be used to represent unspecified characters.

·       Wildcards are used to create patterns that can then be match with different words.

 

·       The two key wildcard characters in the command prompt are the asterisk (*) and the question mark (?).

 

STEP 2 – CONCEPT (ASTERISK WILDCARD)

 

·       The asterisk wildcard can be used in a pattern to represent any number of characters (including 0).

·       For example, the pattern b*t would match with bt, bat, boot and bullet but not bets or about.

STEP 3 – DIR WITH * PATTERN

·       Navigate to the Wildcard directory in the Lesson03Work.  This directory contains many files so that you can practice using dir with patterns.

·       We can use * patterns with many commands.  For now, we will use the dir command.  Try these:

     dir a*

     dir *.txt

     dir p*.jpg


STEP 4 – CONCEPT (QUESTION MARK WILDCARD)

·       The question mark wildcard, used less commonly than the asterisk, can be used in a pattern to represent any one character.

·       There is one exception, at the end of the file name (before the dot and extension), the question mark can represent zero characters.  (This is annoying at times.)

·       For example, the pattern b?t would match with bat and bet but not with boot or bt.

 

STEP 5 – DIR WITH ? PATTERN

 

·       Here are a few patterns to try with the dir command:

         
dir b?ll.txt

         
dir ???.*

         
dir b?b?.*

STEP 6 – ALL TEXT FILES

 

·       Go to the root directory.

·       Try the following:

     dir /s *.txt   

 

Note that you might need Ctrl-C here to stop this command.

 

STEP 7 – TRY TO FIGURE OUT THE REQUIRED PATTERN

 

·       Go back to the Wildcard directory for the next steps.

 

a)    List all jpg files.

b)    List all text files that start with a and end with t.

c)     List all files that contain bb somewhere in the filename.

 

STEP 8 – RENAMING FILES

 

·       We can rename both files and directories using the ren command.  Note that directories do not need to be empty to be renamed.

·       To use ren, we have to specify the item that we want to rename and then specify the new name.

For example, to rename the file
abc.txt to def.txt, we need to use:

     ren abc.txt def.txt


Try it!

STEP 9 – RENAMING DIRECTORIES

·       Renaming directories is exactly the same as files. 

·       The Wildcard directory contains a directory named applez.  Rename it to apples using:

         
ren applez apples

 

STEP 10 – INCORRECTLY NAMED FILE

 

·       The file billyJoel.txt was named incorrectly.  Use dir to find the file with a typo in the filename and rename it correctly.

 

·       The file eltonJohn.txt was named incorrectly.  Use dir to find the file with a typo in the filename and rename it correctly.

 

STEP 11 – REN WITH * PATTERNS

 

·       We can rename multiple files at once by using * patterns with ren.

·       We want to rename all files with the .dat extension to .txt.

Before renaming files, it is important that we check which files would get renamed to make sure it is what we want.  Use the following to see the files:

 

     dir *.dat

You should see three wrestling files.  Let’s rename them using:

 
     ren *.dat *.txt

 

STEP 12 – CONCEPT (BE CAREFUL!)

 

·       When renaming many files at once, it is possible to accidentally rename the wrong files and this could be a big problem.  So it is very important that you are careful with this.

 

STEP 13 – CONCEPT (POSSIBLE ERROR)

 

·       If you are renaming many files at once, if you are not careful, you might be trying to rename multiple files to the same name.  The command prompt will not allow you to name two files the same so you will get an error saying “Duplicate file name exists”.

·       Here is an obviously bad use of ren that you can try:

     ren b*.txt bob.txt

The above will try to rename each text file that starts with
b to the filename bob.txt.  So, the first text file that starts with b will get renamed to bob.txt.  Every other text file that starts with b will not be renamed as that filename already exists and an error will be displayed.

 

·       Below is another situation where we might be trying to rename multiple files to the same name.  However, it might also work depending on the files in the directory.

Try this:

 

               ren b*.txt ba*.txt

 

To understand the possible problem, we have to understand what the above ren command will do.  Let’s say it has a file named bilbo.txt.  It will rename it to balbo.txt.  So, the a becomes the second letter and the rest of the letters stay the same.  Note that the i is replaced and not shifted over to the next spot.

So the above
ren command would cause an issue if we have two files that have the same name except at the second character spot.  For example, bit.txt and bot.txt would both be renamed to bat.txt and that would be a conflict.

 

separator-blank.png