【发布时间】:2021-12-11 04:24:13
【问题描述】:
我正在尝试将变量附加到文件名的开头。我见过的解决方案都使用“+”来连接,但这给了我错误。我怀疑是该变量没有被识别为字符串。但我不知道如何解决这个问题。也许这不是问题。
这是一次尝试:
$JobNum = (get-item $Path).parent.parent.parent
Get-ChildItem Of*.pdf | rename-item -newname { $JobNum + "_"+ $_.name }
错误是:“参数‘NewName’的脚本块输入失败。方法调用失败,因为 [System.IO.DirectoryInfo] 不包含名为‘op_Addition’的方法。
这是另一个。我在 $JobNum 中添加了一个字符串以尝试强制 powershell 将其视为一个字符串,我首先在文件上放置了一个静态作业编号,然后尝试替换它。
$JobNum = -join ((get-item $Path ).parent.parent.parent , "_")
Get-ChildItem Of*.pdf | rename-item -newname { $_.Name -replace 'O', '99999_O' }
Get-ChildItem 99999* | rename-item -newname { $_.name -replace "99999_", $JobNum }
错误是“无法重命名指定的目标,因为它代表路径或设备名称。
感谢任何帮助。
附加信息: $职位编号 |获取会员
名称 MemberType 定义
LinkType CodeProperty System.String LinkType{get=GetLinkType;} 模式 CodeProperty System.String Mode{get=Mode;} ModeWithoutHardLink CodeProperty System.String ModeWithoutHardLink{get=ModeWithoutHardLink;} 目标代码属性 System.String Target{get=GetTarget;} 创建方法 void Create() CreateSubdirectory 方法 System.IO.DirectoryInfo CreateSubdirectory(string path) 删除方法 void Delete(), void Delete(bool recursive) EnumerateDirectories 方法 System.Collections.Generic.IEnumerable[System.IO.DirectoryInfo] EnumerateDirec… EnumerateFiles 方法 System.Collections.Generic.IEnumerable[System.IO.FileInfo] EnumerateFiles(), S... EnumerateFileSystemInfos 方法 System.Collections.Generic.IEnumerable[System.IO.FileSystemInfo] EnumerateFile… Equals Method bool Equals(System.Object obj) GetDirectories 方法 System.IO.DirectoryInfo[] GetDirectories(), System.IO.DirectoryInfo[] GetDirec… GetFiles 方法 System.IO.FileInfo[] GetFiles(), System.IO.FileInfo[] GetFiles(string searchPa... GetFileSystemInfos 方法 System.IO.FileSystemInfo[] GetFileSystemInfos(), System.IO.FileSystemInfo[] Ge... GetHashCode 方法 int GetHashCode() GetLifetimeService 方法 System.Object GetLifetimeService() GetObjectData 方法 void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System… GetType 方法类型 GetType() InitializeLifetimeService 方法 System.Object InitializeLifetimeService() MoveTo 方法 void MoveTo(string destDirName) 刷新方法 void Refresh() ToString 方法字符串 ToString() 属性 属性 System.IO.FileAttributes 属性 {get;set;} CreationTime 属性日期时间 CreationTime {get;set;}
【问题讨论】:
-
什么是 $Path?是字符串还是对象?
标签: powershell filenames