190 likes | 309 Views
Windows Scripts. This exercise (1/2). Work in pairs (two people in one group) or individually You can copy the program file rows from these slides You must edit these with Notepad and save on hard drive C in the temp folder You may need to create it under the default folder on your computer
E N D
This exercise (1/2) • Work in pairs (two people in one group) or individually • You can copy the program file rows from these slides • You must edit these with Notepad and save on hard drive C in the temp folder • You may need to create it under the default folder on your computer • Then you can run these simply by typing the program name
This exercise (2/2) • Put all the programs made in this exercise in one zip-file • This zip files includes eight files from bat1mod1.bat to bat1mod10.bat • Submit these to pekka.makkonen@ttcollege.edu.sa • In your email to Pekka, you must say your names and studentids and in the subject field of your exercise there must be text “windows scripts exercise” • The exact deadline is mentioned in the classes (March xx, 2013)
cmd • Moves you to command prompt where you are able to type DOS command • Type the command into search program and files under Windows button • You will find it and you can select the program • On cmd use first dir-command to list the content of your default directory • Then create a new directory under tempby typing cd temp • You must accept all the commands by pressing enter
Putting out in a file • Before creating scripts or batch files let’s take some useful commands for people who handle operating systems • Dir > file.txt • Put your output to file.txt • Type file.txt • You can see the content of file.txt • Try these
Appending to a file • Dir >> file.txt • Append another dir listing to file.txt • It will be after the previous content • Pipelines are another form of redirection • Try dir | more • Try these
Variables • You can program with Windows like using any programming language • You can define and use variables. • New variables can be instantiated like this: • set name=value
First batch program @echo off set msg1=Hello set msg2=There! echo %msg1% %msg2% Try this program and edit it that it displays the greeting including three words, which have been saved on three variables. Save it name as bat1mod1.bat
Second batch program @echo off set msg1=one set msg2=%msg1% two set msg3=%msg2% three echo %msg3% Try this program and edit it that it displays the greeting including four words, which have been saved on four variables. Save it name as bat1mod2.bat
If structure @echo off if "%1"=="1" echo The first choice is nice if "%1"=="2" echo The second choice is just as nice if "%1"=="3" echo The third choice is excellent if "%1"=="" echo I see you were wise enough not to choose, You Win! Save the script as a bat file Then try the program with different parameters. Then modify this program that there is one additional choice. Change the echo texts to display as well. Save it name as bat1mod3.bat
If exist structure @echo off if exist hello.bat ( echo Removing hello.bat... del hello.bat echo hello.bat removed! ) • Create hello.bat first • Then use this program for removing hello.bat • You must copy the script and save it as a bat file. After that you can test. • In the second step modify the program that it removes hello.bat and hello2.bat both. In addition, modify the echo texts that a user can have information in other way. Save it name as bat1mod4.bat
For structure @echo off if exist bigtxt.txt rename bigtxt.txt bigtxt for %%f in (*.txt) do type %%f >> bigtxt rename bigtxt bigtxt.txt Save the script as a bat file Then try to use the program. Then modify this program that it is processing bat files instead of txt files. Save it name as bat1mod5.bat
FOR construct and use the option /L @echo off FOR /L %%i IN (0 2 100) DO ( echo *** echo %%i echo *** ) /Loption turns in to like a numeric mode Save the script as a bat file Then try to use the program. Then modify this program that it shows some real text instead of *** marks. In addition, modify the steps of for that for structure starts from 1 and steps are 2 until 94 Save it name as bat1mod6.bat
Loops echo off SET /a i=0 :loop IF %i%==10 GOTO END echo This is iteration %i%. SET /a i=%i%+1 GOTO LOOP :end echo That’s it! Save the script as a bat file Then try to use the program. Then modify this program that it echos different text and the loop will repeated 20 times. Save it name as bat1mod7.bat
Creating aliases @echo off REM home.bat, changes to your home directory and lists contents c: cd \temp cls dir Save this script as a bat file Then try to use the program. Then modify this program that it displays content your USB flash drive. Before that it must echo this in your own language. Change the REM row that it tells precisely what your script does. Save it with the name as bat1mod8.bat
Starting programs in batches @echo off start /max "C:\Windows\System32\Notepad.exe" Save this script as a bat file Then try to use the program. Then modify this that it starts other program from your C drive. Before start command write a command which tells the meaning of this batch for a user. Save it with the name as bat1mod9.bat
Larger example and exercise to the end ECHO OFF CLS :MENU ECHO. ECHO ............................................... ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT. ECHO ............................................... ECHO. ECHO 1 - Open Notepad ECHO 2 - Open Calculator ECHO 3 - Open Notepad AND Calculator ECHO 4 - EXIT ECHO. CHOICE /C:1234 IF ERRORLEVEL 1 SET M=1 IF ERRORLEVEL 2 SET M=2 IF ERRORLEVEL 3 SET M=3 IF ERRORLEVEL 4 SET M=4 Continue on next slide
….Script started from previous slide Save this script bat file Then try to use the program. Then modify this that it starts others programs from your C drive. Change also the echo command that a user can get right information. Save it with the name as bat1mod10.bat IF %M%==1 GOTO NOTE IF %M%==2 GOTO CALC IF %M%==3 GOTO BOTH IF %M%==4 GOTO EOF :NOTE cd %windir%\system32\notepad.exe start notepad.exe GOTO MENU :CALC cd %windir%\system32\calc.exe start calc.exe GOTO MENU :BOTH cd %windir%\system32\notepad.exe start notepad.exe cd %windir%\system32\calc.exe start calc.exe GOTO MENU
If you need more • If you need more batch exercises, ask for these from Pekka