Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

Batch Rename Multiple Files?

Similar question to these have been tons of time but, for what i want it is something different. I have tried searching but, not what I want.

Now I have certain (.txt) files in my folder (hidden-files) where certain text files (Multiple text files) are stored which have special characters (%!=^";@% ..etc) in them I would like to remove these special characters and replace them with (-) in a rename loop so the file(s) will be renamed.

Code (Got it from here somewhere) Edited to my use but, it didn't worked.

for /f "delims=" %%a in ('dir "%~dp0hidden-files*.txt"') do (set "newname=%~nx1"
set "newname=%newname:!=-%"
set "newname=%newname:_=-%" 
set "newname=%newname:==-%"
set "newname=%newname:%=-%"
echo ren "%%a" "!newname!")

Would appreciate any help. Cheers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a whole host of problems with your code. I'm too tired to list them all, but there is one issue that is extremely difficult to solve with pure batch - there is no simple way to replace an = literal within a string using batch. The simplest (and perhaps most effective) way to accomplish this is to look character by character for the = and replace it via substring operations.

There are a bunch of other issues that require advanced batch techniques to solve.

I have a much simpler solution for you - my JREN.BAT utility uses regular expression replacement to rename files. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. Use jren /? from the command line to access the built-in help. You might want to use
jren /? | more to see one screen at a time. I never use MORE because my console is configured with a large buffer that lets me scroll up to see past output.

Once you have JREN.BAT on your machine, preferably in a folder that is within your PATH variable, then all you need is the following to rename the files within the current directory - no additional batch script required:

jren "[!_=%]" "-" /fm *.txt

If needed, you can specify the folder as an option:

jren "[!_=%]" "-" /fm *.txt /p "c:YourPathHere"

If you put the command within another batch script, then you will need to use CALL JREN, and the percent needs to be double escaped as %%%%. Percent literals must be escaped as %% within batch files, and the CALL requires an extra round of escape:

@echo off
call jren "[!_=%%%%]" "-" /fm *.txt /p "%~dp0hidden-files"

Update in response to comment

Adding (, ), [, and ] to the list of characters to replace is simple. The only trick is you must escape the ] as ] when it appears within a character set [....].

@echo off
call jren "[!_=%%%%[]()]" "-" /fm *.txt /p "%~dp0hidden-files"

The other option is to place the ] immediately after the opening [.

@echo off
call jren "[]!_=%%%%[()]" "-" /fm *.txt /p "%~dp0hidden-files"

Regular expressions are incredibly powerful, but they can be confusing to the uninitiated. Use jren /?regex to access Microsoft's help page on all the supported regular expression syntax. There are loads of tutorials available on the internet, as well as web sites that allow you to conveniently test out regular expressions.

If you are developing a JREN command, I suggest you first use the /t option to test it out without actually renaming anything.

Last update for question in comment

File names cannot contain *, ?, |, <, >, , /, : or ", so you don't need to worry about those characters. I'm not sure what you did, but I don't see the enclosing [...] to represent a character set. The following should work fine:

call jren "[`~!@#$%%%%^&_ +=,';}{[]()]" "-" /fm *.txt /p "%~dp0hidden-files"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...