Batch delete files under Windows

Recently, when I use Maven, the download of the jar package fails, and then I need to delete it and download it again, but sometimes there are too many files, and it takes too much time to delete one by one, so it is very comfortable to use bat batch processing.

I haven't encountered the syntax of bat before, and then I learned and experimented, and wrote a crude version of the batch file.

My train of thought is:

  • 1. Traverse the target folder.
  • 2. After finding the target file, delete the folder (by observing the jar that failed to download, it can be known that when a file with the suffix sha1-in-progress appears, the file download fails).

I learned a lot of knowledge from writing a batch script to help with work – traversing folders & string processing This blog, thank you very much for sharing.

traverse folders

for loop to traverse folders

Order:for [参数] %%变量名 in (匹配符) do (执行的命令)

parameters can be divided into/d,/r,/l,/f,无参

  • No parameter: traverse the files in the current folder
  • /d : Traverse the subfolders under the current folder
  • /r : Deeply traverse all files in the current folder
  • /l : iterate over a range of values, using an iteration variable to set a start value, then step through a set of values ​​in the range until the value exceeds the set end value
  • /f : for parsing file content

For example:

for /r C:\Users\Panlf\.m2\repository %%i in (*.sha1-in-progress) do (
	echo %%i
)

The meaning of the above is to deeply traverse C:\Users\Panlf\.m2\repositoryall the files under the file, find out sha1-in-progressthe file with the file suffix name and print it.

Of course, you can also output to a file:

for /r C:\Users\Panlf\.m2\repository %%i in (*.sha1-in-progress) do (
	echo %%i >> E:\m.txt
)

You can output the result to a file Eon the disk m.txt, but be careful not to use it >, otherwise it will only output a result to the file and must be used >>.

delete folder

The above is to traverse the files, and now to delete the folder.

bat delete has two commands rd, del.

命令:rd,删除目录 

参数:/s
说明:用于删除目录树,即删除目录及目录下的所有子目录和文件。 

参数:/q XP中的增强DOS命令 
说明:进行删除操作时将取消确认。 

命令:del,删除文件或目录 
参数:/f 
说明:使用此参数可删除只读文件。 

参数:/s、/q
说明:即删除目录及目录下的所有子目录和文件。同时使用参数/Q,可取消删除操作时的系统确认。 

参数:/a 
说明:删除指定属性或指定属性以外的文件,/AR、/AH、/AS、/AA表示删除只读、隐藏、系统、存档文件,
/A-R、/A-H、/A-S、/A-A表示删除除只读、隐藏、系统、存档以外的文件。 

I ended up with a crude version:

@echo off
setlocal enabledelayedexpansion
set /p input_path=请输入要要操作的文件夹:
echo 获取到的地址:%input_path%
for /r %input_path% %%i in (*.sha1-in-progress) do (
	set pan_name=%%~di
	set del_name=%%~pi
	set true_name=!pan_name!!del_name!
	echo !true_name!
	rd /s /q !true_name!
)
pause

Notice

1. %%iAccording to different forusage methods, the content is also different:

%%~fi:表示获取该文件的绝对路径信息
%%~di:表示获取该文件所在的盘符
%%~pi:表示获取该文件的路径,不包含盘符的信息
%%~ni:表示获取该文件的文件名,不包含扩展名信息
%%~xi:表示获取该文件的扩展名
%%~ti:表示获取该文件的上次修改时间
%%~zi:表示获取该文件的大小

2. When defining temporary variables in the for loop

在for循环定义set pan_name=%%~di,如果直接echo pan_name会报echo关闭状态,不能使用。
所以需要第一启用变量延迟功能:setlocal enabledelayedexpansion,第二调用变量的时候使用!pan_name!

3. String concatenation

set true_name=!pan_name!!del_name!

Guess you like

Origin blog.csdn.net/flash_love/article/details/132181121