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-09-25 00:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Help, a batch file to replace specified characters in ASP files? View 1,306 Replies 6
Original Poster Posted 2007-04-09 12:32 ·  中国 广东 广州 白云区 电信
初级用户
Credits 98
Posts 39
Joined 2007-03-16 11:19
19-year member
UID 81943
Gender Male
Status Offline
Environment: windows xp command prompt

Requirement: automatically search and replace (here, use SB_ to replace) the specified characters (here, admin_) in all ASP files in the same folder

Or it would also be fine to directly search all ASP files for the "admin_" string and then delete it.


One more small request: it'd be best if you could add a few comments to the program, because I'm learning batch files now too, and I hope it can help me understand the program a bit. Thanks.
Floor 2 Posted 2007-04-09 15:12 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline


rem Enable delayed expansion;
@echo off&setlocal enabledelayedexpansion

rem Traverse ASP files and replace;

for /f "delims=" %%a in ('dir /b/a-d .\*.asp') do (
for /f "tokens=1* delims=:" %%b in ('findstr /n .* "%%a"') do (
set "o=%%c"
set "o=!o:SB=admin!"
echo/!o!>>%%~na_%%~xa
)
)

Not tested. If there are problems, reply and discuss...
Floor 3 Posted 2007-04-09 21:32 ·  中国 辽宁 丹东 联通
初级用户
★★
Credits 116
Posts 56
Joined 2007-03-07 04:53
19-year member
UID 80966
Gender Male
Status Offline
I'll learn from this too, so ASP can do this as well~~
QQ=3391617
Floor 4 Posted 2007-04-10 06:04 ·  中国 广东 广州 天河区 电信
初级用户
Credits 98
Posts 39
Joined 2007-03-16 11:19
19-year member
UID 81943
Gender Male
Status Offline
Thanks in advance, I'll test it when I get back tonight..
Floor 5 Posted 2007-04-10 10:03 ·  中国 广东 广州 白云区 电信
初级用户
Credits 98
Posts 39
Joined 2007-03-16 11:19
19-year member
UID 81943
Gender Male
Status Offline
Expert, I tried it and it doesn't work. After running it, it creates several ASP files with the same content as the files that need to be modified, but the parts that should be changed have not changed
Floor 6 Posted 2007-04-10 12:51 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

Re vcstone:

I tested it. The batch file above does work, but I improved it again and it will delete blank lines. See the code below:

::Enable delayed expansion;
@echo off&setlocal enabledelayedexpansion
::Traverse ASP files and replace;
for /f "delims=" %%a in ('dir /b/a-d .\*.asp') do (
for /f "tokens=1* delims=:" %%b in ('findstr /n .* "%%a"') do (
if not "%%c"=="" (set "o=%%c"
set "o=!o:SB=admin!"
echo/!o!>>%%~na_%%~xa)
)
)

Note: it can only replace the "SB" characters inside. Characters like "sb", "Sb", or "sB" cannot be replaced. Try again.
Floor 7 Posted 2007-04-11 03:11 ·  中国 广东 广州 白云区 电信
初级用户
Credits 98
Posts 39
Joined 2007-03-16 11:19
19-year member
UID 81943
Gender Male
Status Offline
It works now.. Thanks first! I still don't understand the program very well for the moment, I'll go back and study it a bit. If I have questions I'll ask you again.
Forum Jump: