小编Gar*_*ett的帖子

查找对象数组的索引

当使用值数组时,indexof可用于查找值在数组中的位置。

#this returns '1', correctly identifying 'blue' in position '1' of the array
$valueArray = @('cup','blue','orange','bicycle')
[array]::indexof($valueArray,'blue')
Run Code Online (Sandbox Code Playgroud)

我想使用此命令来查找使用 生成的对象数组中文件(图像)的位置Get-ChildItem,但是无论我调用的对象实际在哪里,返回的位置始终为“-1”。请注意,image123.jpg 位于数组的中间。

$imageArray = Get-ChildItem "C:\Images"
[array]::indexof($imageArray,'image123.jpg')
Run Code Online (Sandbox Code Playgroud)

我注意到,如果我仅将数组更改为文件名,它会返回文件名的实际位置。

$imageArray = Get-ChildItem "C:\Images" | select -expand Name
[array]::indexof($imagesToReview,'image123.jpg')
Run Code Online (Sandbox Code Playgroud)

这只是使用的本质indexof还是有办法在不转换的情况下找到数组中图像文件的正确位置?

arrays powershell indexof

4
推荐指数
1
解决办法
7840
查看次数

Powershell移动并重命名文件

我需要交换一些文件的名称.这些文件位于同一位置,因此我计划将它们移动到临时接地,以避免两个文件具有相同的名称.我试图根据名称参数识别文件,将其移动到临时基地并重命名.

我想使用类似于以下内容的东西:

Get-ChildItem ".\" -Recurse | Where-Object { $_.Name -like "*XYZ*"} | Move-Item -Force -Destination "C:\new\" | Rename-Item -NewName { $_.Name -replace 'XYZ','ABC' }
Run Code Online (Sandbox Code Playgroud)

文件移动,但不重命名.我不能管move-itemrename-item吗?

我很高兴知道是否有更好的方法来交换两个文件的文件名而不移动,但也想知道为什么上面的工作不起作用.

谢谢!

powershell

2
推荐指数
1
解决办法
173
查看次数

输出文本文件以识别PowerShell中的Manipulated Files

我一直在寻找这个问题的帮助几个小时没有运气.我的脚本监视一个文件夹,查找超过12分钟以".tmp"结尾的文件.然后删除'.tmp'.这部分工作正常.

我希望脚本输出一个文本文件,其中包含重命名的文件的文件名.使用下面的脚本,文本文档是空的.

#Computer name from Description
$ComputerName = Get-WmiObject -Class Win32_OperatingSystem | Select Description
$ComputerName = "$ComputerName".split("}")
$ComputerName = "$ComputerName".split("=") | Select-Object -Last 1
$ComputerName = "$ComputerName".split(" ") | Select-Object -First 1

#$ComputerName = $env:computername
Write-Host "This is the computer name: $ComputerName"

$c1Output = 'E:\' + $ComputerName + '\Capture One Session\Output\Market\'

get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) }
    out-file \\server\Report.txt
Run Code Online (Sandbox Code Playgroud)

这应该很容易理解,但我仍然是PowerShell的新手.

谢谢!

powershell rename-item-cmdlet

1
推荐指数
1
解决办法
123
查看次数

Powershell 日期范围

我想创建一个变量,其中包含yyyy_MM_dd从给定日期起一周的格式的日期列表。

我当前的代码有效,但效率不高或很干净。理想情况下,最旧的日期(在下面的示例中为 -6 天)将是一个单独的变量,$lastDate = '-6'以便我可以快速进行调整,同时仍然让脚本从$lastDate.

我计划使用这个变量来查找包含特定格式的日期的路径,所以如果输出是字符串而不是真正的“日期”就可以了。我不想使用日期过滤器,Get-ChildItem因为文件路径的实际日期与路径名不准确。

当前脚本:

$range = (Get-Date).AddDays(-6).ToString('yyyy_MM_dd'), (Get-Date).AddDays(-5).ToString('yyyy_MM_dd'), (Get-Date).AddDays(-4).ToString('yyyy_MM_dd'), (Get-Date).AddDays(-3).ToString('yyyy_MM_dd'), (Get-Date).AddDays(-2).ToString('yyyy_MM_dd'), (Get-Date).AddDays(-1).ToString('yyyy_MM_dd'), (Get-Date).ToString('yyyy_MM_dd')
Run Code Online (Sandbox Code Playgroud)

这将返回所需的结果:

2019_11_28
2019_11_29
2019_11_30
2019_12_01
2019_12_02
2019_12_03
2019_12_04
Run Code Online (Sandbox Code Playgroud)

谁能建议一种更清洁或更有效的方法来做到这一点?

powershell date

0
推荐指数
1
解决办法
418
查看次数

标签 统计

powershell ×4

arrays ×1

date ×1

indexof ×1

rename-item-cmdlet ×1