0

I'm trying to copy files based on the hour of Get-Date's time stamp to match it to files that are saved from a different source in a filename_10_00.ext structure. If I use 24 hour time, I can use the hour of Get-Date to determine which file I'm looking for. (This is to run hourly backups)

This is what I have:

$current_time = Get-Date
$hour_var = $current_time.Hour
Get-ChildItem -Path \\Path\To\Source | Where-Object {
    $_.Name -match "$hour_var"
} | Copy-Item -Destination \\Path\To\Destination 

Can somebody tell me why it can't seem to match based on the PowerShell I've provided?

I am looking for pure PowerShell with ideally no imported modules.

4
  • Please show sample output of Get-ChildItem -Path \\Path\To\Source as well as the desired and actual output of Get-ChildItem -Path \\Path\To\Source | Where-Object {$_.Name -match "$hour_var"}. Commented Mar 2, 2017 at 0:16
  • Unfortunately, there is no sample output. The desired output would be that it uses $hour_var as a regular expression to scan all files for the "filename_hh_mm_ss.ext" to compare the returned hour from $hour_var with the hour in hh from the filename. Commented Mar 2, 2017 at 18:31
  • If Get-ChildItem -Path \\Path\To\Source doesn't produce any output, why are you surprised that nothing is being copied? Commented Mar 2, 2017 at 18:33
  • My apologies. I misunderstood the output of Get-ChildItem -Path \\Path\To\Source as the output of the copy. I can see the files in that folder when I Write-host $test after I assign the Get-ChildItem command to $test Commented Mar 2, 2017 at 21:13

1 Answer 1

1

Assuming leading zeroes in the hour format try this:

$Src = "\\Path\To\Source"
$Dst = "\\Path\To\Destination"

$CurrHour = (Get-Date).Hour.ToString('00')
Get-ChildItem -Path $Src *_??_??.ext | 
  Where-Object BaseName -match "^.*_$CurrHour_\d{2}$" | 
    Copy-Item -Destination $Dst -whatif

If output looks OK remove the -whatif

Sign up to request clarification or add additional context in comments.

5 Comments

Using this example doesn't seem to yield any output on -whatif and removing the -whatif doesn't seem to copy anything. I've revised it with the actual source and destination path along with file extension to give a more accurate example. Src and Dts are shared folders on the PC and I'm running as admin. $Src = "\\WIN-F4GLTG2LGVN\Source" $Dst = "\\WIN-F4GLTG2LGVN\Destination" $CurrHour = (Get-Date).Hour.ToString('00') Get-ChildItem -Path $Src filename_??_??.txt | Where-Object BaseName -match "^filename_$CurrHour_\d{2}$" | Copy-Item -Destination $Dst -whatif
Did you adapt the pseudo filename to fit your environment?
I didn't but I was more concerned that shared folders might be causes issues than the filenames so I wanted them to be more strict. For testing purposes, filename_12_00.txt would be a legitimate example.
Changed above to be less restrictive it uses a wildcard *._??_??.ext to filter filenames in the gci and a pattern "^.*_$CurrHour_\d{2}$" to match on the hour. This worked in my local tests.
Works great. Thanks for your time!

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.