Rename all files to lowercase, replace spaces

2019-05-30 19:17发布

问题:

In a Windows command prompt, how can I rename all the files to lower case and remove all spaces?

回答1:

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.



回答2:

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


回答3:

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