Powershell中的多个匹配内容

时间:2019-12-25 17:22:50

标签: powershell robocopy

我想看看是否有任何行包含单词“ Newer”和/或“ New File”。我已经尝试过以下脚本,但是没有运气。任何见识将不胜感激。

#include<iostream>
#include <fstream>
#include<ctime>
#include<conio.h>
#include<string.h>
using namespace std;



//FUNCTION DECLERATION
void WelcomeScreen();//WelcomeScreen function decleration
void Title();//Title function decleration
void MainMenu();//MainMenu function decleration
void LoginScreen();//LoginScreen function decleration
void Add_patient();// add patient
void func_list();//function to list the patient details
void Search_rec();//Search_rec function declaration
void search_p();   //void Edit_rec();//Edit_rec function declarationvoid
  //void Dlt_patient();//Dlt_rec function declaration
  //void ex_it(void);//exit function declaration



struct patient//structure
{
int id;
int age;
char Gender;
char First_Name[20];
char Last_Name[20];
char Contact_no[15];
char Address[60];
char Email[30];
char Doctor[20];
char Problem[20];
patient* next;
patient* prev;
// char time [];

};

patient* patient_head = NULL;


/* ************************************************* Welcome Screen ********************************************* */
void WelcomeScreen() //function for welcome screen
{

cout << "\n\n\n\n\n\n\n\t\t\t\t#########################################";
cout << "\n\t\t\t\t#\t\t WELCOME TO             #";
cout << "\n\t\t\t\t#   ZS HOSPITAL MANAGEMENT SYSTEM       #";
cout << "\n\t\t\t\t#########################################";
cout << "\n\n\n\n\n Press any key to continue......\n";
system("cls");//Use to clear screen

}

/* ************************************************* Title Screen ********************************************* */
void Title()//function for title screen
{
cout << "\n\n\t\t----------------------------------------------------------------------------------";
cout << "\n\t\t\t\t       ZSc HOSPITAL         ";
cout << "\n\t\t----------------------------------------------------------------------------------";
}


/* ************************************************* Main Menu ********************************************* */
void MainMenu()//function decleration
{
system("cls");
int choose = 0;
Title();// call Title function
cout << "\n\n\n\n\n\t\t\t\t1. Add Patients Record\n";
cout << "\n\t\t\t\t2. Delete Patients Record\n";
cout << "\n\t\t\t\t3. Search Patients Record\n";
cout << "\n\t\t\t\t4. Edit Patients Record\n";
cout << "\n\t\t\t\t5. list Patients Record\n";
cout << "\n\t\t\t\t6. search Patients by doctors name\n";
cout << "\n\t\t\t\t7. Exit\n";
cout << "\n\n\n \n\t\t\t\tChoose from 1 to 7:";
cin >> choose;

switch (choose)//switch to differeht case
{

case 1:
Add_patient();//Add_rec function is called
break;
case 2:
search_p();
//Dlt_patient();//Dlt_rec function is call
break;
case 3:
Search_rec();//View_rec function is call
break;
case 4:
// Edit_rec();//Edit_rec function is call
break;
case 5:
func_list();
break;
case 6:
// ex_it();//ex_it function is call
break;
case 7:
break;
default:
cout << "\t\t\tInvalid entry. Please enter right option :)";
system("pause");
}//end of switch
system("pause");

}

void LoginScreen()//function for login screen
{
//list of variables
int e = 0;
char Username[15];
char Password[15];//cin.getline(Username,15);
char original_Username[15] = "zain";
char original_Password[15] = "123";

do
{
cout << "\n\n\n\n\t\t\t\tEnter your Username and Password :)";
cout << "\n\n\n\t\t\t\t\tUSERNAME:";
cin.getline(Username, 15);
cin.ignore();
cout << "\n\n\t\t\t\t\tPASSWORD:";
cin.getline(Password, 15);
//cin.ignore();


if (strcmp(Username, original_Username) == 0 && strcmp(Password, original_Password) == 0)//check if both the strings are equal and return 0 if equal
{
cout << "\n\n\n\t\t\t\t\t...Login Successfull...";
system("pause");
MainMenu();
break;
}
else
{
system("cls");
cout << "\n\t\t\tPassword in incorrect:<< Try Again :)";
e++;
}

} while (e <= 2);

if (e>2)
{
cout << "You have cross the limit. You cannot login. : :(";
//ex_it();
}

system("cls");
}


void Add_patient()
{

int c = 0, new_patient = 0;
patient* obj = new patient();
if (patient_head == NULL)
{

patient_head = obj;

}
else
{

obj->next = patient_head;
obj->prev = NULL;
patient_head = obj;
}
system("cls");
Title();// call Title function
//list of variables
char ans;

/****************************************id****************************/


/* **************************First Name*********************************** */
A:
cout << "\n\t\t\tFirst Name: ";
cin >> obj->First_Name;
obj->First_Name[0] = toupper(obj->First_Name[0]);
int valid;

if (strlen(obj->First_Name)>20 || strlen(obj->First_Name)<2)
{
cout << "\n\t Invalid :( \t The max range for first name is 20 and min range is 2 :)";
goto A;
}
else
{
for (int b = 0; (unsigned)b<(unsigned)strlen(obj->First_Name); b++)
{
if (isalpha(obj->First_Name[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t First name contain Invalid character :(  Enter again :)";
goto A;
}
}

/* ********************************************** Last name ************************************************ */
B:
cout << "\n\t\t\tLast Name: ";
cin >> obj->Last_Name;
obj->Last_Name[0] = toupper(obj->Last_Name[0]);
if (strlen(obj->Last_Name)>20 || strlen(obj->Last_Name)<2)//strlen to check length of char
{
cout << "\n\t Invalid :( \t The max range for last name is 20 and min range is 2 :)";
goto B;
}
else
{
for (int b = 0; (unsigned)b<(unsigned)strlen(obj->Last_Name); b++)
{
if (isalpha(obj->Last_Name[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t Last name contain Invalid character :(  Enter again :)";
goto B;
}
}
/* ******************************************* Gender ************************************************************** */
int ok;

do
{
cout << "\n\t\t\tGender[F/M]: ";
cin >> obj->Gender;
if (toupper(obj->Gender) == 'M' || toupper(obj->Gender) == 'F')
{
ok = 1;
}
else
{
ok = 0;
}
if (!ok)
{
cout << "\n\t\t Gender contain Invalid character :(  Enter either F or M :)";
}
} while (!ok);
/* ***************************************** Age ********************************************************************** */
cout << "\n\t\t\tAge:";
cin >> obj->age;
/* **************************************** Address ******************************************************************* */
do
{
C:
cout << "\n\t\t\tAddress: ";
cin.getline(obj->Address, 80);
cin.ignore(); //clear buffer before taking new
obj->Address[0] = toupper(obj->Address[0]);

if (strlen(obj->Address)<4)
{
cout << "\n\t Invalid :( \t The max range for address is 40 and min range is 4 :)";
goto C;
}

} while (!valid);

/* ******************************************* Contact no. ***************************************** */
// char ch ;
// ch = getchar();

do
{
D:
cout << "\n\t\t\tContact no: ";
cin >> obj->Contact_no;
if (strlen(obj->Contact_no)>11)
{
cout << "\n\t Sorry :( Invalid. Contact no. must contain 11 numbers. Enter again :)";
goto D;
}
else
{
for (int b = 0; (unsigned)b<(unsigned)strlen(obj->Contact_no); b++)
{
if (!isalpha(obj->Contact_no[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t Contact no. contain Invalid character :(  Enter again :)";
goto D;
}
}
} while (!valid);

/* ************************************************** Email ******************************************** */
do
{
cout << "\n\t\t\tEmail: ";
cin >> obj->Email;
if (strlen(obj->Email)>30 || strlen(obj->Email)<8)
{
cout << "\n\t Invalid :( \t The max range for email is 30 and min range is 8 :)";
}
} while (strlen(obj->Email)>30 || strlen(obj->Email)<8);

/* ********************************************************* Problem *********************************************** */
E:
cout << "\n\t\t\tProblem: ";
cin >> obj->Problem;
obj->Problem[0] = toupper(obj->Problem[0]);
if (strlen(obj->Problem)>15 || strlen(obj->Problem)<3)
{
cout << "\n\t Invalid :( \t The max range for first name is 15 and min range is 3 :)";
goto E;
}
else
{
for (int b = 0; (unsigned)b<(unsigned)strlen(obj->Problem); b++)
{
if (isalpha(obj->Problem[b]))//function in C which can be used to check if the passed character is an alphabet or not.
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t Problem contain Invalid character :(  Enter again :)";
goto E;
}
}

/* ********************************************** Prescribed Doctor **************************************** */
F:
cout << "\n\t\t\tPrescribed Doctor:";
cin.getline(obj->Doctor, 50);
obj->Doctor[0] = toupper(obj->Doctor[0]);
if (strlen(obj->Doctor)>30 || strlen(obj->Doctor)<3)
{
cout << "\n\t Invalid :( \t The max range for first name is 30 and min range is 3 :)";
goto F;
}
else
{
for (int b = 0; (unsigned)b<(unsigned)strlen(obj->Doctor); b++)
{
if (isalpha(obj->Doctor[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t Doctor name contain Invalid character :(  Enter again :)";
goto F;
}
}
//**************************filing************************************
ofstream myfile;
myfile.open("text.txt", ios::app);//open file in write mode
if (!myfile)
{    // file couldn't be opened
cout << "Error: file could not be opened" << endl;

}
else
{

myfile
<< obj->Doctor << "\n"
<< obj->id << "\n"
<< obj->First_Name << "\n"
<< obj->Last_Name << "\n"
<< obj->Gender << "\n"
<< obj->age << "\n"
<< obj->Address << "\n"
<< obj->Contact_no << "\n"
<< obj->Email << "\n"
<< obj->Problem << "\n"
;
//<< date() << "\n";

}
myfile.close();

cout << "\n\n\t\t\t.... Information Record Successful ...";
sd:

cin.ignore();
cout << "\n\n\t\t\tDo you want to add more[Y/N]?? ";
cin >> ans;
if (toupper(ans) == 'Y')
{
Add_patient();
}
else if (toupper(ans) == 'N')
{
cout << "\n\t\t Thank you :) :)";
MainMenu();
}
else
{
cout << "\n\t\tInvalid Input\n";
goto sd;
}
}





void Search_rec(void)
{
char name[20];
system("cls");
Title();// call Title function
patient* obj = new patient();

fstream myfile;//open file to show on screen
myfile.open("Record2.dat");
if (!myfile)
{    // file couldn't be opened
cout << "Error: file could not be opened" << endl;

}
else
{
cout << "\n\n\t\t\t!!!!!!!!!!!!!! Search Patients Record !!!!!!!!!!!!!\n";
cout << "\n Enter Patient Name to be viewed:";
cin >> name;
//fflush(stdin);
//name[0] = toupper(name[0]);
while (!EOF)
{
if (strcmp(obj->First_Name, name) == 0)
{

cout << "\nFull Name";
myfile >> obj->First_Name;
myfile >> obj->Last_Name;
cout << obj->First_Name << " " << obj->Last_Name;
cout << "\nGender";
myfile >> obj->Gender;
cout << obj->Gender;
cout << "\nAge";
myfile >> obj->age;
cout << obj->age;
cout << "\nAddress";
myfile >> obj->Address;
cout << "\nContact No.\n";
myfile >> obj->Contact_no;
cout << "\nEmail";
myfile >> obj->Email;
cout << "\nProblem";
myfile >> obj->Problem;
cout << "\nPrescribed Doctor";
myfile << obj->Doctor;
cout << "================================== THANKS ==============================================================================";
break;
}
}
if (strcmp(obj->First_Name, name) != 0)
{
cout << "Record not found!";
system("pause");
}
}
myfile.close();
L:
system("pause");
cout << "\n\n\t\t\tDo you want to view more[Y/N]??";
char ans;
cin >> ans;
if (toupper(ans) == 'Y')
{
Search_rec();
}
else if (toupper(ans) == 'N')
{
cout << "\n\t\t Thank you :) :)";
MainMenu();
}
else
{
cout << "\n\tInvalid Input.\n";
goto L;
}
}

/* **************************************VIEW RECORD*******************************************/
void func_list()
{

ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (!myReadFile)
{
cout << "no file ";
}
while (!myReadFile.eof())
{

int r=0;
myReadFile.getline(output, 100);

// myReadFile >> output ;
cout << output << "\n";
r++;
if (r == 10)
{
cout << "\n\n";
r = 0;
}

}
myReadFile.close();
cout << "\n";
system("pause");
MainMenu();
}


***void search_p()
{
int choice=0;
char name[100];
//M:
cout << "1 for   patients list and 2 for menu ";
cin >> choice;
if (choice == 1)
{
cout << "\nenter your name ";
cin >> name;
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (!myReadFile)
{
cout << "no file ";
}
while (!myReadFile.eof())
{
int r = 0;
myReadFile.getline(output, 100);
if (output == name)
{
while (r == 10)
{
myReadFile.getline(output, 100);
r++;
}
r = 0;
}
// myReadFile >> output ;
cout << output << "\n";
r++;
if (r == 10)
{
cout << "\n";
r = 0;
}
}
myReadFile.close();
cout << "\n";
MainMenu();
//system("pause");
//goto M;
}
else
{
MainMenu();
}
}***




int main()
{

WelcomeScreen();//Use to call WelcomeScreen function
Title();//Use to call Title function
LoginScreen();//Use to call Menu function
}

Robocopy日志:

RoboCopy.exe  "C:\Folder" "C:\Folder2" *.* /S /E /MIR /MT:8 /X /V /TS /FP /BYTES /ETA /LOG:C:\Output\log\sync.txt

$SourceDir = "C:\Output\log\"
#$GCI_Fiter = '*.txt'
$Include=@("*.txt")

$FileList = Get-ChildItem -LiteralPath $SourceDir -Include "$Include" -File
foreach ($FL_Item in $FileList)   {

$results = Get-Content -Path $FL_Item.FullName | Select-String -pattern '(newer.New File)'

}

4 个答案:

答案 0 :(得分:1)

如果使用$InStuff将文件加载到Get-Content中,则可以使用-match处理集合的方式。这将为您提供与正则表达式模式匹配的行。这样...

$InStuff -match 'newer|new file'

输出...

New File               1 2019/12/25 17:16:04    C:\Folder\2.txt
Newer                  1 2019/12/25 17:16:04    C:\Folder\4.txt

答案 1 :(得分:1)

由于Select-String-Path参数采用数组,因此不需要循环。如果我们使用Glenn's helpful regex pattern,则可以执行以下操作:

$FileList = Get-ChildItem -LiteralPath $SourceDir -Include $Include -File
$results = Select-String -Path $FileList -Pattern '^\s+(Newer|New File)'

说明:

如果找到任何匹配项,

$results将包含MatchInfo个对象。这些结果的默认显示为具有匹配项,匹配项的行号和匹配行的文件的相对路径。但是,您可以访问属性以轻松地不同地调整输出。有关如何查看可用属性的信息,请参见下文:

$results[0] | Get-Member -MemberType Property

   TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name       MemberType Definition
----       ---------- ----------
Context    Property   Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename   Property   string Filename {get;}
IgnoreCase Property   bool IgnoreCase {get;set;}
Line       Property   string Line {get;set;}
LineNumber Property   int LineNumber {get;set;}
Matches    Property   System.Text.RegularExpressions.Match[] Matches {get;set;}
Path       Property   string Path {get;set;}
Pattern    Property   string Pattern {get;set;}

有关如何使用属性的示例,请参见下文:

# List Unique Files That Contain a Match
$results.Path | Get-Unique

# List All File Paths and Their Matching Lines
$results | Select-Object Path,Line

# Default Display
# Format Is RelativePath:LineNumber:Line
$results

# Calculated Properties Including Log File Path, Newer or New File as Type, the Copied File
$results | select Path,
                  @{n='Type';e={$_.Matches.Groups[1]}},
                  @{n='Copied File';e={$_.Line -replace '.*\d+:\d+:\d+\s+'}}

正则表达式详细信息:

^\s+(Newer|New File)

  • ^:字符串的开头,表示此处每行的开头。
  • \s+\s是空白字符。 +是一个或多个匹配项。因此,这表示一个或多个连续的空格。
  • (Newer|New File)|是正则表达式or的断言。这将匹配NewerNew File。使用()可确保首先匹配先前的^\s+。如果不使用(),则将应用^\s+NewerNew File。它确实会创建一个不必要的捕获组。 (?:Newer|New File)是非捕获版本。

答案 2 :(得分:0)

如果在行首有* .txt文件带有@Entity @Table(name="customers") @NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c") public class Customer implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) private int customerNumber; private String addressLine1; private String addressLine2; private String city; private String contactFirstName; private String contactLastName; private String country; private BigDecimal creditLimit; private String customerName; private String phone; private String postalCode; private String state; //bi-directional many-to-one association to Employee @ManyToOne @JoinColumn(name="salesRepEmployeeNumber") private Employee employee; //bi-directional many-to-one association to Order @OneToMany(mappedBy="customer") private List<Order> orders; //bi-directional many-to-one association to Payment @OneToMany(mappedBy="customer") private List<Payment> payments; public Customer() { } public int getCustomerNumber() { return this.customerNumber; } public void setCustomerNumber(int customerNumber) { this.customerNumber = customerNumber; } public String getAddressLine1() { return this.addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return this.addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getContactFirstName() { return this.contactFirstName; } public void setContactFirstName(String contactFirstName) { this.contactFirstName = contactFirstName; } public String getContactLastName() { return this.contactLastName; } public void setContactLastName(String contactLastName) { this.contactLastName = contactLastName; } public String getCountry() { return this.country; } public void setCountry(String country) { this.country = country; } public BigDecimal getCreditLimit() { return this.creditLimit; } public void setCreditLimit(BigDecimal creditLimit) { this.creditLimit = creditLimit; } public String getCustomerName() { return this.customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String getPostalCode() { return this.postalCode; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } public String getState() { return this.state; } public void setState(String state) { this.state = state; } public Employee getEmployee() { return this.employee; } public void setEmployee(Employee employee) { this.employee = employee; } public List<Order> getOrders() { return this.orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public Order addOrder(Order order) { getOrders().add(order); order.setCustomer(this); return order; } public Order removeOrder(Order order) { getOrders().remove(order); order.setCustomer(null); return order; } public List<Payment> getPayments() { return this.payments; } public void setPayments(List<Payment> payments) { this.payments = payments; } public Payment addPayment(Payment payment) { getPayments().add(payment); payment.setCustomer(this); return payment; } public Payment removePayment(Payment payment) { getPayments().remove(payment); payment.setCustomer(null); return payment; } } $true的文件,它将返回Newer(这样,对于以下文件名,文件名也不会显示误报可能会在其名称中包含New File。)

Newer

希望有帮助。

答案 3 :(得分:0)

就像给选择字符串一个模式数组一样简单:

'newer','new file' | select-string newer,'new file'

newer
new file