Code for batch renaming special file names

Time:2020-7-30

One B! A! [email protected]_ S% ා 8i 3c.rar
% es3210 ~%. TXT file
%O%%%! O! Ffi9 ~ CE $- wor8d29 document.docx
Rename.bat
Figure 1
Code for batch renaming special file names
It is required to run batch processing to remove all the numbers, Chinese characters and special characters (including spaces) in the file names of these three files, and rename them to the files containing only letters
The suffix name cannot be changed. At the same time, the batch name cannot be changed. After renaming:
basic.rar
helps.txt
oOfficeWor.docx
Rename.bat
Figure 2
Code for batch renaming special file names
Requirements: code concise, universal, efficiency is not too low.
pusofalse:

Copy codeThe code is as follows:
@echo off
for %%a in (*.*) do (
if “%%~nxa” neq “%~nx0” (
set “name=%%~na”
setlocal enabledelayedexpansion
set name1=!name!
call :lp
ren “!name!%%~xa” “!nam!%%~xa”
set “nam=”
endlocal
)
)
pause
:lp
if defined name1 (
set “var=!name1:~,1!”
if “!var!” leq “Z” (
if “!var!” geq “a” (
set “nam=!nam!!var!”
))
set “name1=!name1:~1!”
goto lp
)

 

batman:

Copy codeThe code is as follows:
@echo off
set “code=a b c d e f g h i j k l m n o p q r s t u v w x y z”
for /f “delims=” %%a in (‘dir /s /a-d /b’) do (
If “%% ~ nxa” NEQ “Rename. Bat”(
set “str=%%~na”&set “file=%%a”&set “var=%%~na”
setlocal enabledelayedexpansion
for %%i in (%code%) do set “str=!str:%%i=!”
set “str=!str: =!”&call :lp
ren “!file!” !files!%%~xa
endlocal
))
goto :eof
:lp
set /a n+=1
for /f “tokens=%n% delims=%str% ” %%a in (“%var%”) do (
if “%%a” neq “” set “files=!files!%%a”&goto lp
)

Describe the overall idea:

As you can see, the file name in the title is composed of letters + numbers + special characters + Chinese characters. It seems very complicated, but can we look at it from another angle: the file name is composed of separator + letter (look at the component separator for numbers, special characters and Chinese characters). Is it much simpler? Ha ha, the trouble is still behind…
So, we have another problem: numbers and special characters are limited, but Chinese characters are a problem. We can’t write all the Chinese characters after delims = (besides, this length is limited)? It’s impossible. What to do? Therefore, we can first file names are not letters of the characters, how to mention? This is where variable substitution is used, replacing all 26 letters with blanks.
Some people will say that there is a problem with efficiency. There is a problem of efficiency here. But let’s think about the efficiency of character by character judgment. Why? Suppose a file name has more than ten a’s. If you use character by character, you have to judge more than ten times, and the variable replacement is done only once.
After this replacement, whether only the characters that are not letters are left in the string. Now just use it as a separator (don’t forget to add spaces) to extract all the letters in the file name, and the rest of the work is just technical processing.

dishuo:

According to the idea of pusofalse, the most convenient tool for extracting characters must be regular expressions. The only regular support for batch commands is findstr. Can we use it to extract characters?
Test successful!
Meet the requirements: concise, universal, efficiency is not too low.
Features: it has strong generality. You only need to modify the regular parameters of findstr to realize various functions. Here are some examples.
“[^ -! – ~ 0-9a-z]” preserves full width characters (Chinese, full angle punctuation, etc.)
[a-z0-9] “retains English letters and numbers

Copy codeThe code is as follows:
@echo off&cls
for /f “tokens=*” %%i in (‘dir /b/a-d “*.*”‘) do (
if “%%~nxsi” neq “%~nxs0” (
Set “old file name =%% ~ nxsi” & set “file name string =%% ~ Ni” & set “new file name =” & set “counter = 0”
del ~filenamechar.lst /q>nul 2>&1
setlocal enabledelayedexpansion
call :split
for /f “tokens=*” %%n in (‘findstr “[a-z]” ~ filenamechar.lst ‘) do set “new file name =! New file name!%% n”
If “! New file name!” NEQ “(
Echo Ren! Old file name! New file name!% ~ Xi
) else (
Echo ^ (the file “! Old file name!” does not contain letters and cannot be renamed. ^)
)
endlocal
)
)
del ~filenamechar.lst /q>nul 2>&1
pause&goto :eof

:split
If “! File name string: ~% counter%, 1!” NEQ “(
If “! File name string: ~% counter%, 1! NEQ” “echo! File name string: ~% counter%, 1! > >~ filenamechar.lst
set /a counter+=1
goto split
)
goto :eof