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-27 21:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to pass custom parameters View 6,064 Replies 15
Original Poster Posted 2006-05-12 15:31 ·  中国 江苏 徐州 电信
新手上路
Credits 18
Posts 7
Joined 2006-05-12 15:21
20-year member
UID 55396
From 风花雪月
Status Offline
My first BAT has the following written:
call 2.bat * txt c:\a

The second one is written like this:

@echo off
if {%1}=={} goto :useageexit
if {%2}=={} goto :useageexit
if {%3}=={} goto :useageexit
for /R %3 %%v IN (%1.%2) DO (
call:rn %%v
)
:rn
if %1==* exit
ren %1 *.doc

That is, passing the parameters from the first one to the second one. The problem is here. I now want to directly write the parameters in the second one without using the first one as a medium to complete the automatic renaming. Is there any expert who knows how to define variables? Well!! You can also give the code!! Thank you

It is best to give a simple example

[ Last edited by namejm on 2007-2-5 at 01:58 PM ]
Floor 2 Posted 2006-05-12 18:18 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline

That is to say, pass the parameters in the first one to the second one. The problem is here. I now want to write parameters directly in the second one without using the first one as a medium to complete the automatic renaming. Is there any expert who knows how to define variables?!! Also, code is okay!! Thank you
...

A bit like a tongue twister, not very clear about your meaning. It seems that you want to use only call 2.bat in the first batch file without adding parameters. If that's the case, you can use set /p in 2.bat to obtain the parameters that need to be operated.
Floor 3 Posted 2006-05-12 20:35 ·  中国 江苏 徐州 电信
新手上路
Credits 18
Posts 7
Joined 2006-05-12 15:21
20-year member
UID 55396
From 风花雪月
Status Offline
Hehe, that's the case. I use the first one to call the second one, and pass in the values of %1 2 3 during the calling process to do the renaming processing according to the passed-in values. Do you understand?

What I want to do now is to directly write the values in the second one without the first one passing the values in. Don't need to write parameter values in DOS, just hardcode it. Thanks ; )
Floor 4 Posted 2006-05-12 21:20 ·  中国 上海 电信
中级用户
★★
论坛第一菜鸟
Credits 361
Posts 166
Joined 2006-04-15 13:23
20-year member
UID 53870
Status Offline
Is it if {%1}=={}?
I remember it should be if "%1"==""

[ Last edited by xjmxjm1234 on 2006-5-13 at 12:56 ]
Diskette Operating System
Floor 5 Posted 2006-05-13 08:04 ·  中国 江苏 徐州 电信
新手上路
Credits 18
Posts 7
Joined 2006-05-12 15:21
20-year member
UID 55396
From 风花雪月
Status Offline
It's okay. I can ensure that my program will definitely run. I just want to complete writing it directly. For example, can I replace %1 with a = how much, and then use a to represent %1 in the following program? Is there such a method?
Floor 6 Posted 2006-05-13 08:08 ·  中国 江苏 徐州 电信
新手上路
Credits 18
Posts 7
Joined 2006-05-12 15:21
20-year member
UID 55396
From 风花雪月
Status Offline
Brothers, please help. Code is also okay, I can just look at it myself. But the most important thing is that it doesn't need to write values under DOS. What is needed is to directly run to complete the renaming. The file name and the path of the file to be renamed are hardcoded
Floor 7 Posted 2006-05-13 12:46 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Use the set command, set a = some value, then replace %1 with %a%, set b = some value, then replace %2 with %b%.....
A tall building starts from the ground.

Re xjmxjm1234:
In fact, there are no official methods and regulations for anti - empty characters. Generally speaking, it is all according to everyone's habits. Sometimes use {}, sometimes use "", and even sometimes use goto, call and anti - empty according to errorlevel. For more detailed explanations, you can refer to the post by Brother willsort: A Deviation in Batch Programming
Floor 8 Posted 2006-05-13 16:21 ·  中国 上海 徐汇区 电信
中级用户
★★
论坛第一菜鸟
Credits 361
Posts 166
Joined 2006-04-15 13:23
20-year member
UID 53870
Status Offline
Diskette Operating System
Floor 9 Posted 2006-05-13 16:48 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
My first BAT wrote it like this
call 2.bat * txt c:\a

@echo off
if {%1}=={} goto :useageexit
if {%2}=={} goto :useageexit
if {%3}=={} goto :useageexit
for /R %3 %%v IN (%1.%2) DO (
call:rn %%v
)
:rn
if %1==* exit
ren %1 *.doc

Isn't it just batch renaming? Is the LZ's method a bit overkill? I don't understand why the owner did this?

@echo off
if "%1"=="" goto :eof
if "%2"=="" goto :eof
if "%3"=="" goto :eof
for /R "%3" %%v IN (%1.%2) DO (
ren "%%v" "%%~nv".doc
)


If the owner is for batch renaming, the above is what I modified according to yours, just one batch processing is enough, no need to be so cumbersome
I hope your problem can be solved
Floor 10 Posted 2006-05-13 18:22 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xjmyga:

According to your description, I can only understand that you want to achieve direct replacement of command-line parameters in batch processing, turning it from a general-purpose program into a specialized program, and if you have the ability to write the two sections of batch processing, you should also be able to easily achieve this conversion. My approach is as follows:


@echo off
for /R c:\a %%v IN (*.txt) DO (
call:rn %%v
)
:rn
if %1==* exit
ren %1 *.doc


Re bagpipe:

Regarding your second piece of code, you can consider discarding the two lines if "%1" and if "%2", because we finally require that all three command-line parameters must be present, so we only need to judge the last one, after all, without %1 and %2, %3 would not exist.

I'm very sorry! Criticizing your code several times within a day is really due to my temperament, please don't mind!
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 11 Posted 2006-05-13 19:54 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
for /R %3. %%v IN (%1.%2) DO ren "%%v" "%%~nv".doc
If there is parameter 3, it is the directory of parameter 3, and the default is the current directory.
Floor 12 Posted 2006-05-14 00:34 ·  中国 江苏 徐州 电信
新手上路
Credits 18
Posts 7
Joined 2006-05-12 15:21
20-year member
UID 55396
From 风花雪月
Status Offline
Thanks to all brothers for your instructions. I'm using PB to modify the program. When calling a BAT in PB, this BAT must be completed in one step and cannot be called using CALL. I'm very grateful to you. Thank you, thank you, thank you!

The method mentioned by willsort I also tried, but there's a problem when running it, so it didn't work. Thank you all. You helped me solve a relatively troublesome problem.
Floor 13 Posted 2006-05-14 17:30 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
The first piece of code was not written by me, but by the thread starter. I just rewrote it according to the thread starter's code. As for omitting %1 %2, I think the thread starter didn't make it clear. If the thread starter had said there must be three parameters, I wouldn't have written it like this. Brother WILLSOR, what are you saying? I don't care about criticism or suggestions. This will make me learn more. I'm not the same person as when I first came to the union. I hope all the experts can give more guidance...
Floor 14 Posted 2006-05-15 08:41 ·  中国 江苏 徐州 电信
新手上路
Credits 18
Posts 7
Joined 2006-05-12 15:21
20-year member
UID 55396
From 风花雪月
Status Offline
@echo off
for /R c:\a %%v IN (*.txt) DO (
call:rn %%v
)
:rn
if "%1"=="*" exit
ren %1 *.doc

Dear moderator or brothers, why is %1 not defined in this program, but REN can use %1 as a parameter value?
Hope to get some guidance from the experts.
Floor 15 Posted 2006-05-15 22:19 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xjmyga:

%0~%9 are system-defined command-line parameter variables, which do not need to be defined by us and cannot be defined by us. However, we can assign values to these variables, and the method is to call a batch file with parameters on the command line or in a batch script. The following methods 1 and 2 are valid in both DOS and CMD, and 3 is only valid in CMD.

1、Command line: batprog arg1 arg2 arg3 ...
2、Batch script: call batprog arg1 arg2 arg3 ...
3、Call itself in a batch script and start execution from a certain label: call:label arg1 arg2 arg3 ...

After this call, parameters such as arg1, arg2, arg3 are passed into the batch script and assigned to %1, %2, %3 respectively. And %0 represents the first string in the command line, that is, the batch script command itself.

After calling: %0 == batprog (methods 1, 2) or its full path name (method 3), %1 == arg1, %2 == arg2...

In the case of building 10, the call:rn sentence in for belongs to the third calling method. Pass %%v as a command line parameter to itself and assign it to %1 for reference in the :rn segment.

In addition, the code on building 10 is directly converted from your original code and still inherits some defects. For example, if %1 == * lacks necessary anti-empty measures, it will definitely go wrong when %1 is an empty value, although there is no possibility of taking an empty value here; and this sentence itself is also because %1 cannot be * and seems redundant, thus making the :rn segment and self-call redundant. The second paragraph of building 9 is exactly to improve this point.

And the reason why %1 will not be * or empty is due to the parsing characteristics of for, which needs to be carefully considered.

Therefore, the final improved result of the code is as follows:


@echo off
for /R c:\a %%v IN (*.txt) do ren %%v *.doc
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: