Get the parent folder name and use it in CMD

I use youtube-dl to fetch videos once in a while.

I have a dopus button (MS-DOS Batch Function button) with the following code:

youtube-dl {clip}

If I have copied a URL, all I need to do to download the video is to click on the dopus button to start the download.


youtube-dl features a -o flag where you can decide what you would like to call the downloaded video file. I would like to call the file the same as the parent folder name.

So for example, if I have the directory:

Cool movie\
    Cool movie.mp4

I would like to run

youtube-dl {clip} -o "Cool movie"

So the question is, how can I get the parent folders name and use it in a batch-file button in dopus?

Just found that this will work:

for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
youtube-dl {clip} -o "%CurrDirName%-trailer"

Although I have no idea if this is the best approach though :stuck_out_tongue:

I'd recommend not using MS-DOS Batch mode at all unless you really have to. MS-DOS has all sorts of special cases with certain characters in file paths and command lines which can cause problems that are hard to understand when they happen.

You can use {sourcepath|nopath} to get the name (without path) of the current folder, and use the normal Standard Function mode to avoid all the MS-DOS issues.

Quoting the clipboard content is also a good thing to do in case there is a space in the text.

youtube-dl "{clip}" -o {sourcepath|nopath}-trailer

That should automatically quote the name if there is a space in the current folder's name, but you can also do it explicitly if that doesn't work (turning off automatic quoting to avoid ending up with two sets of quotes):

youtube-dl "{clip}" -o "{sourcepath|nopath}-trailer"


This makes more sense. I can get this to work:

youtube-dl {clip} -o {sourcepath|nopath|noterm}-trailer

but if I set the button to run as a standard dopus function it seems to run the batch in a python.exe instead of cmd.exe, which does not work. I continue to struggle with this concept.

You could run it via cmd.exe but at that point you might as well use MS-DOS Batch mode since it's more or less the same thing.

I don't know about the particular tool or running Python scripts (presumably that's what it is?) outside of command prompts so there may be other ways.