Bat batch script program <1> Basics

original

Due to my own reasons, I have been at home with my children for the past six months, and I haven’t written a blog for a long time. Recently, I need to copy and copy the part-time work at home. I suddenly became interested and wanted to study batch processing. I feel that it is really convenient to use sometimes.
Create a new text directly, change the suffix to .bat to build a batch program, and then open it with text to write. After searching, I found that iBat is a very good software for editing, very foolish and convenient, and can write batches without command deal with.
Learning objectives: be able to understand Bat files and modify files.



DOS batch command is a scripting language, which is executed line by line according to the top-down process, that is to say, only after the command of the first line is executed, can the second line be executed, and after the second line is executed, Then execute the third line.


[Basic]   
echo off //Close the echo, generally written at the beginning of the batch, indicating that only the command result is displayed, and the executed command is not displayed. If @, @echo off is added in front, then this command itself does not display echo on
/ /Contrary to the above
echo. //Followed by a decimal point, display a blank line
echo //Space followed by text, display text
 
rem //Comment, if the echo is turned on, the content of the comment will be displayed
:: //Comment, complete comment is not Display and echo  
cd c:\ //Jump to C drive 
/? //After the command, /? will prompt the usage and parameters such as choice/?
>nul //The command is connected at the end, without displaying the result and prompt
pause //Pause, Display please press any key to continue, pause>nul does not display text
ping //followed by IP, display delay 
title //window title bar name
color 0A //text green background black
exit //exit batch program, and exit CMD console
cls //clean screen
errorlevel //built-in error level variable 0 No error Other value error
%0 //Script itself

start /max "" "D:\cloudmusic.exe" //Run the program with the largest window
echo text>c:\test.txt (text>>c:\test.exe txt) // Create txt text > If the file exists, it will be overwritten >> If it exists, it will be accumulated and written to

[folder]
md (mkdir) c:\folder//Create folder (..\folder//Create to batch In the upper-level directory where the processing file is located)
ren (rename) c:\folder new name //rename the folder
rd (redir) /s /qc:\folder//delete the folder ( /s delete subdirectories and files /q do not ask)
xcopy /e /yc:\folder D:\folder\ //copy folder (/e copies all subdirectories, including empty directories. /y overwrites files)
copy                                                //copy folder

del //delete file (del 0% delete itself)


【set】

%variable name% //%% is equivalent to a variable reference, such as echo %time% to output the current time

set /p aaa=Please enter a number: //Prompt the user to input and assign the value to aaa
set /a bbb=%aaa% *2 //Execute mathematical operations, bbb is equal to aaa*2


[goto]
goto a //goto jump command a label name specified to jump to the label position and continue to execute the script
: a //Define the label
echo You came with the goto command Label a
 
goto :eof (the built-in End Of File comes to the last line of the program, which is equivalent to exiting the current Bat script, but it will wait for the next command if it does not exit the CMD console) [if] Note: there is no


else
if
if exists c:\learning.txt (echo exists) ELSE (echo does not exist) //whether exists
if not exists c:\learning.txt (echo does not exist) ELSE (echo exists) //does not exist


[& && || | 】
& Compound statement connector, separating multiple commands in a command line
&& Compound statement connector, the command after && will be executed only when the command before && succeeds.
|| Compound statement connector, when the command before || fails, the command after || will be executed.
The | pipe symbol, the output of the process before | is used as the input of the process after |.


【Delay execution】
1. Because the ping command is executed once a second - n represents the number of times 127.1 is the abbreviation of the loopback address of the local network system 127.0.0.1 > nul does not display the result of ping, so the execution is delayed for 5 seconds

ping 127.1>nul -n 5  

2.choice /t Pause for seconds/d The option selected after the time is up/n Hide the option list >nul does not display the result
choice /t 5 /dy /n >nul


example:

@echo off
rem 这是一行注释,开启回显会显示
:: 这也是一行注释,开启回显也不显示
echo.
echo 现在开始Bat基础学习
echo.

title Bat基础学习
color 0A

pause 

:StartStudy
::清理屏幕
cls
echo.
echo  0.关闭Bat
echo  1.当前时间
echo  2.打开网易云音乐
echo  3.测试延迟
echo  4.创建txt文本
echo  5.文件夹操作

echo.
set /p choice=请选择练习的内容:

if %choice%==0 goto end
if %choice%==1 goto a
if %choice%==2 goto b
if %choice%==3 goto c
if %choice%==4 goto d
if %choice%==5 goto e

rem 0.关闭Bat
:end
exit

rem 1.当前时间
::表示从左向右指针向右偏0位,然后从指针偏移到的位置开始提取8位字符
:a
echo.
echo 当前时间是: %date% %time:~0,8% 
echo.
pause
goto StartStudy

rem 2.打开网易云音乐
::需要修改路径
:b 
echo.
start   /max "" "D:\软件\CloudMusic\cloudmusic.exe"
echo.
pause
goto StartStudy

rem 3.测试延迟
:: 出现错误再次输入IP
:c
set /p ip=请输入要ping的IP:
ping %ip%

if not %errorlevel% == 0 (
   echo.
   echo Ping对方的IP出现错误
   goto a
)
echo.
pause
goto StartStudy

rem 4.创建txt文本
:d 
echo.
set /p txtPath=请输入要创建的文本路径: 
set /p txtName=请输入要创建的文本名:
set /p txtContent=请输入文本内容:
echo.
echo %txtContent% >> %txtPath%/%txtName%.txt  &&  echo 创建成功! || echo 创建失败!

echo.
pause
goto StartStudy

rem 5.文件夹操作
::需要修改路径
:e 
::创建文件夹
echo.
echo 开始创建文件夹 

if not exist F:\我的文件夹 (
md  F:\我的文件夹 &&  echo F:\我的文件夹 创建成功! || echo 创建失败!
)else (
 echo 已经存在 F:\我的文件夹,不需要创建!
)
echo.

::延迟3秒 重命名文件夹
echo 按下任意键,将在3秒后把 我的文件夹 改名为 我的改名文件夹
pause>nul
ping 127.0.0.1>nul -n 3 
ren F:\我的文件夹 我的改名文件夹 && echo 改名成功! || echo 改名失败!

::延迟3秒 复制文件夹 /e 复制所有子目录,包括空目录。/y 覆盖文件
echo 按下任意键,将在3秒后把 我的改名文件夹 复制到 D盘 下
pause>nul
choice /t 3 /d y /n >nul
xcopy "F:\我的改名文件夹" "D:\我的改名文件夹\" /e /y && echo D:\改名后的文件夹 复制成功! || echo 复制失败!

::延迟3秒 删除文件夹 /s 是否删除子目录 /q 是否询问
echo 按下任意键,将在3秒后删除创建的文件夹
pause>nul
ping 127.0.0.1>nul -n 3
rd /s /q D:\我的改名文件夹 && rd /s /q F:\我的改名文件夹  && rd /s /q F:\我的文件夹 &&  echo 删除成功! 

echo.
pause
goto StartStudy

Script download:

http://download.csdn.net/download/u012322710/10190451


Guess you like

Origin blog.csdn.net/u012322710/article/details/78924578