Find root name of file in DOS

Posted on 2010-01-07
Last Modified: 2012-05-08
How to get the root name of a file, using dos scripting?
Question by:rsolomon
    LVL 33

    Accepted Solution

    If by root name you mean the part before the .extension:

    @echo off
     for %%F in (*.txt) do @echo  %%~nF
    LVL 33

    Expert Comment

    I used  *.txt  as a generic filespec, but it can by any filespec or a specific file.
    LVL 33

    Expert Comment

    Note that in a batch file, you must double-up the % symbols as I did above.  If you want to run that straight from a command line (no batch) then use single percents:

    for %F in (*.txt) do @echo  %~nF

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now