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