IGGt
IGGt

Reputation: 2759

powershell v2 - rename a file whose name matches a string containing a wildcard

I'm trying to create a script to rename a file that matches a given filename, with a wildcard character. e.g.

I have a folder containing the following files:

I want to scan through this folder, and append the current time to the start of any file starting with the word 'filename'

I have the following so far:

$d = Get-Date -format "yyyyMMddHHmm" 
$dir = "C:\test"
$file = "filename*.log"

get-childitem -Path "$dir" | where-object { $_.Name -like "$file" } | rename-item -path $_ -newname $d."_".$_.name

but it doesn't work. As I see it the individual sections 'should' work from my reading of the documentation, but clearly something is wrong. If someone can point it out it would be appreciated.


We're getting closer. It would appear that -path in the rename-item section needs to be $dir$_ as $_ (seemingly) only contains the filename. (The get-help example suggests it needs to be the full path and filename)

If I take out the rename-item section and replace it with %{write-host $d"_"$_} it gives the correct new filename

However, simply placing this into rename-item section still doesn't update the filename.

rename-item -path $dir$_ -newname $d"_"$_

SUCCESS

the correct syntax appears to be:

get-childitem -Path "$dir" | where-object { $_.Name -like "$file" } | %{rename-item -path $dir$_ -newname $d"_"$_}

The missing element was the %{ ... } surrounding the rename-item section which enabled me to reference $_

Upvotes: 1

Views: 20697

Answers (3)

js2010
js2010

Reputation: 27408

Yet another way to do it without using foreach or %, but a script block instead:

get-childitem -Path "$dir" -filter $file | rename-item -newname { $d + "_" + $_.name }

See the examples in the doc for rename-item: https://msdn.microsoft.com/en-us/powershell/reference/3.0/microsoft.powershell.management/rename-item There's a -replace operator too, but it can't use wildcards? Oh, you don't need wildcards:

get-childitem -Path "$dir" -filter $file | rename-item -newname { $_.name -replace 'filename',($d + '_filename') }

Upvotes: 1

James Ruskin
James Ruskin

Reputation: 990

$d = Get-Date -format "yyyyMMddHHmm" 
$dir = "C:\test"
$file = "filename*.log"

get-childitem -Path $dir | where-object { $_.Name -like $file } | %{ rename-item -LiteralPath $_.FullName -NewName "$d`_$($_.name)" }

This should work, assuming that the errors were relating to "Cannot bind argument to parameter 'Path'", and the NewName string.

Issues included:

You could, instead of passing the pipeline object to a Foreach-Object, pass directly to the Rename-Item - but I'm unsure quite how to reference the name of the object for the -NewName parameter.

Upvotes: 4

alroc
alroc

Reputation: 28144

I don't recall . being a string concatenation operator in PowerShell (I may be wrong). Try this:

rename-item -path $_ -newname "$d_$($_.name)"

Or this

rename-item -path $_ -newname ($d + "_" + $_.name)

Or even this

rename-item -path $_ -newname ({0}_{1} -f $d,$_.name)

See the answers here

Upvotes: 2

Related Questions