Environment: Windows 10
Question: If possible - how do we set the name of the top-most folder on a drive as a variable?

We receive a disc from a third-party that contains updates to a system we use - on the disc is a folder with the current version's title (e.g. blahblahSystemblah-2018October-finalblah) and it contains all of the data in that version. After we put the disc in the system we use robocopy to update the server with the changes, and then users who run the system after that are updated by the server. Our current issues are that we have to set the folder name manually since robocopy can't (to my knowledge) use a wildcard in it's source, the folder name is important elsewhere so we cannot simply get them to put the contents directly on the disc, and ideally we'd like to not have to move the batch file before running it (using current directory would be easy, but as I said: the folder name is important, and we're trying to keep any manual input/effort from users to a bare minimum).

The relevant part of the script would look like this:

@echo off
for (however you can get the top folder name automatically) do (set VRSN=however)

robocopy "D:\VRSN" "C:\local\directory" /e /mir /r:0 /w:0 /log:"C:\log\directory.txt"

The closest thing I could find to getting there is this piece of script, where "knownfile" is the name of a file that will always be in the folder:

setlocal enabledelayedexpansion

cd /d "D:\"

for /R %%G in (knownfile) do (
    set "var1=%%~dpG"
    set "var1=!var1:%CD%=!"
    if not "!var1!"=="!prev!" (
        if "!var1!"=="\" (echo .\) else echo(!var1:~1,-1!
    set "prev=!var1!"

-- but this script spits out more than just the folder with that known file, and it also clips the first character of the folder name. I'll be the first to admit that I do not fully understand the syntax or where it's going wrong, so if anyone could point me in a better direction or help me refine the above code to only return that one folder, it would be very much appreciated.


You can use something as simple as:

@ECHO off
set knownfile=D:\Folder\File.txt

for /F "delims=\ tokens=2" %%G in ("%knownfile%") do (
    set prev=.\%%G

ECHO %prev%


where the knownfile environment variable is the full path with no quotes. You don't need to enable Delayed Expansion for this solution or mess with the CD environment variable.

With for /R, you're walking through a directory tree, whereas for /F is simply processing a string and splitting it at the \ character.

tokens=1 tells it to use the first portion of the string. If you wanted to use any folders after that, you could do that by using tokens=1,2,3 and then set prev=.\%%G\%%H\%%I

If you wanted to get the drive letter, instead of using .\, you could use set prev=%%~dG\%%~G

Further reading:

| improve this answer | |
  • I've been unable to get this to work for me after playing with it for a while - when you say the full path, do you mean I have to include the part that's going to be changing from month-to-month? I could get around this by using CD /d "D:\*\" before your code block, but even when I can get this to return something for %prev%, it ends up being a line of text from the actual known file being referenced (if I use an .inf file, for instance). – mael' Oct 12 '18 at 14:10
  • @mael' Are you including the " around "%knownfile%"? I've updated the answer with a screenshot of my output. If you don't include the " around the variable, it will interpret it as a file-set and not a string. I did accidentally put the wrong token, it was supposed to be 2. For some reason that worked for me the first time I tried it. – Worthwelle Oct 12 '18 at 17:28
  • thanks for your help - I did misinterpret what you wrote about the quotes when you said the variable can't have them (I removed them from the ("%knownfile%") part as well); the issue is still not knowing what the Folder name will be from the D:\Folder\File.txt line, since that's what is changing from month to month. Is there anyway to use a wildcard in this scenario? The ways I've tried just return the * for %prev%. – mael' Oct 12 '18 at 18:54
  • @mael' Yes, you can use set knownfile=%~1 to have the batch file use whatever file is provided as an argument. You can then drag the file onto the batch file to run the batch file with that as the argument. Read this for more information. – Worthwelle Oct 12 '18 at 18:59
  • I was missing the simplest addition to get your solution to work for me. I ended up not even needing a known sub-file, all I needed was the drive letter. I used cd /d "D:\*\" since I could use a wildcard when setting the working directory, then I just set knownfile=%cd% and that did it. /facepalm thanks for your help! – mael' Oct 15 '18 at 20:14

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.