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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1025410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:31:42  来源:igfitidea点击:

Find and rename files with no extension?

windowsbatch-filescriptingrename

提问by Glenn

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. Add .bla to the end of the file name
  1. 查找没有扩展名的文件(在指定文件夹中)
  2. 将 .bla 添加到文件名的末尾

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

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

回答by Wadih M.

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:

编辑2:

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

回答by Helen

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"

回答by akf

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"