批处理-FOR 实际应用

FOR - Loop through a set of files in one folder.
FOR /R - Loop through files (recurse subfolders) .
FOR /D - Loop through several folders.
FOR /L - Loop through a range of numbers.
FOR /F - Loop through items in a text file.
FOR /F - Loop through the output of a command.

遍历文件夹中所有指定类型文件,查找字符串并输出结果到指定文件

@echo off

for /R %%i in (Log*.txt) do (
	echo %%i
	for /F "tokens=1,2 delims=" %%j in ('findstr /r "voltage" %%i') do (
		REM 此处要在文本中查找字符串,需要在括号中使用表达式 findstr
		echo %%j
		echo %%i >> 1.txt && echo %%j >> 1.txt
))

pause

遍历文件夹中所有指定类型文件,切分路径后拼接新名存入指定文件夹

@echo off
cd %~dp0
md Screen_pic
for /R %%i in (camera*.png) do (
	echo %%i
	for /F "tokens=4,5* delims=\" %%a in ("%%i") do (
		REM 4,5 代表取第四列第五列
		REM 此处 %%i 为字符串,需加双引号,否则无法正确切分
		REM echo %%a %%b
		REM echo %%i
		REM echo .\Screen_pic\%%a_%%b
		copy %%i .\Screen_pic\%%a_%%b
))

pause
posted @ 2022-02-11 14:48  可乐芬达  阅读(100)  评论(0编辑  收藏  举报