问题描述
在收到批评家没有以正确的方式提出问题的批评之后,我又以我认为更好的方式回来了.我正在尝试解决必须写一个批处理文件而被卡住的作业问题.
after receiving critics for not asking the question the right way, I have returned again with what I think is better way. I am trying to solve homework problem in which I have to write a batch file and I am stuck.
在CMD中调用此批处理文件时,它将收到4个参数:
When this batch file is called in CMD it receives 4 parameters:
- (1)某些目录的路径,该目录以\结尾;例如:C:\ Users \ PC \ Desktop \
- (2)文件扩展名;例如:txt
- (3)一些字符串;例如:"ab"
- (4)一个整数;示例:3
此批处理文件的目的是在本地目录中查找所有扩展名为(2)的文件X,以及(1)接收的所有子目录,其中所有子目录(3)中包含子字符串(3)的行超过(4)行,并进行写操作在控制台下一句话:
The purpose of this batch file is to find all files X with extension (2) on local directory and all subdirectories received by (1), which have more than (4) lines in them that have substring (3) and write in console next sentence:
文件X具有超过(4)个留置权,更精确的Y,具有子串(3)".其中X是满足这些条件的文件的完整路径,而Y是在其中找到子字符串的确切行数.
"File X has more than (4) liens, more precise Y, which have substring (3)". Where X is full path of the file that fulfills those conditions and Y exact number of lines in which substring is found.
控制台中的输入应如下所示:myfile.bat C:\ Users \ PC \ Desktop \ txt ab 3
Input in console should be like this: myfile.bat C:\Users\PC\Desktop\ txt ab 3
我解决这个问题的尝试是这样的:
My attempt of solving this was like this:
@echo off
for %%a in ('dir /s %1*.%2') do (
for /f "delims= " %%G in ('find /c "%3" ^<%%a') do (
set linecount=%%G
if %linecount% gtr %4 (
echo File %%a has more than %4 lines, more precisley %linecount% which have substring %3
)
)
)
希望这次我做的部分正确.
Hope this time I've done it partially right.
推荐答案
根据您到目前为止的问题参数,仅此而已,这是一个单行批处理文件解决方案,(测试非常有限):
Based on your question parameters to date, and just for the hell of it, here's a one line batch file solution, (with very limited testing):
@For /F "Delims=" %%A In ('Dir/B/S/A-D-S-L "%~1*.%2"') Do @For /F "Tokens=3Delims=:" %%B In ('Find /I /C "%~3" "%%A"') Do @If %%B Gtr %4 Echo File %%A has more than %4 lines, more precisley%%B, which have substring %3
此命令使用Where
命令(我更喜欢它,因为它不适用于标记到目录名称的尾随反斜杠):
@For /F "Delims=" %%A In ('Where/R "%~1" *.%2') Do @For /F "Tokens=3Delims=:" %%B In ('Find /I /C "%~3" "%%A"') Do @If %%B Gtr %4 Echo File %%A has more than %4 lines, more precisley%%B, which have substring %3
这篇关于将结果从FIND发送到IF中的cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!