หากคุณต้องการทราบตำแหน่งปัจจุบันของไฟล์แบทช์ (และหาก Windows ของคุณไม่ใช่รุ่นที่เก่าแก่มาก) ให้พิมพ์for /?ในหน้าต่าง 'กล่อง DOS' เลื่อนลง. อ่าน.
คุณจะพบว่าตอนนี้คุณสามารถอ่าน (จากภายในไฟล์แบทช์) ตัวแปรเหล่านี้:
%0 - as the name how this batchfile was called
%~d0 - as the drive letter where this batchfile is located ('\\' in case of share)
%~p0 - as path (without the drive letter) where this batchfile is located
%~n0 - as filename (without suffix) of this batchfile
%~x0 - as filename's suffix (without filename) of this batchfile
%~a0 - as this batchfile's file attributes
%~t0 - as this batchfile's date+time
%~z0 - as this batchfile's filesize
%~dpnx0 - as this batchfile's fully qualified path+filename
[... and then some more ...]
วิธีนี้ใช้ได้หลายกรณี สมมติ batchfile mytest.batที่เรียกว่า คุณสามารถเรียกมันได้หลายวิธี:
..\..\to\mytest.bat ............................... (เส้นทางสัมพัทธ์)
d:\path\to\mytest.bat........................... (เส้นทางแบบเต็ม)
\\fileserver\sharename\mytest.bat... (เส้นทางที่แชร์จากระยะไกล)
... และคุณจะได้รับคุณค่าที่ถูกต้องในตัวแปรของคุณ
cd | sed "s/.*\\//"(นั่นทำให้การส่งออก cd (cwd) ไปยังการค้นหานิพจน์ทั่วไปและแทนที่โดยแทนที่ทุกอย่างก่อนที่จะสิ้นสุด \ ไม่มีอะไรเลย)