Powershell Get ChildItem 使用错误路径查找匹配项
Powershell GetChildItem using wrong path to find match
现在我正在尝试编写一些 Powershell 脚本来检查文件是否存在。
我的一个变量叫做 test ,它的重点是在基本路径中找到不早于一天的文件
文件命名逻辑:File2021-02-1717821等
我现在的代码:
$basepath='C:\Users\MyName\Documents20-09-06 193009 Testing'
$date=(get-date).AddDays(-1).ToString("YYYY-MM-dd")
$test=Get-ChildItem $basepath | Where-Object Name -match ($date) | ForEach-Object {$_.FullName}
但是 Test 只会 return 脚本位置的文件而不是 basepath 变量的文件。我还有一个变量 return 符合测试标准的最新文件,但它也 return 脚本本身而不是在 basepath 目录中查找。
$last = Get-ChildItem -Path $test | Sort-Object LastAccessTime -Descending | Select-Object -First 1
这是你想要做的吗?
($basepath = 'D:\Temp\DateStringFiles')
# Results
<#
D:\Temp\DateStringFiles
#>
($date = (get-date).AddDays(-1).ToString('yyyy-MM-dd'))
# Results
<#
2021-02-16
#>
Get-ChildItem -Path $basepath |
Format-Table -AutoSize
# Results
<#
Directory: D:\Temp\DateStringFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-Feb-21 09:07 0 File2021-02-1617821.txt
-a---- 17-Feb-21 09:07 0 File2021-02-1617822.txt
-a---- 17-Feb-21 09:07 0 File2021-02-1717821.txt
#>
(
$test = Get-ChildItem -Path $basepath |
Where-Object -Property FullName -Match $Date |
Format-Table -AutoSize
)
# Results
<#
Directory: D:\Temp\DateStringFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-Feb-21 09:07 0 File2021-02-1617821.txt
-a---- 17-Feb-21 09:07 0 File2021-02-1617822.txt
#>
(
$last = Get-ChildItem -Path $basepath |
Where-Object -Property FullName -Match $Date |
Select-Object -First 1 |
Format-Table -AutoSize
)
# Results
<#
Directory: D:\Temp\DateStringFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-Feb-21 09:07 0 File2021-02-1617821.txt
#>
现在我正在尝试编写一些 Powershell 脚本来检查文件是否存在。 我的一个变量叫做 test ,它的重点是在基本路径中找到不早于一天的文件
文件命名逻辑:File2021-02-1717821等
我现在的代码:
$basepath='C:\Users\MyName\Documents20-09-06 193009 Testing'
$date=(get-date).AddDays(-1).ToString("YYYY-MM-dd")
$test=Get-ChildItem $basepath | Where-Object Name -match ($date) | ForEach-Object {$_.FullName}
但是 Test 只会 return 脚本位置的文件而不是 basepath 变量的文件。我还有一个变量 return 符合测试标准的最新文件,但它也 return 脚本本身而不是在 basepath 目录中查找。
$last = Get-ChildItem -Path $test | Sort-Object LastAccessTime -Descending | Select-Object -First 1
这是你想要做的吗?
($basepath = 'D:\Temp\DateStringFiles')
# Results
<#
D:\Temp\DateStringFiles
#>
($date = (get-date).AddDays(-1).ToString('yyyy-MM-dd'))
# Results
<#
2021-02-16
#>
Get-ChildItem -Path $basepath |
Format-Table -AutoSize
# Results
<#
Directory: D:\Temp\DateStringFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-Feb-21 09:07 0 File2021-02-1617821.txt
-a---- 17-Feb-21 09:07 0 File2021-02-1617822.txt
-a---- 17-Feb-21 09:07 0 File2021-02-1717821.txt
#>
(
$test = Get-ChildItem -Path $basepath |
Where-Object -Property FullName -Match $Date |
Format-Table -AutoSize
)
# Results
<#
Directory: D:\Temp\DateStringFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-Feb-21 09:07 0 File2021-02-1617821.txt
-a---- 17-Feb-21 09:07 0 File2021-02-1617822.txt
#>
(
$last = Get-ChildItem -Path $basepath |
Where-Object -Property FullName -Match $Date |
Select-Object -First 1 |
Format-Table -AutoSize
)
# Results
<#
Directory: D:\Temp\DateStringFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-Feb-21 09:07 0 File2021-02-1617821.txt
#>