One B! A! [email protected]_ S% ා 8i 3c.rar
% es3210 ~%. TXT file
%O%%%! O! Ffi9 ~ CE $- wor8d29 document.docx
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:
Requirements: code concise, universal, efficiency is not too low.
for %%a in (*.*) do (
if “%%~nxa” neq “%~nx0” (
ren “!name!%%~xa” “!nam!%%~xa”
if defined name1 (
if “!var!” leq “Z” (
if “!var!” geq “a” (
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”
for %%i in (%code%) do set “str=!str:%%i=!”
set “str=!str: =!”&call :lp
ren “!file!” !files!%%~xa
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.
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?
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
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
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. ^)
del ~filenamechar.lst /q>nul 2>&1
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