China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-29 06:42
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Remove specific characters before file names View 3,397 Replies 21
Original Poster Posted 2006-12-07 04:12 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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



@echo off
for /f "delims=" %%a in ('dir/b *.txt') do call :next %%a
echo Finish...
goto :eof
:next
set pp=%1
:loop
if %pp:~0,1%==0 set pp=%pp:~1%& goto :loop
ren %1 %pp%
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-12-07 22:46
我今后在论坛的目标就是做个超级坏人!!!
Floor 2 Posted 2006-12-07 04:29 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Brother 9527 has a wide range of interests! Learned it.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2006-12-07 08:20 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 4 Posted 2006-12-07 08:42 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
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:


@echo off
for /f "delims=" %%i in ('dir/b *.txt') do (
echo Processing.......
set /a name=%%~ni+0
call ren "%%i" "%%name%%%%~xi"
)
echo Processing completed
pause
Floor 5 Posted 2006-12-07 09:10 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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~ :)


C:\TEMP\file>set /a 00000020
16


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~ :)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2006-12-07 09:50 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 7 Posted 2006-12-07 09:55 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
```
@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
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-12-07 22:46
Floor 8 Posted 2006-12-07 10:26 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2006-12-07 11:42 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
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):

@echo off & echo Processing.......
for /f "delims=" %%i in ('dir/b *.txt') do (
set /a name=2%%~ni-1%%~ni
call set /a name=1%%~ni-%%name%%
call rename "%%i" "%%name%%%%~xi"
)
echo Processing completed
pause
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
namejm +4 2006-12-07 12:49
redtek +5 2006-12-07 23:19
Floor 10 Posted 2006-12-07 11:47 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
```
@echo off
for /f "delims=0 tokens=*" %%i in ('dir /b *.txt') do echo %%i
pause>nul
goto :eof

Not tested.
```
Recent Ratings for This Post ( 5 in total) Click for details
RaterScoreTime
namejm +4 2006-12-07 12:50
youxi01 +4 2006-12-07 13:01
pengfei +9 2006-12-07 13:10
redtek +5 2006-12-07 21:54
ccwan +5 2006-12-07 22:20
Floor 11 Posted 2006-12-07 11:57 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 ]
业精于勤而荒于嬉,形成于思而毁于随。
Floor 12 Posted 2006-12-07 12:40 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Post 9 test results:

Before testing:

Directory of F:\test

2006-12-06 23:33 <DIR> .
2006-12-06 23:33 <DIR> ..
2006-10-26 23:13 17 NT-CMD.bat
2006-12-06 23:34 2 0001.txt
2006-12-06 23:34 2 0002.txt
2006-12-06 23:34 2 0003.txt
2006-12-06 23:34 2 0004.txt
2006-12-06 23:34 2 0005.txt
2006-12-06 23:34 2 0006.txt
2006-12-06 23:34 2 0007.txt
2006-12-06 23:34 2 0008.txt
2006-12-06 23:34 2 0009.txt
2006-12-06 23:34 2 00010.txt
2006-12-06 23:34 2 00011.txt
2006-12-06 23:34 2 00012.txt
2006-12-06 23:34 2 00013.txt
2006-12-06 23:34 2 00014.txt
2006-12-06 23:34 2 00015.txt
2006-12-06 23:34 2 00016.txt
2006-12-06 23:34 2 00017.txt
2006-12-06 23:34 2 00018.txt
2006-12-06 23:34 2 00019.txt
2006-12-06 23:34 2 00020.txt
2006-12-06 23:34 2 00021.txt
2006-12-06 23:34 2 00022.txt
2006-12-06 23:34 2 00023.txt
2006-12-06 23:34 2 00024.txt
2006-12-06 23:34 2 00025.txt
2006-12-06 23:34 2 00026.txt
2006-12-06 23:34 2 00027.txt
2006-12-06 23:34 2 00028.txt
2006-12-06 23:34 2 00029.txt
2006-12-06 23:34 2 00030.txt
2006-12-06 23:34 2 00031.txt
2006-12-06 23:34 2 00032.txt
2006-12-06 23:34 2 00033.txt
2006-12-06 23:34 2 00034.txt
2006-12-06 23:34 2 00035.txt
2006-12-06 23:34 2 00036.txt
2006-12-06 23:34 2 00037.txt
2006-12-06 23:34 2 00038.txt
2006-12-06 23:34 2 00039.txt
2006-12-06 23:34 2 00040.txt
2006-12-06 23:34 2 00041.txt
2006-12-06 23:34 2 00042.txt
2006-12-06 23:34 2 00043.txt
2006-12-06 23:34 2 00044.txt
2006-12-06 23:34 2 00045.txt
2006-12-06 23:34 2 00046.txt
2006-12-06 23:34 2 00047.txt
2006-12-06 23:34 2 00048.txt
2006-12-06 23:34 2 00049.txt
2006-12-06 23:34 2 00050.txt
51 File(s) 117 bytes
2 Dir(s) 17,874,583,552 bytes free


After testing:

Directory of F:\test

2006-12-06 23:33 <DIR> .
2006-12-06 23:33 <DIR> ..
2006-12-06 23:38 6 NT-CMD.bat
2006-12-06 23:34 2 1.txt
2006-12-06 23:34 2 2.txt
2006-12-06 23:34 2 3.txt
2006-12-06 23:34 2 4.txt
2006-12-06 23:34 2 5.txt
2006-12-06 23:34 2 6.txt
2006-12-06 23:34 2 7.txt
2006-12-06 23:34 2 8.txt
2006-12-06 23:34 2 9.txt
2006-12-06 23:34 2 10.txt
2006-12-06 23:34 2 11.txt
2006-12-06 23:34 2 12.txt
2006-12-06 23:34 2 13.txt
2006-12-06 23:34 2 14.txt
2006-12-06 23:34 2 15.txt
2006-12-06 23:34 2 16.txt
2006-12-06 23:34 2 17.txt
2006-12-06 23:34 2 18.txt
2006-12-06 23:34 2 19.txt
2006-12-06 23:34 2 20.txt
2006-12-06 23:34 2 21.txt
2006-12-06 23:34 2 22.txt
2006-12-06 23:34 2 23.txt
2006-12-06 23:34 2 24.txt
2006-12-06 23:34 2 25.txt
2006-12-06 23:34 2 26.txt
2006-12-06 23:34 2 27.txt
2006-12-06 23:34 2 28.txt
2006-12-06 23:34 2 29.txt
2006-12-06 23:34 2 30.txt
2006-12-06 23:34 2 31.txt
2006-12-06 23:34 2 32.txt
2006-12-06 23:34 2 33.txt
2006-12-06 23:34 2 34.txt
2006-12-06 23:34 2 35.txt
2006-12-06 23:34 2 36.txt
2006-12-06 23:34 2 37.txt
2006-12-06 23:34 2 38.txt
2006-12-06 23:34 2 39.txt
2006-12-06 23:34 2 40.txt
2006-12-06 23:34 2 41.txt
2006-12-06 23:34 2 42.txt
2006-12-06 23:34 2 43.txt
2006-12-06 23:34 2 44.txt
2006-12-06 23:34 2 45.txt
2006-12-06 23:34 2 46.txt
2006-12-06 23:34 2 47.txt
2006-12-06 23:34 2 48.txt
2006-12-06 23:34 2 49.txt
2006-12-06 23:34 2 50.txt
51 File(s) 106 bytes
2 Dir(s) 17,874,583,552 bytes free


So far, no major problems have been found, and the problem of the number base has been successfully solved.
Floor 13 Posted 2006-12-07 12:51 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code of youxi01 and 3742668 is a bit unexpected and very wonderful.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2006-12-07 12:54 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
The joy of moderator 3742668, directly using 0 as a separator. This method should also be universal, wonderful...
业精于勤而荒于嬉,形成于思而毁于随。
Floor 15 Posted 2006-12-07 13:02 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
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 ]
Forum Jump: