python编写自动化脚本工具_这些自动化场景,批处理脚本完全可以取代 Python!...

1. 前言

提到自动化解决方案,相信大部分人会想到用 Python 语言,只需要根据功能场景,编写 Python 脚本即可。

369f49ce7f9721facda9c960f2ebf506.jpg-wh_651x-s_360139536.jpg

相反,PC 端的 Batch 批处理 似乎快被忘记了,很多人对它嗤之以鼻,认为这么古老的脚本语言貌似没什么用,Python 似乎可以取而代之。

相比 Python 脚本,Batch 批处理脚本在某些场景下,使用更简洁、方便、高效,即写即用,不需要依赖 Python 环境,并且可以完全摆脱打包等繁琐步骤。

2. 批处理

基础新建批处理脚本很简单,只需要新建一个文本文件,然后修改编码方式为:ANSI 编码,接着编写脚本逻辑代码,最后保存文件以 .bat 结尾即可。

690f0cc97c3aecfad235cf9493c08d85.jpg-wh_600x-s_2771927990.jpg

Batch 常用命令包含:echo、::/rem、title/color、cd/md/dir、rd/del/copy、pause、goto、for、if、set、start等。

其中:

@echo off 代表在本行开始关闭回显,不显示正在执行的批处理命令及执行结果,一般放在批处理文件第一行。

echo 日志参数:用于在控制台输出日志,偏于理解脚本执行逻辑

::/rem 注释内容:注释内命令

title/color:设置窗体标题和背景颜色

cd:切换目录

md:创建目录

dir:显示文件夹的内容

rd:删除一个目录

del 删除模式 文件:删除文件。通过配置删除模式,可以删除任意文件,包含隐藏、只读、系统文件

copy:拷贝文件

pause:暂停命令,一般放在批处理文件最后一行

goto:跳转命令,一般和「 : 任务名称 」搭配使用,执行一个循环任务,实例见第 3 节

for:循环命令,和 Python 中的 for 语法类似

if:判断命令

set:设置一个变量

start:调用外部程序的命令

3. 实用场景

下面聊聊几个批处理比较实用的场景:

1、对文件夹或桌面下的文件进行分类,然后放置到不同的文件夹内,方便归纳管理

@echooff

for%%iin(*) do (md %%~xi

move*%%~xi %%~xi)

pause

你没看错,只需要 4 行代码其中,for 用于遍历当前文件夹,遍历的结果用 do 分别去执行后面的命令%%~xi 是截取 %%i 的扩展名,使用 md 命令新建一个文件夹move 的作用是:将源文件移动到新的文件夹中。

2、删除当前目录(包含子目录)下所有的 build 文件夹

使用 Android Studio 编译后,如果项目存在多个 Module,可能会存在多个 build 文件夹,可以使用下面的批处理脚本一键删除。

@echooff

:: 打开到当前目录下

cd /d "%~dp0"

echo 开始删除

:: 循环删除

for/r /D %%iin(*build*) do rd /s /q"%%i"

echo 删除完成

pause

脚本很简单,只需要打开项目根目录,利用「 for + do 」循环遍历,根据匹配规则删除文件即可。

其中:%~dp0:批处理文件当前目录/s:从所有子目录下删除文件。

/q:指定以「 安静模式 」执行删除操作,删除不需要确认相比 Python 脚本,批处理脚本代码更简洁。

3、执行 Python 脚本定时任务

比如,我编写完一个 Python 采集爬虫,我想 5 分钟执行一次,这里可以使用 goto 命令。

@echooff

title 循环运行Python代码

:: 5分钟执行一次,单位为s

setINTERVAL=300

:: 提前执行一次,把执行时间打印出来

echo 开始执行 - %time%

python C:/test.py

:: 使用timeout进行倒计时

timeout %INTERVAL%

:: 新建一个任务

:Task

echo 开始执行 - %time%

python C:/test.py

timeout %INTERVAL%

:: 使用goto命令,开始跳转到上面的任务,开始执行

gotoTask

4、Git 提交代码

正常使用 git 命令行提交代码( 不使用 IDE ),需要使用 git add .、git commit -m 提交日志、git pull、git push 四条命令。

使用批处理脚本,只需要双击一下,输入提交日志就完事了。

具体代码如下:

@echooff

title 提交代码

echo 提交代码,简化操作

:: 状态

git status

:: set:等待输入,赋值给变量msg

set/p commit_msg=代码提交注释:

:: 提交代码的 4 条命令

git add.

git commit-m %commit_msg%

git pull

git push

echo 提交成功

pause

5、清除系统垃圾文件

指定删除模式、待删除的路径,调用 del 命令去删除即可。

@echooff

:: 配置

title Alic Feng batTool forClean

color 03

mode con cols=42 lines=20

echo executes cleaning,Please waiting...

::程序删除系统无用文件开始

del /f /s /q %systemdrive%\*.tmp 1>nul 2>nul

del /f /s /q %systemdrive%\*._mp 1>nul 2>nul

del /f /s /q %systemdrive%\*.log 1>nul 2>nul

del /f /s /q %systemdrive%\*.gid 1>nul 2>nul

del /f /s /q %systemdrive%\*.chk 1>nul 2>nul

del /f /s /q %systemdrive%\*.old 1>nul 2>nul

del /f /s /q %systemdrive%\recycled\*.* 1>nul 2>nul

del /f /s /q %windir%\*.bak 1>nul 2>nul

del /f /s /q %windir%\prefetch\*.* 1>nul 2>nul

del /f /s /q %windir%\temp\*.* 1>nul 2>nul

del /f /q %userprofile%\cookies\*.* 1>nul 2>nul

del /f /q %userprofile%\recent\*.* 1>nul 2>nul

del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\*.*"1>nul 2>nul

del /f /s /q "%userprofile%\Local Settings\Temp\*.*"1>nul 2>nul

del /f /s /q "%userprofile%\recent\*.*"1>nul 2>nul

::删除系统垃圾文件结束

echo 清除系统垃圾完成!!!

echo. & pause

4. 最后

上面几个简单的小功能都是使用批处理脚本编写的,实际上,Python 与批处理各有各的优势。

Python 语法更加优雅易懂,大量的依赖库可以直接使用;批处理在 PC 端某些特点的场景,无论是编写还是使用,都有它特定的优势。

当然,可以使用转换工具将批处理脚本转为 EXE 可执行程序,供 Python 去调用,完成一些复杂的自动化任务。

【编辑推荐】

【责任编辑:华轩 TEL:(010)68476606】

点赞 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Git指令的Shell脚本,能够快速便捷地管理Git库,包括添加修改、提交修改、显示库状态、推送到远程库、从远程库更新到本地、版本恢复等操作。 使用方法: 1. 在Linux系统中,将本文件放在Git库目录下,利用Shell运行本文件; 2.在windows系统中,需下载安装与操作系统相对应的Git软件,并将本文件放在Git库目录下,双击即可运行。 运行示例: Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? h A: Add all changes to repository. C: Commit all changes to repository. D: Show differences between current version with HEAD->. E: Exit shell script. F: Fetch origin/master and merge. L: Show latest two-weeks logs of repository. P: Push commissions to origin/master. U: User command mode(Press ‘Enter’ to exit). R: Reset current version according version_id. S: Show status of repository. Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? s On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Git.sh modified: PyNote/PyNote_2.md no changes added to commit (use "git add" and/or "git commit -a") Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? a On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: Git.sh modified: PyNote/PyNote_2.md
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值