Instruction
CIT 360 Spring 2020
Project #3
Due by Friday March 27th 2027 (Week 10)
This project should be performed in groups of 2 students. Please be sure to prepare one single submission with both team members’ names on it.
Overview
Each group of 2 students will submit one zip file to Canvas named project3.zip
This zip file should include the following…
• CHANGENAMES.BAT
• changenames.ps1
• pspause.ps1
• calc.ps1
• Readme.txt
o Inside this Readme.txt file should be the names of both/all group members.
____________________________________________________
Part 1: CHANGENAMES.BAT
Create a batch file CHANGENAMES.BAT that does the following (note that %1, %2, and %3 refer to command line arguments passed to the batch file when executed):
1. Creates a set of %1 empty files with randomly generated names, with filename extension %2.
2. Uses the dir command to display a list of the file names.
3. Uses the pause command to pause execution until the user strikes a key.
4. Changes the filename extension of these files from %2 to %3.
5. Uses the dir command to display a list of the modified file names.
6. Pause
7. Uses the del /p to delete the files with extension %3. The /p option will cause the command to prompt for a confirmation before the deletion of each file.
Hints
• type nul > x%random%.txt creates an empty file with a randomly generated name. The name will be of the form x34382.txt, where the number 34382 is randomly generated. There is a similar command in Linux/Unix called “touch” which creates an empty file.
• A common loop when performing admin tasks on a collection of files is a loop that iterates over files in the current directory. For example:
o for %%x in (*) do ( … )
• Note that this loop does not use the /L option.
• If %%x represents the name of a file (name plus extension), then %%~nx represents the name of the file without the filename extension.
Example Run
C:\>changenames 3 blah bleah run command
caution: don’t use “bat”
as one of the extensions
3 random .blah files
02/18/2015 08:22 AM 0 f2567.blah
02/18/2015 08:22 AM 0 f29221.blah
02/18/2015 08:22 AM 0 f4481.blah
3 File(s) 0 bytes
Press any key to continue . . . pause
files renamed
02/18/2015 08:22 AM 0 f2567.bleah
02/18/2015 08:22 AM 0 f29221.bleah
02/18/2015 08:22 AM 0 f4481.bleah
3 File(s) 0 bytes
Press any key to continue . . . pause
C:\\f2567.bleah, Delete (Y/N)? y deleting with confirmation
C:\\f29221.bleah, Delete (Y/N)? y
C:\\f4481.bleah, Delete (Y/N)? y
dir is empty (except .bat)
02/18/2015 08:23 AM .
02/18/2015 08:23 AM ..
02/13/2015 08:22 AM 237 changenames.bat
1 File(s) 237 bytes
Press any key to continue . . . pause
C:\> done
____________________________________________________
Part 2: changenames.ps1 and pspause.ps1
Write a PowerShell script changenames.ps1 to repeat the task of the CHANGENAMES.BAT batch file. Note that there is no “pause” command in PowerShell, so you should write your own, with help from Google. Organize this as a separate script file pspause.ps1 and call this script file from the changenames.ps1 script file.
NOTE: you may not call any .bat file from either .ps1 file. (You must solve the pause problem another way.)
____________________________________________________
Part 3: calc.ps1
Write a PowerShell script named calc.ps1 that performs simple arithmetic calculations of the form
> calc 3 + 4
7
> calc 8 * 3
24
…
You need only support +, -, *, and / operations.
_________________________________________________
PowerShell Basics
Script Structure
param($p1,$p2,…) list of params at the top
function f($a,$b,$c) { some function
…
$result = …
…
Write-output $result
}
…
function g(…) { another function
…
}
… main program … the actual script starts here
Comments
# this is a one liner
<# this
spans multiple lines #>
Comparisons
–eq –ne –lt –le –gt –ge
Boolean Operations
–and –or –not
Boolean Constants
$true $false
Loops
$i = 1
while ($i –le 10) {
Write-output $i
$i++
}
If-Else
if (…) {
…
}
elseif (…) {
…
}
elseif (…) {
…
}
Calling a Function
function f($a, $b, $c) {
…
return …
}
$x = f 1 2 3 don’t write f(1,2,3)