Listing Files without thier extensions batch

February 14, 2011 at 00:20:02
Specs: Windows XP
Im using a batch file so that I can generate a text file that lists all the files in a folder. I just right click on the folder and click "generate" (I have added a context menu to the right click menu that executes the .bat file)
I'm working with these commands:

cd %1
dir /a /b /-p /o:gen >"C:\WINDOWS\Temp\filelisting.txt"
copy "C:\WINDOWS\Temp\filelisting.txt" %1
del "C:\WINDOWS\Temp\filelisting.txt"

That works perfectly, and lists all the files that are in the folder and lists them in a text file e.g.


but I was wondering if the list can be made without any file extensions included/listed, e.g.


I know that *.* will exclude extensions normally in DOS, but it doesnt seem to work anywhere in the above command.

Can anyone please assist? I have been stuck on this one trying to get it to work.

See More: Listing Files without thier extensions batch

Report •

February 14, 2011 at 01:06:19
wheres the part of the code that generates the filenames? i think it should be in there.

Report •

February 14, 2011 at 01:09:32
That small code above generates the list, the hard part is generating the list without the filename extensions.

Report •

February 14, 2011 at 02:51:16
@echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt

Note for D_pikster:

Please, avoid to post if you are not sure about what you want to suggest. Here people are looking for effective solutions not foggy advices. Obviously you can post whatever you like/feel but what I say is matter of courtesy to OPs.

Report •

Related Solutions

February 14, 2011 at 03:22:58
IVO that is awesome! Thank you so much! I was just fiddling around with this:

[code]@FOR /f "delims=" %%? IN ('DIR/b *.frm') DO @ECHO/%%~n? >>d:\listfrm.txt

But it wouldnt work as effective as your code, yours is perfect, thank you again!!

Report •

February 14, 2011 at 03:25:06
Doesn't this forum accept [code] tags?

Report •

February 14, 2011 at 03:34:41
One other quick thing, is it possible to exclude certain file names/extensions from being listed, for example, when I generate my lists with the above code (these are image files .bmp) because Windows makes preview icons in folders (thumbnails) this gets listed in the generated text file e.g. Thumbs.db (or in the case of the above code just "Thumbs" without the .db extension. As Thumbs.db is irrelevant to the file list it would be good not to have it listed in the generated text file.

Does the /exclude command have any effect? \db\

Report •

February 14, 2011 at 03:48:05
@echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir /A-D /B /O:GEN') do (
  if /I not "%%~xj"==".db" echo %%~nj >> filelisting.txt

Unfortunately /exclude switch doesn't apply to dir command so you have to customize the script according to your purpose.

Report •

February 14, 2011 at 04:05:25
IVO that is simply superb!

That tag:

do (
if /I not "%%~xj"==".db"

How does that work to exclude the .db (sorry for the dumb question but I would love to know how that line of code works)? I never even knew about that type of code thats all, seems like it could be handy for other functions as well.

One thing I have just also realised, and I have absolutely no idea how to solve is that after each filename listed there is a space, is it at all possible to have filenames listed without a space next to it?

Report •

February 14, 2011 at 05:19:43
Worked it out, this section of code:

do echo %%~nj >>

Remove the space between "nj" and the ">>"

So it reads:

do echo %%~nj>>

Hope this helps anyone else out trying to find a similar solution.

Thanks again IVO, really appreciate the help!

Report •

February 14, 2011 at 06:46:27
About the if /I not...statement...

that is just one of the multiple instances the if may be tailored to fit conditional purposes; the statement is overcoded for sake of safety, /I to ignore up/lowercase in the strings to be compared ".db" and "%%~xj" i.e. the extension of the current file extracted by the internal variable modifier ~x (~n catches the file name, ~t the timestamp...).

To know more about batch commands at prompt type the command code with the /? switch (e.g. IF /?, FOR /? in some case with | more to read page by page: for /? | more).

I suggest what above since my script is actually a compound statement joining for and if and I assume you may learn better by reading the quick on-lime help than my raw English. Anyway if you need any support contact me freely by a private message.

Report •

February 15, 2011 at 01:22:06
Thank you very much, I'm going to do a bit more research into this.

Report •

February 15, 2011 at 15:07:28
@D_Pikster .

There should not be any further part of the code. He added the batch file to the right click menu of all folders and then executing the batch file by just right clicking on folders.

If a batch file is executed by this way then %1 variable is the full path of that folder.

you can add a batch script or something else to any folder/filetype in windows.

1) create a reg key with any name here. HKEY_CLASSES_ROOT\Directory\shell
2) suppose you named the key as PKSTR, in right pane you will see a vlaue "Default", Put the text you want to appear in right click. if you put & before any charector that will be supported by keyboard.
3) now create another key under the key you just created, it would be like HKEY_CLASSES_ROOT\Directory\shell\PKSTR\command
4) Now in right side of command key you will see a default "Value". Put the command line / exe path or whatever you want here.

Right click on folders and you will see the result. Backup the registry to make sure you wont break anything

Subhash Chandra.

Report •

Ask Question