Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rename a special file name using batch processing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to rename special files using batch processing, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

A B! a! 1235@_ s%#8i three c.rar

h^e12 0~%l%! p!@. s321-File.txt

%o%%% ! O! ffi9~ ce$- Wor8d29 document.docx

Rename.bat

Figure 1:

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 files containing only letters, but

The suffix name cannot be changed, and the batch name cannot be changed. After renaming:

basic.rar

helps.txt

oOfficeWor.docx

Rename.bat

Figure 2:

Requirements: code concise, generic, not too inefficient.

pusofalse:

The 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:

The 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:

You see that the file name in the question is composed of letters + numbers + special characters + Chinese characters. It looks very complicated, but we can look at it from another angle: the file name is composed of separators + letters (numbers, special characters, and Chinese characters are all regarded as separators). Is it much simpler? Hehe, the trouble is still behind.

So, we have a problem again: numbers, special characters are limited, but Chinese characters are troublesome. We can't write all Chinese characters after delims=(and this length is limited), can we? This is impossible, what should we do? So, we can first file name is not a letter of the characters all proposed, how to mention? This involves variable substitution, replacing all 26 letters with blanks.

Some people will say that there is a problem with efficiency. There is indeed an efficiency problem here, but let's think about comparing the efficiency of character-by-character judgment. Why? Suppose a file name has more than a dozen a, with character by character to judge more than a dozen times, and variable replacement only once to get done.

After such substitution, whether only characters that are not letters are left in the string. Now just use it as a delimiter (don't forget to add spaces) to extract all the letters in the file name, and the rest is just technical processing.

dishuo:

According to the idea of pusofa mottled bamboo, the most convenient tool for extracting characters must be regular expressions. The only batch command that supports regular expressions is findstr. Can you use it to extract it?

The test was successful!

Meet the requirements: concise, versatile, not too inefficient.

Features: Very versatile, only need to modify the regular parameters of findstr to achieve various functions, to give a few examples.

"[^-!-~ 0-9a-z]"Keep full angle characters (Chinese, full angle punctuation, etc.)

"[a-z0-9]" Keep English letters and numbers

The code is as follows:

@echo off&cls

for /f "tokens=*" %%i in ('dir /b/a-d "*.* "') do (

if "%%~nxsi" neq "%~nxs0" (

set "old filename =%%~nxsi"&set "filename string =%%~ni"&set "new filename ="&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 filename =! New file name!%% n"

if "! New file name! " neq "" (

echo ren ! Old file name! ! New file name!%%~ xi

) else (

echo ^(file "! Old file name! "Does not contain letters, cannot rename.^)

)

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

Thank you for reading this article carefully. I hope that the article "How to rename special files using batch processing" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report