Batch file to add a file extension to all files in a directory without one

Posted on 2012-08-15
Last Modified: 2012-08-15

I have a directory on a Windows XP machine that contains hundreds of files that look like this:


Some of them have valid file extensions and some have no file extensions.  

I'd like to write a batch file that adds a .DAT file extension to all files in my directory with no extension and leave the files with an extension unchanged.

I tried this at the command line:

for %x in (*) do ren "%x" "%x.DAT"

and got this:

Can I do this with a batch file? If so, suggestions please.

Many thanks!
Question by:monkeybiz12345
    LVL 67

    Accepted Solution

    It's much easier than you would expect:
      ren *. *.dat
    will only rename files without extension. However, if you execute that twice, you will get an (stupid) error message - "The syntax of the command is incorrect.". Just ignore that, it is trying to tell you that there are no files to rename.
    LVL 11

    Expert Comment


    Nice one. To suppress the error messege, it would be better to do it like this:

        ren *. *.DAT 2>nul


    Please try this:

        @for %a in (*.*) do @if "%~xa"=="" @ren "%a" "%a.dat"

    The '@' symbols just suppress the echoing of commands to the screen and aren't strictly needed.

    In a batch file you would do it like this:

    @echo off
    for %%a in (*.*) do if "%%~xa"=="" ren "%%a" "%%a.dat"

    Open in new window


    Author Closing Comment

    ZOINKS!  Why didn't I think of that?  

    Qlemo, that's exactly what I needed.

    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.

    There are 2 things you must have in order to connect to the internet behind a router, The "Gateway IP" of the router, which is usually something like, I've seen routers with default values of:,,, …
    Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    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…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now