查找并重命名没有扩展名的文件?

时间:2021-06-03 10:32:24

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

所以,我有一堆没有扩展名的文件。我想编写一个Windows批处理脚本,它将:

  1. Find files with no extension (in a specified folder)
  2. 查找没有扩展名的文件(在指定的文件夹中)

  3. Add .bla to the end of the file name
  4. 将.bla添加到文件名的末尾

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

我是一个Windows批处理脚本菜鸟我甚至不知道从哪里开始。建议?

3 个解决方案

#1


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

对于Windows批处理文件,这将仅重命名扩展名为.bla的文件:

rename *. *.bla

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

请注意,第一个参数是星号和点:*。

The second argument is: *.bla

第二个参数是:* .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.

附加说明:相反的命令行会将所有.bla文件重命名为无扩展名文件。

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"
)

#2


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"

#3


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"

#1


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

对于Windows批处理文件,这将仅重命名扩展名为.bla的文件:

rename *. *.bla

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

请注意,第一个参数是星号和点:*。

The second argument is: *.bla

第二个参数是:* .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.

附加说明:相反的命令行会将所有.bla文件重命名为无扩展名文件。

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"
)

#2


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"

#3


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"