简体   繁体   中英

remove strange file extensions

I have a folder which contains some files with names as given below

0003.4b3d943b8df71af248d12f8b2e7a224a
0004.1874ab60c71f0b31b580f313a3f6e777
0005.1f42bb885de0ef7fc5cd09d34dc2ba54
0006.7a32642f8c22bbeb85d6c3b5f3890a2c
0007.859c901719011d56f8b652ea071c1f8b
.
.
.

I want to rename these files by removing these extensions and need only file name part ie.

0003
0004
0005
0006
.
.
.
.

How to achieve this using a script?

If all the files in the folder are to be renamed, then all you need is:

ren * *.

See How does the Windows RENAME command interpret wildcards? for more info.

If there are also files in the folder that should not be renamed (files with "normal" extensions), then the following will work:

for /f "eol=; delims=" %%F in ('dir /b /a-d *^|findstr /r "\.................................$"') do @ren "%%F" *.

The \\. matches the dot, and the 32 . match the 32 characters of the extension. The $ anchors the regex match to the end of the line.

Change each %%F to %F if you want to run the command directly from the command prompt instead of from within a batch script.

The line you are looking for is

for %%I in (d:\temp\*) DO ren "%%I" "%%~nI"

Where d:\\temp is the location of the files you want to rename. Note that your batch file has to be in a different folder, else you will rename it as well. Also note that this will rename all the files in the target folder.

try this:

for /f "delims=" %%i in ('dir /a-d /b') do echo ren "%%~fi" "%%~ni"

look at the output and remove the echo , if it is OK.

使用这样的东西

for /f "delims=" %%A in ('dir /a:-d /b /o:n') do echo %%~nA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM