Thứ Bảy, 18 tháng 7, 2015

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" ??

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 %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 variable
for %%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 : ~n

for %i in (*.*) do echo %~ni
Extract the file extension without the filename : ~x
for %i in (*.*) do echo %~xi
Extract the file attribute : ~a
for %i in (*.*) do echo %~ai
Extract the file time : ~t
for %i in (*.*) do echo %~ti
Extract the drive only : ~d
for %i in (*.*) do echo %~di
Extract the path only : ~p
for %i in (*.*) do echo %~pi  
Extract the complete name : ~s
for %i in (*.*) do echo %~si  
Extract the file length (in bytes) : ~z
for %i in (*.*) do echo %~zi  
%~$PATH:i searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string.
for %i in (java.exe) do @echo. %~$PATH:i  
To remove quotes
>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%
The path (without drive) where the script is : ~p0
set BAT_HOME=%~p0
echo %BAT_HOME%
cd %BAT_HOME%
The drive where the script is : ~d0
set BAT_DRIVE=%~d0
echo %BAT_DRIVE%
The complete script name : ~s0
set BAT_PATH=%~s0
echo %BAT_PATH%
The script name only (as called with or without the extension): %0
set BAT_NAME=%0
echo %BAT_NAME%

Nhãn:

0 Nhận xét:

Đăng nhận xét

Đăng ký Đăng Nhận xét [Atom]

<< Trang chủ