首先,我想为目录中的所有文本文件生成目录列表.接下来将获取每个文本文件和获取内容.然后我想浏览内容并搜索与第一个文件共享内容的另一个目录中的所有文本文件,并将这些相应的匹配输出到源文件后面的文件.
我对这一切都很陌生,并意识到我错过了一些严肃的基本知识块.我所拥有的小脚本体验是在Javascript中,它似乎并不完全可转移.(虽然编程是编程,但我被告知.)
这是我到目前为止:
$max = get-content h:test1\one.txt | Measure-Object
$A = get-content h:test1\one.txt
For($i=0; $i -lt $max.count ; $i++){
select-string h:test2\*.txt -pattern $($A[$i]) | Format-Table | Out-File ($i + '.txt')
}
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西:
$max = get-content $files[i] | Measure-Object
$A = get-content files[i]
For($j=0; $j -lt $max.count ; $j++){
select-string h:test2\*.txt -pattern $($A[$j]) | Format-Table | Out-File($files[i].basename + $j + '.txt')
}
Run Code Online (Sandbox Code Playgroud)
任何和所有的帮助将非常感激,
裴家
所以
第1册(one.txt)
法国首都是巴黎.
巴黎的人口是十二.
第2册(two.txt)
法国是一个美丽的国家.
法国首都是巴黎.
我基本上想要一个报告,即two.txt与one.txt共享一行.
首先,我想为目录中的所有文本文件生成目录列表
这是如何做:
$textFiles1 = dir -Path C:\Books1 -Filter *.txt
$textFiles2 = dir -Path C:\Books2 -Filter *.txt
Run Code Online (Sandbox Code Playgroud)
- 接下来将获取每个文本文件和获取内容.
- 我想看看第一本书中的任何一行是否都出现在其他任何一本书中.
这是一个执行此操作的算法(未经测试)(已测试):
foreach ($textFile in $textFiles1) {
$lines = get-content -Path $textFile
foreach ($line in $lines) {
foreach ($textFile2 in $textFiles2) {
$lines2 = get-content -Path $textFile2
if ($lines2 -contains $line) {
$matchMessage = 'Line: "{0}" is duplicated in "{1}".' -f $line, $textFile2
$matchMessage | out-file C:\report.txt -encoding UTF8 -Append
}
}
}
}
notepad C:\report.txt
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
187 次 |
最近记录: |