0

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
}
}

1 Answer 1

1

Your issue may be caused by the Select -Index 1, as Powershell uses 0 based indexing this will actually select the second line of the file. If you change this to 0 it should correctly get the header row.

On a separate note, instead of doing $filePath = $fileDirectory + "\" + $file; you can just use $file.FullName to get the file path.

EDIT:

I think this should do what you're after:

[string] $FileDirectory = "D:\Data";
[string] $OutputPath  = "D:\Output\FileA";
[string] $OutputPath2 = "D:\Output\FileB";
[string] $OutputPath3 = "D:\Output\FileC";

foreach ($FilePath in Get-ChildItem $FileDirectory | Select-Object -ExpandProperty FullName)
{
    [string] $Header = Get-Content $FilePath -First 1

    if ($Header -match 'Name,Emp.*') {
        Copy-Item $FilePath $OutputPath
    }
    elseif ($Header -match 'Name,Group.*') {
        Copy-Item $FilePath $OutputPath2
    }
    else {
        Copy-Item $FilePath $OutputPath3
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Also, the statement "then copy the file to folderA and if line start with 'Name,Group.' then copy the file to folderB else copy file to folder C" doesn't seem to be true. Files will only be copied to "D:\Output\FileA" and "D:\Output\FileC".
modified to else if instead of and
The problem was because . before * in if statement. I thought . is must follow by *. After remove . and change index to 0 the problem is work as expected. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.