views:

631

answers:

4

So, I've got a bunch of files with no extension. I want to write a windows batch script that will:

  1. Find files with no extension (in a specified folder)
  2. Add .bla to the end of the file name

I'm such a windows batch script noob I don't even know where to start. Suggestions?

+10  A: 

For windows batch files, this will rename only files with no extension to the .bla extension:

rename *. *.bla

Notice the first argument is a star and a dot: *.

The second argument is: *.bla

The start dot (*.) combination represents files with no extensions in this context.

Before:

06/21/2009  11:57 PM                 6 test
06/21/2009  11:57 PM                 7 test.exe
06/21/2009  11:57 PM                 7 test2

After:

06/21/2009  11:57 PM                 6 test.bla
06/21/2009  11:57 PM                 7 test.exe
06/21/2009  11:57 PM                 7 test2.bla

Additional note: The opposite commandline would rename all .bla files into no extension files.

EDIT:

For recursively renaming files with no extension across subdirectories (doesn't support spaces in paths):

@echo off
FOR /F %%i in ('dir /b/s/A-d') DO (
  if "%%~xi" == "" rename "%%~fi" "%%~ni.bla"
)

EDIT2:

For recursively renaming files with no extension across subdirectories (supports spaces in path):

@echo off
for /f "tokens=* delims= " %%i in ('dir /b/s/A-d') DO (
  if "%%~xi" == "" rename "%%~fi" "%%~ni.bla"
)
Wadih M.
I considered that, but there isn't even a dot at the end of the name... will this still work?
Alan
Also, how would I include subfolders?
Alan
@Alan - Yes it will work, and how many subfolders are we talking about?
JFV
I tested it, start dot represents files with no extensions.
Wadih M.
@JFV Just one level deep, but a bunch of them.
Alan
Then just use the same syntax, but define the new directory:ren *. *.blaren <path to new dir>\*. *.bla
JFV
Rather, there are a bunch of folders one level deep...so i need to figure out something that will process them all. Hmmm...
Alan
In case you're still interested... I've done an edit to recursively do that across all subfolders.
Wadih M.
Re-edited it to change "echo" to "rename"
Wadih M.
another edit for paths with spaces...
Wadih M.
Thanks a bunch, this is exactly what I needed!
Alan
+3  A: 

to do this in subdirectories use this:

 for /f %a in ('dir /b /ad /s') do rename %a\*. *.bla

if you are using this in a batch file, you need to double the '%'

 for /f %%a in ('dir /b /ad /s') do rename %%a\*. *.bla

edit:

and if you have spaces in your directory names, you can try this (batch version):

 for /f "tokens=* delims= " %%a in ('dir /b /ad /s') do rename "%%a\*." "*.bla"
akf
nice! (15 char limit)
Charlie Somerville
Minor issue here - not working for directories with spaces...otherwise looking good
Alan
+1  A: 

Here's another possible command for renaming files with no extensions recursively (assuming that file paths don't contain spaces):

for /f %i in ('dir *. /b /s /a-d') do rename "%~fi" "%~ni.bla"

Batch version (with doubled %):

@echo off
for /f %%i in ('dir *. /b /s /a-d') do (
   rename "%%~fi" "%%~ni.bla"
)


If file or folder names contain spaces, use this command instead:

for /f "tokens=* delims= " %i in ('dir *. /b /s /a-d') do rename "%~fi" "%~ni.bla"

Batch version:

@echo off
for /f "tokens=* delims= " %%i in ('dir *. /b /s /a-d') do (
   rename "%%~fi" "%%~ni.bla"
)


Edit: here's even shorter one-liner that supports spaces in paths:

for /r %i in (*.) do ren "%~fi" "%~ni.bla"

Batch version:

@for /r %%i in (*.) do ren "%%~fi" "%%~ni.bla"
Helen
A: 

deleted - this is a question, not an answer.

skris88