简体   繁体   中英

Rename all files to lowercase, replace spaces

在Windows命令提示符下,如何将所有文件重命名为小写并删除所有空格?

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

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