Code for renaming special file names in Batch Processing

Source: Internet
Author: User

One B! A! 1235 @ _ s % # 8i 3 c.rar
H ^ e12 0 ~ % L %! P! @. S321-file .txt
% O %! O! Ffi9 ~ Ce $-Wor8d29 document .docx
Rename. bat
Figure 1:

You must run batch processing to remove all the numbers, Chinese characters, and special characters (including spaces) in the names of these three file names and rename them to files with only letters,
The suffix name cannot be changed, and the batch processing name cannot be changed. after renaming:
Basic.rar
Helps.txt
OOfficeWor.docx
Rename. bat
Figure 2:

Requirement: the code is concise, common, and inefficient.
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" "(
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:

We can see that the file name in the question is composed of letters, numbers, special characters, and Chinese characters. It looks complicated, but can we look at it from another angle: the file name is composed of delimiters and letters (all numbers, special characters, and Chinese characters are considered as delimiters). Is it much simpler? haha, the trouble is still coming...
Now we have another problem: Numbers and special characters are limited, but Chinese characters are troublesome, we cannot write all Chinese characters After delims = (and the length is limited )? This is impossible. What should we do? Therefore, we can put forward all the characters in the file name that are not letters. How can we raise them? This requires variable replacement. replace all 26 letters with null.
Some people may say that there is a problem with efficiency. There is indeed a problem with efficiency here, but it is still a bit more efficient to compare the character-by-character judgment. Why? Assume that a file name contains more than a dozen characters, and the variable is replaced only once.
Whether the string after replacement is only a character other than a letter is left. Now you just need to 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 only technical processing.

Dishuo:

According to pusofalse, the most convenient tool for extracting characters must be a regular expression. The only RegEx supported by the batch processing command is findstr. Can I use it for extraction?
Test successful!
Meeting requirements: concise, General, and inefficient.
Features: strong versatility. You only need to modify the regular parameters of findstr to implement various functions. For example.
"[^ -! -~ 0-9a-z] "Reserved full-width characters (Chinese characters, full-width punctuation marks, etc)
"[A-z0-9]" Reserved letters and numbersCopy 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 ^ (File "! Old File name! "It 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.