|
xjmyga
新手上路

积分 18
发帖 7
注册 2006-5-12 来自 风花雪月
状态 离线
|
『楼 主』:
如何传递自定义参数
使用 LLM 解释/回答一下
我的第一个BAT中写的是这样
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
就是说把第一个中的参数传到二中 问题就在这里 我现在想在二中直接写
入参数 不需要一来做媒介来完成自动改名 可有高手知道如何定义变量
不!! 给代码也行!!谢谢
最好能给出一个简单点的例子
Last edited by namejm on 2007-2-5 at 01:58 PM ]
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 ]
|
|
2006-5-12 15:31 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
就是说把第一个中的参数传到二中 问题就在这里 我现在想在二中直接写
入参数 不需要一来做媒介来完成自动改名 可有高手知道如何定义变量
不!! 给代码也行!!谢谢
...
有点绕口令的味道,不是很明白你的意思。似乎是想在第一个批处理中只用call 2.bat,而不加参数。如果是这样的话你可以在2.bat中用set /p来获得需要操作的参数。
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.
|
|
2006-5-12 18:18 |
|
|
xjmyga
新手上路

积分 18
发帖 7
注册 2006-5-12 来自 风花雪月
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
呵呵是这样的 我用第一个调用第二个 在调用过程把%1 2 3 的值传入进去根据传入的值做改名处理 明白吗
我现在想做的是 在第二中直接写入值而不需要第一个把值传进去
不用在DOS中写入参数值 直接写死谢谢 
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 ; )
|
|
2006-5-12 20:35 |
|
|
xjmxjm1234
中级用户
   论坛第一菜鸟
积分 361
发帖 166
注册 2006-4-15
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
是 if {%1}=={} 吗?
我记得应该用 if "%1"==""
Last edited by xjmxjm1234 on 2006-5-13 at 12:56 ]
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 |
|
2006-5-12 21:20 |
|
|
xjmyga
新手上路

积分 18
发帖 7
注册 2006-5-12 来自 风花雪月
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
这个没事的我可以保正我的程序一定能运行的 我只想一步玩成写死
比如说 能不能把%1换成 a=多少,在下面的程序中就用a代做%1这样的方法有没有
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?
|
|
2006-5-13 08:04 |
|
|
xjmyga
新手上路

积分 18
发帖 7
注册 2006-5-12 来自 风花雪月
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
各位兄弟帮帮忙呀 给代码也可以 我自己看好好 不过最重要的是不需要在DOS下写入值 要的是直接运行就能完成改名 文件名与被文件名路径写死
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
|
|
2006-5-13 08:08 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
用set命令,set a=多少,然后把%1换成%a%,set b=多少,再把%2换成%b%.....
万丈高楼平地起。
Re xjmxjm1234:
其实防空字符并没有什么官方的方法与规定,一般来说都是根据各人的习惯,有时候用{},有时候用“”,甚至有时候用goto,call以及根据errorlevel来防空,更详细的解释可以参考willsort兄的帖子: 批处理编程的异类
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
|
|
2006-5-13 12:46 |
|
|
xjmxjm1234
中级用户
   论坛第一菜鸟
积分 361
发帖 166
注册 2006-4-15
状态 离线
|
|
2006-5-13 16:21 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
我的第一个BAT中写的是这样
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
不就是批量改名吗?LZ的方法是不是有些小题大做了?不明白楼主为什么这么做?
@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
)
如果楼主是为了批量改名,上面的我是安装你的改的,就一个批处理就可以了,没有必要那么烦琐
希望你的问题能够解决
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
|
|
2006-5-13 16:48 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Re xjmyga:
按照你的叙述,我只能理解为你想实现命令行参数在批处理中的直接替换,将它由一个通用性程序变成一个专用型程序,而如果你具备编写者两段批处理的能力,就也应该可以轻松实现这种转换。我的做法如下:
@echo off
for /R c:\a %%v IN (*.txt) DO (
call:rn %%v
)
:rn
if %1==* exit
ren %1 *.doc
Re bagpipe:
关于你的第二段代码,可以考虑舍弃if "%1" 和 if "%2" 两句,因为我们最终要求三个命令行参数都必须具备,那么只需要判断最后一个就可以了,毕竟没有%1和%2,%3就无从存在。
很抱歉!一日之内屡次指摘兄的代码,实乃心性使然,万望勿怪!
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-5-13 18:22 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
for /R %3. %%v IN (%1.%2) DO ren "%%v" "%%~nv".doc
有参数3则为参数3的目录,默认为当前目录。
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.
|
|
2006-5-13 19:54 |
|
|
xjmyga
新手上路

积分 18
发帖 7
注册 2006-5-12 来自 风花雪月
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
多谢各位兄弟指教 小弟用PB改程序,在PB中调中BAT,这个BAT就必需是一步玩成不能用CALL来调用,十分的感谢你们谢谢谢谢!!
willsort说的方法我也试过可是在运行现在点问题所以没用,谢谢你们啦。帮我解决了个比较头痛的问题
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.
|
|
2006-5-14 00:34 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
第一段代码不是我写的,是楼主写的,我只是按照楼主的代码改写的,至于省略%1 %2,我看还是楼主没有说清楚,如果楼主说必有三个参数我也就不这样写了, WILLSORT兄哪里话,批评或者提意见我不在乎,这样会让我学到的更多,我已经不是刚来到联盟时的我了,望所有高手多多指点.......
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...
|
|
2006-5-14 17:30 |
|
|
xjmyga
新手上路

积分 18
发帖 7
注册 2006-5-12 来自 风花雪月
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
@echo off
for /R c:\a %%v IN (*.txt) DO (
call:rn %%v
)
:rn
if %1==* exit
ren %1 *.doc
请问板主OR各位兄弟,为什么这个程序中没有定义%1,然REN能用%1做参数值呢
还望高手指点
@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.
|
|
2006-5-15 08:41 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Re xjmyga:
%0~%9是系统预定义的命令行参数变量,不需要也不能由我们来定义。但是我们可以给这些变量赋值,方法即使通过在命令行或者批处理中带参数调用一个批处理。以下方法1、2在DOS和CMD中同时有效,3只在CMD中有效。
1、命令行:batprog arg1 arg2 arg3 ...
2、批处理:call batprog arg1 arg2 arg3 ...
3、批处理中调用自身并从某个标签处起始执行:call:label arg1 arg2 arg3 ...
经过此次调用,arg1,arg2,arg3这些参数被传入批处理中并分别赋给%1,%2,%3。而%0代表命令行的第一个串,即批处理命令本身。
调用后:%0==batprog(方法1、2)或者其全路径名(方式3),%1==arg1,%2==arg2...
10楼的案例中,for中的call:rn一句即属于第三种调用方式。将%%v作为命令行参数传递给自身,并赋给%1,供在:rn段中引用。
另外,10楼代码是从你的原代码直接转换而来,仍然继承了一些缺陷。比如if %1==* 缺乏必要的防空措施,在%1为空值时必然会出错,尽管在这里它没有取空值的可能;而此句本身也因为%1不可能为*而显得多余,从而使:rn段和自我调用显得多余。9楼的第二段代码正是针对此点进行改进的。
而之所以%1即不会为*,也不会为空,皆因for的解析特性,这需要慢慢揣摩。
因此,代码的最后改进结果如下:
@echo off
for /R c:\a %%v IN (*.txt) do ren %%v *.doc
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-5-15 22:19 |
|