中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-25 23:47
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » Remove specific characters before file names
Printable Version  3,610 / 21
Floor1 9527 Posted 2006-12-07 04:12
银牌会员 Posts 438 Credits 1,185 From 北京
This is what I saw on the GOOGLE group, and I sent it here for everyone to study. Although it has great limitations, you can take a look.

Mainly to remove the 0 before the file name

Before processing:
0000001.txt 00000010.txt 00000011.txt
00000012.txt 00000013.txt 00000014.txt 00000015.txt 00000016.txt
00000017.txt 00000018.txt 00000019.txt 0000002.txt 00000020.txt
0000003.txt 0000004.txt 0000005.txt 0000006.txt 0000007.txt
0000008.txt 0000009.txt

After processing:
1.txt 10.txt 11.txt 12.txt 13.txt
14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 2.txt
20.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt
9.txt


Floor2 ccwan Posted 2006-12-07 04:29
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Brother 9527 has a wide range of interests! Learned it.
Floor3 redtek Posted 2006-12-07 08:20
金牌会员 Posts 1,147 Credits 2,902
Floor4 youxi01 Posted 2006-12-07 08:42
高级用户 Posts 247 Credits 846 From 湖南==》广东
If the name is purely as shown by the owner (type), it seems that the characteristics of numbers can be used to handle the leading "00....." to simplify the code:

Floor5 redtek Posted 2006-12-07 09:10
金牌会员 Posts 1,147 Credits 2,902
Appreciate brother youxi01's very clever method~ :)

It's too coincidental that SET /A actually treats so many 0-prefixed 00000020 as octal numbers starting with 0. As a result, octal 20 is exactly 16 in decimal~ :)



The unexpected thing is that the 00000020.txt was converted by SET /A from the octal number that SET thought into 16.txt in decimal~ :)
But this is just an accident~ :)

Appreciate brother youxi01's fun implementation method~ :)
Floor6 不得不爱 Posted 2006-12-07 09:50
超级版主 Posts 2,044 Credits 5,310 From 四川南充
If the 4th floor encounters 00000018.txt and 00000019.txt, it will be unable to process.
Floor7 不得不爱 Posted 2006-12-07 09:55
超级版主 Posts 2,044 Credits 5,310 From 四川南充
```
@echo off
for /f "delims=" %%i in ('dir/b *.txt') do (
echo Processing.......
set /a name=1%%~ni%%100000
call ren "%%i" "%%name%%%%~xi"
)
echo Processing completed
pause
```
Floor8 namejm Posted 2006-12-07 10:26
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The method of set /a is either affected by the number system and prone to errors, or it is difficult to know the number of leading zeros, so it is not easy to find the result by adding 1 at the leading position and then subtracting. Still, the method of detecting whether the leading digit is 0 to decide whether to continue to discard one leading 0 is more universal.
Floor9 youxi01 Posted 2006-12-07 11:42
高级用户 Posts 247 Credits 846 From 湖南==》广东
I'm sorry, I forgot about the issue of number bases. My careless (impulsive) personality still hasn't changed! namejm's statement is good, but this problem isn't without a solution. Let's see what other problems there are in the following code (after solving the number base issue):
Floor10 3742668 Posted 2006-12-07 11:47
荣誉版主 Posts 718 Credits 2,013
```
@echo off
for /f "delims=0 tokens=*" %%i in ('dir /b *.txt') do echo %%i
pause>nul
goto :eof

Not tested.
```
Floor11 pengfei Posted 2006-12-07 11:57
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Originally posted by redtek at 2006-12-7 09:10:
Appreciate the very clever method of user youxi01~ : )

What a coincidence! SET /A actually treats so many 0-prefixed 00000020 as an octal number starting with 0, and the result is that the octal 20 happens to be...


This method of taking digits in a loop to remove zeros is indeed very cool, and it works well in various situations. It has also worked well in dealing with floating-point numbers in batch processing.

Regarding the problem of octal errors, there has been a discussion before.

http://www.cn-dos.net/forum/viewthread.php?tid=22951&fpage=1&highlight=

[ Last edited by pengfei on 2006-12-7 at 01:09 PM ]
Floor12 youxi01 Posted 2006-12-07 12:40
高级用户 Posts 247 Credits 846 From 湖南==》广东
Post 9 test results:

Before testing:


After testing:


So far, no major problems have been found, and the problem of the number base has been successfully solved.
Floor13 namejm Posted 2006-12-07 12:51
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The code of youxi01 and 3742668 is a bit unexpected and very wonderful.
Floor14 pengfei Posted 2006-12-07 12:54
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
The joy of moderator 3742668, directly using 0 as a separator. This method should also be universal, wonderful...
Floor15 youxi01 Posted 2006-12-07 13:02
高级用户 Posts 247 Credits 846 From 湖南==》广东
The pleasure of version 3742668 moderator!!! The moderator should come and take a look more often, your code always makes people feel refreshed! Envy + learning!

[ Last edited by youxi01 on 2006-12-7 at 01:05 PM ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023