I need to grab the folder name of a currently executing batch file. I have been trying to loop over the current directory using the following syntax (which is wrong at present):
set mydir = %~p0 for /F 'delims=\' %i IN (%mydir%) DO @echo %i
Couple of issues in that I cannot seem to pass the ‘mydir’ variable value in as the search string. It only seems to work if I pass in commands; I have the syntax wrong and cannot work out why.
My thinking was to loop over the folder string with a ‘\’ delimiter but this is causing problems too. If I set a variable on each loop then the last value set will be the current folder name. For example, given the following path:
C:\Folder1\Folder2\Folder3\Archive.bat
I would expect to parse out the value ‘Folder3’.
I need to parse that value out as its name will be part of another folder I am going to create further down in the batch file.
Many thanks if anyone can help. I may be barking up the wrong tree completely so any other approaches would be greatly received also.
You were pretty close to it 🙂 This should work:
For some reason the for command doesn’t like ‘\’ as a delimiter, so I converted all ‘\’ to ‘;’ first (
SET mydir=%mydir:\=;%)