Home > Net >  How to put a variable into a filename in powershell
How to put a variable into a filename in powershell

Time:10-27

I'm trying to append a variable to the beginning of a filename. The solutions I've seen all use a " " to concatenate, but this is giving me errors. I suspect it's that the variable isn't being recognized as a string. But I don't know how to fix that. And maybe that's not the problem.

Here was one attempt:

$JobNum = (get-item $Path).parent.parent.parent
Get-ChildItem Of*.pdf | rename-item -newname { $JobNum   "_"  $_.name }

The error was : "The input to the script block for parameter 'NewName' failed. Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'op_Addition'.

Here was another. This one I added a string to $JobNum to try to force powershell to see it as a string and I put a static jobnumber on the file first then tried to replace it.

$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 }

The error was "Cannot rename the specified target, because it represents a path or device name.

Any help is appreciated.

Additional Information: $JobNum | Get-Member

Name MemberType Definition


LinkType CodeProperty System.String LinkType{get=GetLinkType;} Mode CodeProperty System.String Mode{get=Mode;} ModeWithoutHardLink CodeProperty System.String ModeWithoutHardLink{get=ModeWithoutHardLink;} Target CodeProperty System.String Target{get=GetTarget;} Create Method void Create() CreateSubdirectory Method System.IO.DirectoryInfo CreateSubdirectory(string path) Delete Method void Delete(), void Delete(bool recursive) EnumerateDirectories Method System.Collections.Generic.IEnumerable[System.IO.DirectoryInfo] EnumerateDirec… EnumerateFiles Method System.Collections.Generic.IEnumerable[System.IO.FileInfo] EnumerateFiles(), S… EnumerateFileSystemInfos Method System.Collections.Generic.IEnumerable[System.IO.FileSystemInfo] EnumerateFile… Equals Method bool Equals(System.Object obj) GetDirectories Method System.IO.DirectoryInfo[] GetDirectories(), System.IO.DirectoryInfo[] GetDirec… GetFiles Method System.IO.FileInfo[] GetFiles(), System.IO.FileInfo[] GetFiles(string searchPa… GetFileSystemInfos Method System.IO.FileSystemInfo[] GetFileSystemInfos(), System.IO.FileSystemInfo[] Ge… GetHashCode Method int GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System… GetType Method type GetType() InitializeLifetimeService Method System.Object InitializeLifetimeService() MoveTo Method void MoveTo(string destDirName) Refresh Method void Refresh() ToString Method string ToString() Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property datetime CreationTime {get;set;}

CodePudding user response:

J Web, is $JobNum a string or some kind of object?

I was able to make your original syntax work when $JobNum is a string.

PS C:\Users\vvangor001\documents\Scripts - Local> $JobNum = "Job001"
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local> Get-ChildItem .\*.jpg

    Directory: C:\Users\vvangor001\documents\Scripts - Local

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/21/2019  12:13 PM         153628 WebEx-20190221-2.JPG
-a----        2/21/2019  12:14 PM         165229 WebEx-20190221-3.JPG
-a----        2/21/2019  12:12 PM         100058 WebEx-20190221.JPG

PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local> Get-ChildItem .\*.jpg | Rename-item -NewName { $JobNum   "_"   $_.Name }
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local>
PS C:\Users\vvangor001\documents\Scripts - Local> Get-ChildItem .\*.jpg


    Directory: C:\Users\vvangor001\documents\Scripts - Local


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/21/2019  12:13 PM         153628 Job001_WebEx-20190221-2.JPG
-a----        2/21/2019  12:14 PM         165229 Job001_WebEx-20190221-3.JPG
-a----        2/21/2019  12:12 PM         100058 Job001_WebEx-20190221.JPG


PS C:\Users\vvangor001\documents\Scripts - Local>

To determine what $JobNum looks like, run this command:

PS C:\Users\vvangor001\documents\Scripts - Local> $JobNum | Get-Member


   TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
Clone            Method                System.Object Clone(), System.Object ICloneable.Clone()
...

In the example above, we can see that $JobNum is a string.

  • Related