位置: 编程技术 - 正文

DOS批处理 函数定义与用法(dos命令批量处理文件)

编辑:rootadmin

推荐整理分享DOS批处理 函数定义与用法(dos命令批量处理文件),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:dos批处理实例,dos批处理实例800例,dos批处理命令,dos批命令,dos批处理实例800例,dosbox批处理,dos批处理命令,dos常用命令与批处理文件,内容如对您有帮助,希望把文章链接给更多的朋友!

What it is, why it`s important and how to write your own.

Description: The assumption is: A batch script snippet can be named a function when:

1.... it has a callable entrance point.2.... on completion execution continues right after the command that initially called the function.3.... it works the same no matter from where it`s being called, even when it calls itself recursively.4.... the variables used within a function do not conflict with variables outside the function.5.... it exchanges data with the calling code strictly through input and output variables or a return code.

The benefits behind functions are:

1.Keep the main script clean2.Hide complexity in reusable functions3.Test functions independently from the main script4.Add more functionality to your batch script simply by adding more functions at the bottom5.Don`t worry about the function implementation, just test it and use it Create a Function What is a function&#; Call a Function How to invoke a function&#; Example - Calling a Function An Example showing how it works. Passing Function Arguments How to pass arguments to the function&#; Parsing Function Arguments How to retrieve function arguments within the function&#; Example - Function with Arguments An Example showing how it works. Returning Values the Classic Way The classic way of returning values and the limitations. Returning Values via References Let the caller determine how to return the function result and avoid the need of dedicated variables. Example - Returning Values using Variable Reference An Example showing how it works. Local Variables in Functions How to avoid name conflicts and keep variable changes local to the function&#; Returning Local Variables How to pass return values over the ENDLOCAL barrier&#; Recursive Functions Tadaaah!!! Summary Defining a standard format for a DOS batch function DOS Batch - Function Tutorial What it is, why it`s important and how to write your own.

Create a Function - What is a functionDescription: In DOS you write a function by surrounding a group of command by a label and a GOTO:EOF command. A single batch file can contain multiple functions defined like this. The label becomes the function name. Script:

Call a Function - How to invoke a functionDescription: A function can be called with the CALL command followed by the function label. Script: . call:myDosFunc

Example - Calling a Function - An Example showing how it worksDescription: The use of batch functions will divide the script into two sections.

1.The main script: starting at line 1 ending with a GOTO:EOF command that terminates the script.2.The function section: filling the second half of the batch file with one or more functions to be callable from the main script. Script:

Script Output: Script Output going to execute myDosFunc here the myDosFunc function is executing a group of commands it could do a lot of thingsreturned from myDosFunc Press any key to continue . . . Passing Function Arguments - How to pass arguments to the functionDescription: Just as the DOS batch file itself can have arguments, a function can be called with arguments in a similar way. Just list all arguments after the function name in the call command. Use a space or a comma to separate arguments. Use double quotes for string arguments with spaces. Script:

Parsing Function Arguments - How to retrieve function arguments within the functionDescription: Just as the DOS batch file itself retrieves arguments via %1 â&#;¦ %9 a function can parse it`s arguments the same way. The same rules apply. Let`s modify our previews example to use arguments. To strip of the double quotes in an arguments value the tilde modifier, i.e. use %~2 instead of %2. Script:

Example - Function with Arguments - An Example showing how it worksDescription: The following example demonstrates how to pass arguments into a DOS function. The :myDosFunc function is being called multiple times with different arguments.

Note: The last call to myDosFunc doesn`t use double quotes for the second argument. Subsequently "for" and "me" will be handled as two separate arguments, whereas the third argument "me" is not being used within the function. Script: Script Output: Script Output going to execute myDosFunc with different arguments here the myDosFunc function is executing a group of commands it could do of things YeePEE. here the myDosFunc function is executing a group of commands it could do of things for me. here the myDosFunc function is executing a group of commands it could do of things for me. here the myDosFunc function is executing a group of commands it could do of things for. Press any key to continue . . .

Returning Values the Classic Way - The classic way of returning values and the limitationsDescription: The CALL command doesn`t support return values as known by other programming languages. The classic walkaround is to have the function store the return value into a environment variable. The calling script can use this variable when the function returns. The :myGetFunc function below demonstrates how the variable var1 gets the "DosTips" string assigned which can then be used in the calling function.

DOS批处理 函数定义与用法(dos命令批量处理文件)

Note: The var1 variable is reserved for this particular function. Any data stored in var1 by the calling function before calling :myGetVar will be overwritten. Usage:

Script:

Script Output: Script Output var1 before: some hopefully not important stringvar1 after : DosTips Returning Values via References - Let the caller determine how to return the function result and avoid the need of dedicated variablesDescription: Instead of using "global" variables for return value, the function can use one of it`s arguments as variable reference. The caller can then pass a variable name to the function and the function can store the result into this variable making use of the command line expansion of the command processor:

Note: The var1 variable is not reserved for this articular function. Any variable can be passed to the function the caller has full control. Usage:

Script:

Script Output: Script Output var1 after : DosTips Example - Returning Values using Variable Reference - An Example showing how it worksDescription: This code shows how the var1 variable is being passed into a :myGetFunc function simply by passing the variable name. Within the :myGetFunc function the command processor works like this:1.Reads the set command into memory: set "%~1=DosTips"2.Expand the variables, i.e. %~1 like this: set "var1=DosTips"3.Finally execute the command and assign the new string to var1 Script:

Script Output: Script Output var1 before: CmdTipsvar1 after : DosTips Press any key to continue . . .

Local Variables in Functions - How to avoid name conflicts and keep variable changes local to the functionDescription: The SETLOCAL causes the command processor to backup all environment variables. The variables can be restored by calling ENDLOCAL. Changes made im between are local to the current batch. ENDLOCAL is automatically being called when the end of the batch file is reached, i.e. by calling GOTO:EOF. Localizing variables with SETLOCAL allows using variable names within a function freely without worrying about name conflicts with variables used outside the function. Script:

Script Output: Script Output aStr before: Expect no changed, even if used in functionvar1 before: No change for this one. Now what&#;aStr after : Expect no changed, even if used in functionvar1 after : No change for this one. Now what&#; Press any key to continue . . . Returning Local Variables - How to pass return values over the ENDLOCAL barrierDescription: The question is: When localizing a function via SETLOCAL and ENDLOCAL, how to return a value that was calculated before executing ENDLOCAL when ENDLOCAL restores all variables back to its original state&#; The answer comes with "variable expansion". The command processor expands all variables of a command before executing the command. Letting the command processor executing ENDLOCAL and a SET command at once solves the problem. Commands can be grouped within brackets. Script:

Script Output: Script Output aStr before: Expect no changed, even if used in functionvar1 before: Expect changedaStr after : Expect no changed, even if used in functionvar1 after : DosTips Press any key to continue . . .

Recursive Functions - Tadaaah!!!Description: Being able to completely encapsulate the body of a function by keeping variable changes local to the function and invisible to the caller we are now able to call a function recursively making sure each level of recursion works with its own set of variables even thought variable names are being reused.

Example: The next example below shows how to calculate a Fibonacci number recursively. The recursion ss when the Fibonacci algorism reaches a number greater or equal to a given input number. The example starts with the numbers 0 and 1 the :myFibo function calls itself recursively to calculate the next Fibonacci number until it finds the Fibonacci number greater or equal .

The first argument of the myFibo function is the name of the variable to store the output in. This variable must be initialized to the Fibonacci number to start with and will be used as current Fibonacci number when calling the function and will be set to the subsequent Fibonacci number when the function returns. Script:

Script Output: Script Output The next Fibonacci number greater or equal is . Press any key to continue . . . Summary - Defining a standard format for a DOS batch functionDescription: With the information learned in this section we can define a standard format for a DOS batch functions as shown below. Also check out the rich set of ready to use DOS functions provided by the DosTips.com function library. Script:

出处:

批处理bat命令 获取当前盘符和当前目录和上级目录的代码 批处理命令获取当前盘符和当前目录%~d0是当前盘符%cd%是当前目录可以用echo%cd%进行打印测试以下例子是命令行编译VisualStudio编写的程序:@echooffsetb=%cd%//

ECHO 不换行实现方法 我想用批处理实现向s.txt中多次分别导入文本例如:aaaa","bbbb","cccc","dddd"实现s.txt内效果如:aaaabbbbccccdddd可是echo命令每次导入的时候会强制换行,如:aaa

CMD中使用attrib命令设置文件只读、隐藏属性详解 本文介绍一个cmd下的一个attrib.exe的小程序,它可以用来设置文件的属性。我们知道文件的属性有只读、隐藏、系统、存档和无内容索引等5个,只读和隐

标签: dos命令批量处理文件

本文链接地址:https://www.jiuchutong.com/biancheng/371857.html 转载请保留说明!

上一篇:批处理bat计算两个时间差(bat批处理命令大全)

下一篇:批处理bat命令 获取当前盘符和当前目录和上级目录的代码(批处理 /b)

  • 旅游业成本交税会计分录怎么做
  • 如何计算转让金融商品盘亏应交增值税
  • 子公司自购买日开始持续计算的净资产包括商誉吗
  • 企业罚职工的钱违法吗?
  • 个人经营所得税减免税优惠政策
  • 税务局登录账号是多少
  • 利润表里的所得税怎么算
  • 网上报税超过了时间还能报吗
  • 申报入库税款怎么分税种发给税管员
  • 公司名称变更发票还能认证吗
  • 房地产经营税金计算公式
  • 税收制度具体内容
  • 新个税法案专项扣除如何实施
  • 购税控盘账务处理
  • 0退税产品怎么征税
  • 报无票收入又开发票了怎么办?
  • 预付材料款会计分录
  • 企业土地是不是都应该缴纳房产税?
  • 冷库租赁收入税率
  • 在下列哪种情况下
  • 不抵扣勾选怎么挽回
  • 网络科技公司会计核算及账务处理
  • 涉农和中小企业贷款分类证明没有就不能进行税前扣除么
  • 公司清算补偿工资标准
  • 留存收益转增股本,另一方公司怎么做账
  • 生产领用产成品验证会计分录
  • 只交社保不发工资可以吗
  • 建筑工程购买的家用清洁电器能勾选认证吗
  • 网络限速数值
  • 应交所得税和所得税费用的区别计算公式
  • 重置edge浏览器设置
  • win10创意者更新易升
  • 离职补偿金如何缴纳个人所得税?
  • 2022年增值税免税政策
  • php 二维数组
  • 外购产品会计分录
  • Php实现邀请用户加入企业生成邀请链接
  • 公司注销未分配利润会计分录
  • 会计凭证附件规范要求
  • thinkphp ide
  • 年度成本费用总额公式
  • 持续集成的步骤
  • 发票认证相符什么意思
  • 购买方未抵扣,销售方申请红字
  • 替票报销开什么发票
  • wallengine
  • 货款分批付的会计分录
  • 设备购入后又退出怎么办
  • 固定资产盘亏要改变资产原值吗
  • SQL Server 2008 R2 超详细安装图文教程
  • 以前年度损益调整属于哪类科目
  • 一年内到期的非流动资产
  • 税金及附加需不需要计提
  • 入股投资的钱能取出来吗
  • 长期股权投资与其他权益工具投资的区别
  • 经纪代理包括什么
  • 自产货物赠送客户账务处理
  • 原始凭证分类的依据有什么
  • 普通发票和增值发票的区别在哪里
  • 收到伙食费的会计处理
  • 小企业建账选哪种会计制度
  • sql注入神器
  • mysql缓冲区
  • mysql 源码 下载
  • win2008r2安装ftp
  • 如何在卸载程序中隐藏已安装程序
  • win10系统如何将c盘的软件移到d盘
  • Windows Server 2003下DHCP服务器的安装与简单配置图文教程
  • 苹果电脑如何查看WiFi密码
  • ebr.ahrcu
  • windows1021h2更新
  • macbook截
  • windows10预览版怎么样
  • Unity3D & Java 基于 Protobuf 通信实现
  • 遮罩层出现后怎么点击其他地方隐藏
  • unity shaders and effects cookbook
  • Unity3D面试题整合
  • unity平移场景视图怎样操作?
  • 控制程序的先后顺序是怎样的
  • 税务总局2013年65号公告
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设