Match file name and copy with PowerShell

I am trying to get this simple PowerShell script to work. I have googled, but I can not find the answer.

As you can see, I indicate the location of the source and destination.

The $ filedate variable does dateadd to get the date of the day, as the file name contains the date.

There is a string in the filter variable $ that I am looking for, including the date part.

The Get-ChildItem command works on its own, but when I apply a filter, it returns nothing. What am I missing to get this to work?

$source = "C:\MSSQL.1\Backup\" $destination = "D:\MSSQL.2\Backup\" $filedate = (get-date).AddDays(-1).tostring('yyyyMMdd') $filter = "FULL_(local)_Product_" + $filedate + "*" Get-ChildItem -Path $source -filter $filter | Copy-Item -Destination $destination 
+6
source share
1 answer

Try filtering the file list using the Where-Object cmdlet and the -match operator:

 $source = "C:\MSSQL.1\Backup\" $destination = "D:\MSSQL.2\Backup\" $filedate = (Get-Date).AddDays(-1).ToString("yyyyMMdd") $filter = "FULL_(local)_Product_$filedate" Get-ChildItem -Path $source | Where-Object { $_.Name -match $filter } | Copy-Item -Destination $destination 

If you still do not get any results, we need to look at the filter itself.

Related Resources:

+13
source

Source: https://habr.com/ru/post/892012/


All Articles