Get current batchfile directory

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Get current batchfile directory

Message par ForumBot »

Get current batchfile directory




Source : Stack Overflow [windows]

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Get current batchfile directory

Message par ayi »

System read-only variable %CD% keeps the path of the caller of the batch, not the fichier batch location.


Vous pouvez get the name of the script batch itself as typed by the user with %0 (e.g. scripts\mybatch.bat). Parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the script batch (e.g. W:\scripts\) and %~f0 will return the full pathname (e.g. W:\scripts\mybatch.cmd).


Vous pouvez refer to other files in the same folder as the script batch by using this syntax:


CALL %0\..\SecondBatch.cmd


This can even be used in a subroutine, Echo %0 will give the call label but, echo "%~nx0" will give you the filename of the script batch.


When the %0 variable is expanded, the result is enclosed in quotation marks.


More on batch parameters.

Répondre

Revenir à « Performance & Dépannage »