Get current folder name instead of current path in windows batch file

16,974

There really isn't an easy way to do it. This works in a .bat file:

for %%* in (.) do @echo %%~n*
Share:
16,974

Related videos on Youtube

Lenin Raj Rajasekaran
Author by

Lenin Raj Rajasekaran

Updated on September 18, 2022

Comments

  • Lenin Raj Rajasekaran
    Lenin Raj Rajasekaran 3 months

    Possible Duplicate:
    Get current folder name by a DOS command?

    In my batch file, I have,

    SET currentFolder=%CD%
    echo "%currentFolder%"
    

    Prints the whole directory path.

    C:\www\scritps\
    

    I need to get scripts alone

    • CharlieRB
      CharlieRB about 11 years
      Answer already here.
  • barlop
    barlop about 11 years
    that's fantastic (of course, * can be replaced with a letter), out of interest, howcome you used * ? btw, FWIW for any that don't know, # and ? work too. %%? and %%#
  • Aaron
    Aaron about 11 years
    I didn't want to use something like "x" (or another letter) because I didn't want the original poster to get confused at the "~nx" part. But you're right, you could substitute any letter there. Coding in DOS .bat is so archaic that it's sometimes difficult to see what's going on.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck almost 6 years
    This doesn't play well with directories that use . in their names. Any tips?