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-19 21:58
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] setlocal EnableDelayedExpansion and "!" View 9,324 Replies 25
Floor 16 Posted 2006-06-19 13:26 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Please provide the specific Chinese content that needs to be translated so that I can assist you with the translation.
Floor 17 Posted 2006-06-19 18:58 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re: kcdsw:

Sorry! Your question was noticed by me soon, but unfortunately, I did many tests at that time and later, and the result proved that there are many problems in using the for+echo method to copy and edit text, especially for files like cover.ps which contain quite a lot of special characters.

So I was ready to change my approach and use other methods to edit text.

The first method that came to mind was edlin, because it is a command-line text editor built into the NT series. After going through some trouble, I found that edlin cannot handle extremely long files, and the file is only loaded up to more than 530 lines and then ends.

Then, the remaining path was only third-party script tools. Considering that it was just to replace some simple text, so sed was chosen, and its script is not cumbersome to write. The following code was tested with GNU sed 3.02, and the result was correct and efficient.


@echo off

:: Get the original position parameters
:: for /f "tokens=1,2,3,4,5,6,7" %%a in ('findstr /C:"DeviceRGB" cover.ps') do set jpg=%%a %%b %%c %%d %%e %%f %%g

:: Get the pixel values of the image for judgment
for /f "tokens=2,3" %%h in ('findstr /C:"setcolorspace" cover.ps') do (
if %%h EQU %%i echo The author's photo in the cover does not need to be changed!
if %%h LSS %%i echo The photo is irregular!
if %%h GTR %%i set /a newsize=730*%%h/%%i
)

:::: Get the line number and content of the line
:: for /f "tokens=1,* delims=:" %%m in ('findstr /N /C:"730 730" temp.ps') do (
:: set iLN=%%m
:: set sLine=%%n
::)
::
:: set sLine=%sLine:730 730=!newsize! 730%
::
:: echo %iLn%d> _temp.edl
:: echo i%iLn%>> _temp.edl
:: echo %sLine%>> _temp.edl
:: echo >> _temp.edl
:: echo %iLn%,%iLn%l>> _temp.edl
:: echo e>> _temp.edl
::
:: edlin cover.ps < _temp.edl

:: Write the new size to the specified line
if %newsize% NEQ 0 sed -e "s/\(.*\)730\( 730.*\)/\1%newsize%\2/" cover.ps >cover3.txt


[ Last edited by willsort on 2006-6-20 at 10:31 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 18 Posted 2006-06-20 10:15 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
@echo off 
setlocal EnableDelayedExpansion

:: Get original positional parameters
for /f "tokens=1,2,3,4,5,6,7" %%a in ('findstr /C:"DeviceRGB" cover.ps') do set jpg=%%a %%b %%c %%d %%e %%f %%g

:: Get pixel values of the image for judgment
for /f "tokens=2,3" %%h in ('findstr /C:"setcolorspace" cover.ps') do (
if %%h EQU %%i echo No need to change the author's photo on the cover!
if %%h LSS %%i echo The photo is irregular!
if %%h GTR %%i set /a newsize=730*%%h/%%i
)

:: Write the new size to the specified line
if %newsize% NEQ 0 sed -e "s/\(.*\)730\( 730.*\)/\1%newsize%\2/" cover.ps >cover3.txt


If written like this, is it just replacing the original 730 730?
If so, can I just get rid of the first for line?
Just need to extract the pixel size, then calculate the required new data and then replace?


Can you provide the download of the sed version you mentioned?

[ Last edited by kcdsw on 2006-6-20 at 10:29 ]
Floor 19 Posted 2006-06-20 10:30 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re kcdsw:

That's right.

The first for statement in the original code was used when considering the for+echo and edlin solutions. After switching to the sed solution, since it doesn't affect the output result, it was overlooked to delete it. Similar statements also include setlocal.

Now the original code has been modified.

For the sed download link, please refer to . Please note that using GNU sed 4.0.7 will filter out the carriage return characters (0x0D) in the text lines and generate Unix - format text. GNU sed 3.02 doesn't have this problem.

GNU utilities for Win32
http://unxutils.sourceforge.net/

[ Last edited by willsort on 2006-6-20 at 10:39 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 20 Posted 2006-06-20 11:33 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Hehe, thanks moderator.

Previously I was very stubborn to use batch processing, maybe it's my psychology playing tricks.
Always feeling that most external programs have no Chinese annotations, not written by myself, and also not at ease, difficult to learn and understand.
Today I used it and felt it's不错 (Note: "不错" here remains as it is since it's part of the original text and the task is to translate the Chinese parts. Wait, no, I made a mistake. The entire text needs to be translated. Let's start over.)

Hehe, thank you moderator.

Previously I was very stubborn to use batch processing, maybe it's my psychology playing tricks.
Always feeling that most external programs have no Chinese annotations, are not written by myself, and I'm also not at ease, hard to learn and understand.
Today I used it and felt it was good.

Wait, no, let's do it properly:

Original Chinese: 呵呵 谢谢版主

先前我很固执的要用批处理,可能是我的心理在做怪
总是觉得外来的程序大多没中文注解,也不是自己写的,也不放心,难学难懂
今天一用 感觉不错

Translation:

Hehe, thanks moderator.

Previously I was very stubborn about using batch processing, maybe it's my psychology playing tricks.
Always feeling that most external programs have no Chinese annotations, are not written by myself, and I'm also not at ease, hard to learn and understand.
Today when I used it, I felt it was good.
Floor 21 Posted 2006-06-20 11:37 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
I used QuickBFC to compile.

When compiling, I bundled sed in.

Because when the compiled exe runs, it first needs to release the original bat file, and the location is %temp%.

I think the path where it releases the bundled file is probably also here, or the folder where the exe is located.

In this way, the exe will automatically pass the path of cover.ps to the bat and sed, which makes me more at ease, avoiding that sed and bat are not together. Avoiding me telling it the path of cover.ps, saving a lot of things!!!

Here, once again, I thank the moderator and all the brothers who paid attention to my post!!

Bow to you, little brother!!
Floor 22 Posted 2007-03-17 23:34 ·  中国 上海 黄浦区 电信
初级用户
Credits 49
Posts 22
Joined 2005-08-04 13:44
20-year member
UID 41332
Gender Male
Status Offline
Floor 23 Posted 2007-05-13 12:31 ·  中国 广东 广州 天河区 电信
新手上路
Credits 4
Posts 2
Joined 2007-05-13 11:22
19-year member
UID 88381
Gender Male
Status Offline
So many experts!!!!!! Admiring them....
Floor 24 Posted 2007-10-12 22:23 ·  中国 浙江 温州 电信
初级用户
★★
Credits 158
Posts 89
Joined 2007-04-25 08:06
19-year member
UID 86402
Gender Male
Status Offline
Awesome. Push it up.
Floor 25 Posted 2008-10-31 13:54 ·  中国 湖南 长沙 联通
新手上路
Credits 4
Posts 3
Joined 2007-11-03 01:05
18-year member
UID 101547
Gender Male
Status Offline
Confusedly, download it and take a look.
Floor 26 Posted 2008-11-20 23:00 ·  中国 山东 青岛 联通
新手上路
Credits 1
Posts 1
Joined 2008-11-18 22:56
17-year member
UID 131398
Gender Male
Status Offline
Let me download and try it
Forum Jump: