QuickTip: Windows Shell – Dealing with files without extension

Since files in windows are typically associated with corresponding applications by the file type (extension), it is so difficult to deal with these files without extensions. You created some text files (may be a batch job?) without any extension and you want to rename them to .txt, so you can open them in Text editor. To do this use the following syntax:

ren *. *.txt
(syntax is REN[ame] <from> <to>)

Notice the *. without any extension. This makes it pull up all files without extension.

dir *.
Of course the above DIR command will list all the directories as well, as most of them don’t have any extensions. To be able to list only files, use the below command:

dir /a-d *.

DIR command comes with several commandline options. One of them is /a for attributes. /aD is to list only directories. /a-D (i.e, a (minus)D ) does the opposite – lists only files. (I showed D in upper case just to showcase it, but case doesn’t matter).

Back to renaming files, you can use it to remove extensions too, but there are some interesting twists too. Suppose you had a file,


Notice there is .a.sql

If I rename this file, using the *.

ren fix_10.2.a.sql *.

it simply removes the .SQL at the end and the file becomes a .A type file (ren fix_10.2.a)!!! I expected to find it in dir *. – but it didn’t find. I had to do a dir *.a to find it.

And, how do you include this in a script to list/process those files?

On command line, try:
FOR %F in (*.) DO (echo %~nxF)

In a script (batch file) you will need to double the %. And you can include any # of commands inside the parentheses after DO.

Note, it automatically excludes sub-directories here!

~nF there basically says, take the name portion (~n) of the file in variable F.

Batch file syntax is very unique (weird sometimes). I often work on *nix and I can not help but notice how structured a shell script is compared to batch file. I don’t see a rhyme or reason why it is done a particular way in Windows scripting, but works!! That’s probably because Windows (DOS) batch files grew over a time period, borrowing from all sources, including shell scripts. I will touch base on these later.


Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s