I've a scenario where everyday I will received 2 csv files where the file naming is something like CMCS_{Timestamp}, example CMCS_02012016100101 and CMCS_02012016100102 . This 2 files are different files and have different structure, but because this 2 files will go into same folder where my ETL tools will pick it up and process. So I wrote a script where the script will based on the structure of the file to distinguish it whether is a file A or file B.
For File A, i tell a script to look at first line of the file and if line start with 'Name,Emp(Date).' then copy the file to folderA else if line start with 'Name,Group.' then copy the file to folderB else copy file to folder C
Here the code that i wrote, the powershell does not generate any errors but it does not produce any results too. I wonder what wrong in my script.
$fileDirectory = "D:\Data";
$output_path = "D:\Output\FileA";
$output_path2 = "D:\Output\FileB";
$output_path2 = "D:\Output\FileC";
foreach($file in Get-ChildItem $fileDirectory)
{
# the full file path.
$filePath = $fileDirectory + "\" + $file;
$getdata = Get-Content -path $filePath
$searchresults = $getdata | Select -Index 1 | Where-Object { $_ -like 'Name,Emp(Date).*' }
$searchresults2 = $getdata | Select -Index 1 | Where-Object { $_ -like 'Name,Group.*' }
if ($searchresults -ne $null) {
Copy-Item $filePath $output_path
}
if ($searchresults2 -ne $null) {
Copy-Item $filePath $output_path2
}
}