Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and rename files with no extension?

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?

like image 638
Glenn Avatar asked Jun 22 '09 03:06

Glenn


People also ask

How do I name a file without an extension?

To create a file without an extension with Notepad, use quotation marks. The quotation marks ensure the integrity of the file name chosen without an extension. The file is saved with a name and a file type of "file" which has no extension.

How do I add an extension without an extension?

Go to “File > Save.” Or simply press Ctrl + S keys. Type the name of the file under inverted commas. Let's say if you want to save the file with the name sample file, type “sample file” and click on the Save button. After that, Windows will save the file with no extension.

How do I rename multiple files without sequential numbers?

Select multiple files in a folder. To do so, press and hold down the CTRL key while you are clicking files. After you select the files, press F2. Type the new name, and then press ENTER.

How do I rename files in bulk without brackets?

In the File Explorer window, select all files, right-click and select rename. Windows will select the starting number as the number supplied between the round brackets so name the file using a number that is 1 digit more than the number of digits required.


2 Answers

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"
)
like image 147
Wadih M. Avatar answered Sep 19 '22 18:09

Wadih M.


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"
like image 38
Helen Avatar answered Sep 20 '22 18:09

Helen