Trong bài viết
Batch Extract path và tên tệp từ một biến
được tìm thấy tập lệnh này để trích xuất tất cả các phần của tệp từ một biến môi trường:
@ECHO OFF
SETLOCAL
set file=C:\Users\l72rugschiri\Desktop\fs.cfg
FOR %%i IN ("%file%") DO (
ECHO filedrive=%%~di
ECHO filepath=%%~pi
ECHO filename=%%~ni
ECHO fileextension=%%~xi
)
Và danh sách đầy đủ để trích xuất thông tin từ tham số đầu tiên của .battập lệnh:
%~1 - expands %1 removing any surrounding quotes (")
%~f1 - expands %1 to a fully qualified path name
%~d1 - expands %1 to a drive letter only
%~p1 - expands %1 to a path only
%~n1 - expands %1 to a file name only
%~x1 - expands %1 to a file extension only
%~s1 - expanded path contains short names only
%~a1 - expands %1 to file attributes
%~t1 - expands %1 to date/time of file
%~z1 - expands %1 to size of file
Mà có thể được sử dụng, ví dụ:
set file=%~f1
set filepath=%~dp1
set filename=%~nx1
Hoặc như một biến FOR cục bộ:
for %%a in (..\Desktop\fs.cfg) do (
set file=%%~fa
set filepath=%%~dpa
set filename=%%~nxa
)