Menu

Showing posts with label Batch. Show all posts
Showing posts with label Batch. Show all posts

Tuesday, October 29, 2019

[Batch Script] วิธีการ Disable การเปลี่ยนภาพพื้นหลังหน้าจอ
[Batch Script] How to Disable Changing Desktop Background

กรณีที่เราต้องการตั้งค่าการเปลี่ยนภาพพื้นหลังหน้าจอผ่าน Batch Script เราจำเป็นต้องเปลี่ยนค่าของ Registry แทนการตั้งค่าผ่าน UI

ขั้นแรก เราต้องหาให้ได้ว่า Registry ตัวใดที่ควบคุมการตั้งค่าการเปลี่ยนภาพพื้นหลัง ซึ่งสามารถหาได้จากลิ้งค์ข้างล่างนี้
How to stop people from changing your Windows 10 desktop wallpaper

ขั้นต่อไป เราต้องเขียน Batch Script ที่มีคำสั่งตามด้านล่างนี้ เพื่อเพิ่ม Key และ Value ของ Registry ข้างต้น
REG ADD HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop /f /v NoChangingWallPaper /t REG_DWORD /d 1
สามารถอ่านเพิ่มเติมได้ที่ Batch Script - Adding to the Registry

กรณีที่เราต้องการลบการตั้งค่าข้างต้น เราสามารถรันคำสั่งตามด้านล่างนี้ใน PowerShell
REG DELETE HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop /f /v NoChangingWallPaper
สามารถอ่านเพิ่มเติมได้ที่ Batch Script - Deleting from the Registry

Friday, October 11, 2019

[Batch Script] วิธีการใช้วันที่และเวลาสำหรับชื่อไฟล์ในไฟล์ Batch
[Batch Script] How to Use Date and Time for Filename in Batch File

สำหรับค่าของวันที่จะอยู่ในตัวแปรชื่อ date ซึ่งเราสามารถเรียกใช้ตามตัวอย่างดังนี้
echo Date format = %date%
echo dd = %date:~4,2%
echo mm = %date:~7,2%
echo yyyy = %date:~10,4%
สำหรับค่าของเวลาจะอยู่ในตัวแปรชื่อ time ซึ่งเราสามารถเรียกใช้ตามตัวอย่างดังนี้
echo Time format = %time%
echo hh = %time:~0,2%
echo mm = %time:~3,2%
echo ss = %time:~6,2%
จากทั้ง 2 ตัวอย่างข้างต้นเป็นการใช้ substring ในการดึงค่าส่วนที่เราต้องการ ซึ่งจะมีรูปแบบการใช้งานดังนี้
%variable_name:~start_position,length%
โดยที่
  • variable_name เป็นชื่อตัวแปรที่เก็บข้อความที่ต้องการตัด
  • start_position เป็นตำแหน่งเริ่มต้นของข้อความที่ต้องการตัด
  • length เป็นความยาวของข้อความที่ต้องการตัด

กรณีที่เราต้องการตั้งชื่อไฟล์ โดยมีวันที่และเวลาในรูปแบบ yyyymmdd_hhmmss อยู่ในชื่อไฟล์ด้วย เราสามารถใช้คำสั่งดังนี้
set filename=test_%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%%time:~6,2%.7z
echo %filename%
หมายเหตุ ตำแหน่งเริ่มต้นของ substring แต่ละชุดจะแตกต่างกันไป โดยขึ้นอยู่กับการตั้งค่า Date and time formats ของแต่ละเครื่อง

Friday, November 29, 2013

[Batch Script] ความแตกต่างระหว่างคำสั่ง CALL และ START
[Batch Script] What Difference between CALL and START commands

CALL เป็นวิธีการเรียกไฟล์ batch อื่น (ไฟล์ batch ที่ถูกเรียก) ภายในไฟล์ batch ที่รันอยู่ (ไฟล์ batch ที่เรียก) แล้วกลับมายังไฟล์ batch ที่เรียก โดยไม่ยกเลิกการประมวลผลของไฟล์ที่เรียก และมีการใช้สภาพแวดล้อมเดียวกันสำหรับไฟล์ batch ที่เรียกและไฟล์ batch ที่ถูกเรียก
CALL is the way to call another batch file within a batch file and return to the caller without aborting the execution of the calling batch file, and using the same environment for both batch files.

START เป็นวิธีการเริ่มโปรแกรมในหน้าต่าง cmd.exe ใหม่ ดังนั้นมันสามารถรันไฟล์ batch ที่ถูกเรียกได้พร้อมกัน
START is the way to start a program in a new cmd.exe instance, so it can run a called batch file asynchronosly

[Batch Script] วิธีการเปิดเบราว์เซอร์ Chrome ด้วยตำแหน่งที่ต่างกัน
[Batch Script] How to Open Chrome Browsers with Difference Position

สมมติว่า ;C:\Program Files (x86)\Google\Chrome\Application ถูกเพิ่มไปยังพาธของระบบ
Suppose that ;C:\Program Files (x86)\Google\Chrome\Application is added to system path

สำหรับการเปิดเบราว์เซอร์ Chrome 2 เบราว์เซอร์ด้วยตำแหน่งที่ต่างกัน ลองสคริปต์ด้านล่าง
For opening two Chrome browsers with difference position, try below script
start chrome --window-position=100,100 --new-window
start chrome --window-position=400,400 --new-window

หมายเหตุ: ปิดเบราว์เซอร์ Chrome ทั้งหมดก่อนรันสคริปต์นี้
Notice: Close all Chrome browsers before running this batch script

Monday, November 4, 2013

[Batch Script] วิธีรันไฟล์แบทช์ทั้งหมดภายในแฟ้มที่กำหนด
[Batch Script] How to Run All Batch Files within Specific Folder

ถ้าคุณมีไฟล์แบทช์มากมาย คุณไม่จำเป็นต้องรวมมันมาไว้ในไฟล์เดียวเพื่อรัน
If you have many batch files, you are not necessary to merge them into one file

สคริปต์แบทช์เป็นตามด้านล่าง
Batch script is below
  1. @echo off
  2. FOR %%x IN ("path_batch_folder\*.bat") DO call "%%x"

โดยที่ path_batch_folder คือ ตำแหน่งของแฟ้มที่เก็บไฟล์แบทช์
where path_batch_folder is a path of  folder of batch files

Tuesday, October 29, 2013

[Batch Script] คำสั่งและตัวเลือกสำหรับบราวเซอร์ Chrome
[Batch Script] Chrome Browser Command Line

สมมติว่า ;C:\Program Files (x86)\Google\Chrome\Application ถูกเพิ่มไปยัง system path (ตาม วิธีการเพิ่มค่าใหม่ใน System Path)
Suppose that ;C:\Program Files (x86)\Google\Chrome\Application is added to system path (follow How to Add New Values to System Path)

เพื่อเปิดบราวเซอร์ Chrome ใช้คำสั่ง
To open Chrome browser, use command
start chrome

เพื่อเปิด URL ที่ต้องการด้วยบราวเซอร์ Chrome ใช้คำสั่ง
To open Chrome browser with specified URL, use command
start chrome "url"

[Batch Script] วิธีการหาพาธของแฟ้มของไฟล์ Batch ที่กำลังประมวลผล
[Batch Script] How to Get Directory Path of Executing Batch File

เพื่อเข้าไปยังแฟ้มของไฟล์ batch ที่กำลังประมวลผล ใช้คำสั่ง
To get into directory path of executing batch file, use command
cd /d %~dp0

Wednesday, June 12, 2013

[Batch Script] FOR Syntax

syntax-FOR-Files
       FOR %%parameter IN (set) DO command

syntax-FOR-Folders
       FOR /D %%parameter IN (folder_set) DO command
 
 syntax-FOR-List of numbers  
       FOR /L %%parameter IN (start,step,end) DO command

[Batch Script] วิธีเปลี่ยนค่าตัวแปรในลูป FOR
[Batch Script] How to Change Variable in FOR Loop

เนื่องจากลูป FOR ถูก interpret ตัวแปรในลูป FOR จึงไม่สามารถเปลี่ยนค่าและใช้ค่าที่ถูกเปลี่ยนในลูปเดียวกันได้
Due to FOR loop is interpreted, we cannot change a variable within a FOR loop and use that changed variable in the same loop

เราจึงต้องใช้ Delayed variable expansion ดังนั้นใส่คำสั่งนี้ก่อนลูป FOR
Delayed variable expansion is needed so add this command before FOR loop 
setlocal enabledelayedexpansion

แล้วใส่คำสั่งนี้ใต้ลูป FOR
Then add this command below FOR loop
endlocal 

[Batch Script] เปลี่ยนชื่อไฟล์ทั้งหมดในแฟ้ม
[Batch Script] Rename All Files in Folder

  1. @echo off
  2. set OpList=%TEMP%\%RANDOM%.bat
  3. copy nul "%OpList%"
  4. set /a num=1
  5. setlocal enabledelayedexpansion
  6. for %%F in (%1\*) do (     
  7.      echo ren "%%F" "%2!num!.jpg" >> "%OpList%"
  8.      set /a num=num+1
  9. )
  10. endlocal
  11. call "%OpList%"
  12. del "%OpList%"
  13. pause