Lay ten file: Thao tac voi file batch DOS
Can a DOS batch file determine its own file name?
For example, if I run the batch file C:\Temp\myScript.bat , is there a command within myScript.bat that can determine the string "myScript.bat" ??
For example, if I run the batch file C:\Temp\myScript.bat , is there a command within myScript.bat that can determine the string "myScript.bat" ??
ANSWER
Using the following script, based on SLaks answer, I determined that the correct answer is:
echo The name of this file is: %~n0%~x0
echo The name of this file is: %~nx0
And here is my test script:@echo off
echo %0
echo %~0
echo %n0
echo %x0
echo %~n0
echo %dp0
echo %~dp0
pause
What I find interesting is that %nx0 won't work, given that we know the '~' char usually is used to strip/trim quotes off of a variable.----------------------
You can only extract path and filename from (1) a parameter of the BAT itself
in
%~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
And then try the following:
Either pass the string to be parsed as a parameter to a CALL
---------------------------------------------------------------%1
, or (2) the parameter of a CALL %1
or (3) a local FOR variable %%a
.in
HELP CALL
or HELP FOR
you may find more detailed information:%~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
And then try the following:
Either pass the string to be parsed as a parameter to a CALL
call :setfile ..\Desktop\fs.cfg
echo %file% = %filepath% + %filename%
goto :eof
:setfile
set file=%~f1
set filepath=%~dp1
set filename=%~nx1
goto :eof
or the equivalent, pass the filename as a local FOR variablefor %%a in (..\Desktop\fs.cfg) do (
set file=%%~fa
set filepath=%%~dpa
set filename=%%~nxa
)
echo %file% = %filepath% + %filename%
@ECHO OFF
SETLOCAL
set file=C:\Users\l72rugschiri\Desktop\fs.cfg
FOR /f %%i IN ("%file%") DO (
ECHO filedrive=%%~di
ECHO filepath=%%~pi
ECHO filename=%%~ni
ECHO fileextension=%%~xi
)
Not really sure what you mean by no "function"Obviously, change
ECHO
to SET
to set the variables rather thon ECHOing them...See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx for a full list.
-------------------------------
Filename parsing in batch file and more idiomsTag(s): Misc Prog HowTo
In the following examples, we iterate a list of files and use the idiom ~[idiom] to extract certain part of a given filename. Extract the filename without the extension : ~nfor %i in (*.*) do echo %~ni
for %i in (*.*) do echo %~xi
for %i in (*.*) do echo %~ai
for %i in (*.*) do echo %~ti
for %i in (*.*) do echo %~di
for %i in (*.*) do echo %~pi
for %i in (*.*) do echo %~si
for %i in (*.*) do echo %~zi
for %i in (java.exe) do @echo. %~$PATH:i
>for %i in ("real'howto") do @echo. %i "real'howto" >for %i in ("real'howto") do @echo. %~i real'howto
The path (with drive) where the script is : ~dp0
set BAT_HOME=%~dp0 echo %BAT_HOME% cd %BAT_HOME%
set BAT_HOME=%~p0 echo %BAT_HOME% cd %BAT_HOME%
set BAT_DRIVE=%~d0 echo %BAT_DRIVE%
set BAT_PATH=%~s0 echo %BAT_PATH%
set BAT_NAME=%0 echo %BAT_NAME%
Nhãn: Mẹo
0 Nhận xét:
Đăng nhận xét
Đăng ký Đăng Nhận xét [Atom]
<< Trang chủ