Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

windows - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...