将许多文件中的子字符串打印到单个文本文件中

时间:2018-02-28 10:13:35

标签: powershell substring

我需要从单个文件夹中的许多文件中提取第20到30个字母,然后将结果打印到文本文件中。 我知道我需要使用using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace practise_Delegates_MenuDriven { class Program { delegate int Converter(int value); static int ToCel(int fah) { return fah; } static int ToFah(int celsius) { return celsius; } static int ToKel(int celsius2) { return celsius2; } static int ToRank(int celsius3) { return celsius3; } static void DisplayMenu() { Console.WriteLine("Celsius Converter"); Console.WriteLine("================="); Console.WriteLine("C - Celsius to Fahrenheit"); Console.WriteLine("F - Fahrenheit to Celsius"); Console.WriteLine("K - Celsius to Kelvin"); Console.WriteLine("R - Celsius to Rankine"); Console.WriteLine("X - Exit"); } static char GetMenuOption(char letter) { Console.WriteLine("Please enter C, F, K, R, X: "); letter = Convert.ToChar(Console.ReadLine()); return letter; } static int GetValue(int value) { Console.WriteLine("Please enter the value to convert: "); value = Convert.ToInt32(Console.ReadLine()); return value; } static void PrintResult(int fromValue,int result,string fromstring,string toString) { Console.WriteLine(fromValue + " " + fromstring + " is " + result + " " + toString); } static void Main(string[] args) { Converter process = ToCel; Converter process1 = ToCel; Converter process2 = ToKel; Converter process3 = ToKel; ***switch statement? while loop?*** } } 但是无法管理多个文件。 我为单个文件使用创建了一个工作脚本:

substring(20,10)

但现在我需要处理多个文件。 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

这可能有效

#Path to directory 
$DirPath = "C:\Files"
#get all text files from above path
$Files = Get-ChildItem -Path $DirPath -Filter "*.txt"
#path to text file where strings are stored
$DesFile = "C:\String.txt"

#loop through all files and save to desfile
foreach ($txt in $Files)
{
    $var = Get-Content -Path $txt.FullName
    $var.Substring(20,10) | Add-Content -Path $DesFile
}