You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将文件元数据宽高字符串属性转为整数的PowerShell问题

解决PowerShell中图片宽度/高度属性转整数为空的问题

我来帮你搞定这个问题!你遇到的情况是Breedte(宽度)和Hoogte(高度)的属性值转整数时返回空,但Grootte(大小)却能正常工作——仔细看你的输出就能发现端倪:Breedte的值是‎1920 pixels,前面藏了个不可见的零宽度非连接符(U+200E,对应PowerShell里的[char]8206),这就是转换失败的核心原因!

问题根源

这个隐藏字符是系统通过Get-FileMetaData返回元数据时附带的,当你用.split(' ')[0]提取数值部分时,实际上拿到的是‎1920(前面带隐藏字符),[convert]::ToInt32()无法识别包含这种特殊字符的字符串,所以直接返回空值;而Grootte的数值388前面没有这个隐藏字符,所以转换完全正常。

解决方案

只需要在转换前把这个隐藏字符替换掉就行,修改你代码里的Width和Height计算表达式:

@{ Name = 'Width' Expression = { [convert]::ToInt32(($_.$widthProperty.split(' ')[0] -replace [char]8206), 10) }},
@{ Name = 'Height' Expression = { [convert]::ToInt32(($_.$heightProperty.split(' ')[0] -replace [char]8206), 10) }}

修改后的完整脚本

直接替换你原来的脚本部分即可:

$threshold = 160 # size in kB
# Config for Property names (Translations)
$nameProperty = 'Naam';
$widthProperty = 'Breedte';
$heightProperty = 'Hoogte';
$sizeProperty = 'Grootte';
Clear-Host
# Get all files
Get-FileMetaData 'C:\Users\Anton\Pictures\StackOverflow' | Select-Object -Property Breedte, Hoogte, Grootte,
@{ Name = 'Name' Expression = { $_.$nameProperty }},
@{ Name = 'Width' Expression = { [convert]::ToInt32(($_.$widthProperty.split(' ')[0] -replace [char]8206), 10) }},
@{ Name = 'Height' Expression = { [convert]::ToInt32(($_.$heightProperty.split(' ')[0] -replace [char]8206), 10) }},
@{ Name = 'Size' Expression = { [convert]::ToInt32($_.$sizeProperty.split(' ')[0], 10) }}

这样处理后,Width和Height就能正确显示1920和1080的整数数值了,和Size字段的表现一致。

内容的提问来源于stack exchange,提问作者Anton Plancke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:55:12