I have been researching and trying without success to get the current directory name into a variable such as %CurrDirNam% for creating a text file with the same folder name something the third example below which works, however when run, it overwrites the existing file and it's contents, so I modified the bottom line to check for the file's existence. Set DirName=%cd% For %%A in ("%dirname%") do ( Set Name=%%~nxA ) IF NOT EXIST %Name%.txt Echo %Dirname% > %DirName%\%Name%.txt Rem ---OOPS sorry I do not know how to re-format the code with CRLF and indents. I'm new at this...

  • The variable I found is %cd%. However, when trying to create the text file, an 'access denied' message is returned, even when run as administrator. the batch is Set DirName=%cd% -Followed by- Echo %Dirname% > %DirName%.txt. The use of the variable name after the redirection is what causes the message. – Joanne Mar 19 '19 at 6:16
  • What is the full path to the directory you are currently in when running the command? – DavidPostill Mar 19 '19 at 7:32
  • The drive and folder path is C:\Temp so the text file I hope to have created is temp.txt – Joanne Mar 19 '19 at 7:48
  • Echo %Dirname% > %DirName%.txt. The first Dirname is not the same as the second DirName (look at the case of the letters) – DavidPostill Mar 19 '19 at 8:03
  • @DavidPostill It's Windows, so not case sensitive. %Dirname% is equal to %DirName% – Berend Mar 19 '19 at 8:09

If you run this

Set DirName=%cd% 
Echo %Dirname% > %DirName%.txt

from C:\Temp, it will expand to

Set DirName=C:\Temp 
Echo C:\Temp > C:\Temp.txt

This will fail if you don't have write permissions on C:\ and would explain the Access Denied message you get.

If you want to create a file in C:\Temp, use something like this:

Set DirName=%cd% 
Echo %Dirname% > %DirName%\test.txt

If the name of the file must match the directory name, use this trick:

Set DirName=%cd%
For %%A in ("%dirname%") do (
    Set Name=%%~nxA
Echo %Dirname% > %DirName%\%Name%.txt
  • It should probably be noted that the user might not have a c:\temp directory and/or write access to it. If using Windows temp due, it is best to use %TEMP% variable for a place where to put temporary files – Gnudiff Mar 20 '19 at 9:07
  • @Gnudiff No one said anything about temporary files? – Berend Mar 20 '19 at 9:09
  • that's right, I missed that you only used temp as an example – Gnudiff Mar 20 '19 at 9:10
  • @Gnudiff I just copied that from one of the OP's comments – Berend Mar 20 '19 at 9:11

I tested this on my Windows 10 system and it worked with no problem.

>path.txt echo %cd%

I ran this in a batch script while in the directory I wanted the path of. In my case, I only wanted the path to create a URL from it I found that by using >url.txt echo %cd:\=/% it also takes care of all the backslashes that needed to be forward slashes in the URL and saved me the trouble

Only step left is a little editing on the url.txt file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.