开发者

Rename all files to lowercase, replace spaces

In a Windows command prompt, how can I rename all the files to lower case and remove a开发者_开发问答ll spaces?


Make a batch file

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ( ' dir /b /a-d *.* ') do (
set name="%%i"
set newname=!name: =!
rename "%%i" !newname!
)

NOTE: Run under a test directory and see if you have the expected result. I have not tested it.

EDIT: Forgot to say this will only remove the spaces.


I used this batch file to rename all folders and subfolders to lowercase names:

@ECHO OFF
CALL:GETDIRS

:GETDIRS
FOR /F "delims=" %%s IN ('DIR /B /L /AD') DO (
    RENAME "%%s" "%%s"
    CD "%%s"
    CALL:GETDIRS
    CD ..
)
GOTO:EOF


To make the trick of "lowercase" and "remove spaces" ...

In the given solution, in the 'dir' statement, use also "/l"

The /L statement in dir forces to lowercase the filenames in the result.

As "Windows-RENAME" command, if you use the "same" filename, it will note convert from uppercase to lowercase.

ren XPTO.TXT xpto.txt

The result will always be : XPTO.TXT

To 'bypass' this, we use the ephemeral technique: move old to temp, then -> move temp to new

Then the solution would be:

@echo off
if exist temporaryfilenametorename del temporaryfilenametorename /f/q
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir *.csv /l /b /a-d') do (
set name="%%i"
set newname=!name: =!
rename "%%i" temporaryfilenametorename
rename temporaryfilenametorename !newname!
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜