1

I am working out a scenario using PowerShell.

As of now, I am trying to workout a scenario where I have a batch job which upon successful execution first creates a date folder for that particular day and then creates a .CSV file under. The folder structure than look like below.

\\Server\SourceA\Processed\20200120\TestA.CSV

when the job runs next day, it will create another folder and file like below.

\\Server\SourceA\Processed\20200121\TestB.CSV

This is how there have been so many folders already created in past.

My PS script is good to run daily after the batch job is completed. I read a date, append to path and it copies file from source to destination folder. But I want to enable my PS script to read all previous date folders created under

\\Server\SourceA\Processed\

Another tricky part is - under the date folder there is few other sub folders. I.e.

\\Server\SourceA\Processed\20191010\Log
\\Server\SourceA\Processed\20191010\Charlie
\\Server\SourceA\Processed\20191010\Alpha
\\Server\SourceA\Processed\20191010\Delta

among them, I only need to read files from Log folder only. Hence, my actual source path becomes like

\\Server\SourceA\Processed\20191010\Log\TestA.CSV

Here is my script (which is static right now and unable to read past existing date folders).

$fullSourceFileName = "\\Server\SourceA\Processed\"
$date = Get-Date -format "yyyyMMdd"
$fullSourceFileName = "$($fullSourceFileName)$($date)\Log

$destination = "\\Server\DestA\Processed\"
$destination = "$($destination)$($date)\"

get-childitem -path $fullSourceFileName -recurse | copy-item -destination "$($destinationFolder)$($date)\"

Your help is highly appreciated.

1 Answer 1

1

I did not know I can use foreach loop in Powershell. So, here is the answer to read all dynamic date folder under my given path. I hope this helps the community.

$fullSourceFileName = "\\Server\SourceA\Processed\"
$DirToRead = "\Log\"
$dates = Get-ChildItem -Path $fullSourceFileName -Directory
$destination = "\\Server\DestA\Processed\"

foreach ($date in $dates){
        $ActualPath = "$($fullSourceFileName)$($date)$($DirToRead)"
        if(!(Test-Path $ActualPath))
        {
             Write-Output $($ActualPath)$(" source path does not exist")
        }
        else
        {
             get-childitem -path $ActualPath -recurse | copy-item -destination "$($destinationFolder)$($date)\"
        }
        $ActualPath = ""
}
Sign up to request clarification or add additional context in comments.

Comments

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.