Windows batch: Get the first folder name starting with some value

batchbatch filecmd.execommand linesvn

I have a batch file in which I pass the name of the folder I want to use as a argument. But now I want to make it a little smarter by letting the user specify only the first few characters of the name of the folder they'd like to pass on as an argument.

For example, when I have three folders in my current folder:

  • FolderOne
  • FolderTwo
  • AnotherFolder

Whenever I type "An" as the argument, I want the batch file to loop through the folders in the current folder and get the name of the first folder that starts with the specified string. In this case, that would be "AnotherFolder".

I know how to do it in C# but I'm new to batch files. Can someone please help me with this? I'm sorry if it's a noob question, but I really can't seem to get it working.

A little context to this script: I currently have a folder for my Projects, which contains a huge amount of folders which are all under version control (TortoiseSVN). To make things simpler for me, I'd created a batch file in which I only have to type svnlog "some folder name" to show the SVN log of that project. The current batch:

cd C:/Projects/
SET p=C:/Projects/
SET a=%1
SET pa=%p%%a%
TortoiseProc.exe /command:log /path:%pa%

Best Answer

  • Alright, I already figured it out. See the batch file below.

    cd C:/Projects/
    SET p=C:/Projects/
    SET a=%1
    for /D %%x in (%a%*) do if not defined f set "f=%%x"
    SET pa=%p%%f%
    TortoiseProc.exe /command:log /path:%pa%
    
  • Related Question