Windows batchfile tips and tricks: Difference between revisions
From WickyWiki
mNo edit summary |
mNo edit summary |
||
| Line 46: | Line 46: | ||
| filesize=101 bytes | | filesize=101 bytes | ||
|- | |- | ||
| D:\path\to> | | D:\path\to> | ||
|} | |} | ||
Revision as of 14:20, 25 April 2020
Note: the %% and %~ notation is used within a batchfile and will not work on the DOS prompt.
This will make your DOS batch file ask for administrator rights. It creates and uses a small Visual basic script.
Batchfile information
Within a batchfile the '%0' parameter will contain the full path and name of the batchfile.
@echo off set fullpath=%~dp0 for %%i in ( %0 ) do set basefile=%%~ni for %%i in ( %0 ) do set basepath=%%~pi for %%i in ( %0 ) do set basedrive=%%~di for %%i in ( %0 ) do set filesize=%%~zi echo batchfile=%0 echo fullpath=%fullpath% echo basedrive=%basedrive% echo basepath=%basepath% echo basefile=%basefile% echo filesize=%filesize% bytes
| D:\path\to> example.bat |
| batchfile=D:\path\to\example.bat |
| fullpath=D:\path\to\ |
| basedrive=D: |
| basepath=\path\to\ |
| basefile=example |
| filetimestamp=/=. |
| filesize=101 bytes |
| D:\path\to> |
In general:
| Use | Meaning |
|---|---|
| %~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 |
Timestamp
@echo off
call :get_timestamp
echo timestamp=%timestamp%
goto :EOF
:get_timestamp
rem timestamp YYYYMMDD-HHMMSS (werkt alleen in batchfile)
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
rem set "timestamp=%YYYY%.%MM%.%DD%-%HH%.%Min%.%Sec%"
set "timestamp=%YYYY%.%MM%.%DD%-%HH%.%Min%"
goto :EOF
| timestamp=2020.04.25-11.53 |