Advertisement
aotreborn111

OS_Assignment_1

Jul 23rd, 2020 (edited)
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.78 KB | None | 0 0
  1. @echo off
  2. title OS_Assignment_1
  3. color b1
  4. rem เก็บชื่อไฟล์(รวมทั้งนามสกุลไฟล์)ไว้ในตัวแปร filename
  5. rem %~0 เป็น environment variable ที่มีค่าเป็น full path ของ batch file เช่น
  6. rem %~0 = C:\Users\User\Desktop\test.bat
  7. rem prefix n คือ ชื่อไฟล์, x คือ file extension
  8. rem %~nx0 = test.bat
  9. set filename=%~nx0
  10.  
  11. :select
  12. echo Main Menu
  13. echo 1. Displays current year in CE and BE formats.
  14. echo 2. Moves all files in specified directory to its root.
  15. echo 3. Plays Guess the Number.
  16. echo.
  17.  
  18. rem set /p คือ การรับค่า input แล้วเอาไปเก็บในตัวแปร
  19. set /p choice="Please Select: "
  20. rem เข้าไปเมนูต่าง ๆ ถ้า input ไม่ใช่เลข 1-3 จะต้องใส่ input
  21. rem goto จะทำให้โปรแกรมจะกระโดดไปตัวงานใน labelต่าง ๆ ตามที่ประกาศไว้
  22. rem label คือบรรทัดที่ขึ้นต้นด้วย colon(:)
  23. if %choice%==1 goto :choice1
  24. if %choice%==2 goto :choice2
  25. if %choice%==3 (goto :choice3) else (goto :reselect)
  26.  
  27. :choice1
  28. rem %date% จะ return ค่าในรูปแบบ   Thu 23/07/2020
  29. rem ~10,4 หมายถึง เอาตั้งแต่ตัวที่ 10  ถึงตัวที่ 14 (10+4) ทำให้เหลือแค่ 2020
  30. set /a year=%date:~10,4%
  31. set /a year2=%year%+543
  32. echo This is %year% CE or %year2% BE.
  33. pause
  34. echo.
  35. echo.
  36. goto :select
  37.  
  38.  
  39. :choice2
  40. echo Please enter directory.
  41. echo Enter "quit" to cancel.
  42. rem รับค่า path ไปยังโฟล์เดอร์ที่จะย้ายไฟล์
  43. rem เช่น C:\ffmpeg\bin
  44. set /p dpath="Directory: "
  45. if exist "%dpath%" (
  46.     echo Directory Exist!
  47.     rem cd = change directory (/d หมายถึงให้เปลี่ยน drive ด้วย)
  48.     cd /d "%dpath%"
  49.     rem วน loop (รอบเดียว) ที่โฟล์เดอร์ตาม input
  50.     rem เนื่องจาก  การใช้ prefix ทำได้แค่เฉพาะกับตัวแปรใน loop และ environment variable เท่านั้น
  51.     rem จะได้ CurrDirName=ชื่อโฟล์เดอร์อย่างเดียว
  52.     rem สมมุติให้ %dpath% คือ C:\ffmpeg\bin จะได้  CurrDirName = bin
  53.     for %%I in (.) do set CurrDirName=%%~nxI
  54.     rem change directory ขึ้นไป 1 ขั้น เช่นจาก  C:\ffmpeg\bin ไป  C:\ffmpeg
  55.     cd ..
  56.     rem กำหนดค่า parent path
  57.     set ppath=%cd%
  58.     rem ถ้า path ที่ user ใส่เข้ามา ไม่มี parent folder จะข้ามไปที่  :wrongdir
  59.     if "%ppath%"=="%dpath%" (
  60.         echo cd=%cd%
  61.         echo dpath=%dpath%
  62.         goto :wrongdir
  63.     )
  64. ) else (
  65.     goto :wrongdir
  66. )
  67.  
  68. echo.
  69. echo Source Directory = %dpath%
  70. echo Destination Directory = %cd%
  71. echo.
  72. set ans=0
  73. set fail=1
  74. echo Note: Subfolder(s) will not be moved.
  75. echo Do you really want to move file(s)?
  76. set /p choice="Yes(Y)/No(N): "
  77. if %choice%==Y (set ans=1
  78.     set fail=0)
  79. if %choice%==y (set ans=1
  80.     set fail=0)
  81. if %choice%==N (set ans=0
  82.     set fail=0)
  83. if %choice%==n (set ans=0
  84.     set fail=0)
  85. if %ans%==1 (
  86.     rem วน loop ทุก ๆ ไฟล์ที่อยู่ในโฟล์เดอร์
  87.     for /r %%i in ("%CurrDirName%\*.*") do (
  88.         if not %%~nxi==%filename% (
  89.             rem ทำการย้ายไฟล์ ถ้าพบว่าไฟล์ในโฟล์เดอร์มีชื่อไม่ตรงกับชื่อของ batch file ที่ทำงานอยู่นี้
  90.             echo Moving %%~nxi ...
  91.             move "%%i" "%cd%"
  92.         )
  93.     )
  94.     echo.
  95.     echo.
  96.     echo Success!
  97.     pause  
  98. )
  99. if %fail%==1 goto :c2fail
  100. echo.
  101. goto :select
  102. :c2fail
  103. echo.
  104. echo Invalid Choice!
  105. echo Program will return to Main Menu.
  106. pause
  107. echo.
  108. goto :select
  109.  
  110. :choice3
  111. rem สุ่มเลข 1-100
  112. rem %random% จะให้ค่าตัวเลขตั้งแต่ 0 ถึง 32767
  113. set /a number=%random% * 100 / 32768 +1
  114. rem echo %number%
  115. :guess
  116. set /p inum="Guess the Number (1-100): "
  117. rem number operations: GTR คือ มากกว่า, LSS คือ น้อยกว่า, EQU คือ เท่ากับ
  118. if %inum% GTR %number% (
  119.     echo Too high!
  120.     pause
  121. )
  122. if %inum% LSS %number% (
  123.     echo Too low!
  124.     pause
  125. )
  126. if %inum% EQU %number% (
  127.     echo Correct!
  128.     echo.
  129.     pause
  130.     goto :select
  131. )
  132. echo.
  133. goto :guess
  134.  
  135. :reselect
  136. echo Invalid Choice!
  137. pause
  138. cls
  139. goto :select
  140.  
  141. :wrongdir
  142. echo Invalid Directory!
  143. pause
  144. echo.
  145. goto :choice2
  146.  
  147. :end
  148. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement