0

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:

  • 201412180900_filename_123.log
  • 201412181000_filename_123.log
  • 201412181100_filename_456.log
  • filename_789.log

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

IGGt
  • 2,627
  • 10
  • 42
  • 63
  • 1
    What *is* happening when you run it, if I might ask? – James Ruskin Dec 19 '14 at 12:04
  • "It doesn't work" isn't useful in attempting to help you. Describe what happens and what you expected to happen. – alroc Dec 19 '14 at 12:06
  • sorry, I should have been clearer, Basically when I run it nothing happens. No error message. But the filename doesn't change. What I was expecting to happen was either get an error message, or the filename should change. – IGGt Dec 19 '14 at 13:14

3 Answers3

3
$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.

Community
  • 1
  • 1
James Ruskin
  • 808
  • 7
  • 13
2

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

Community
  • 1
  • 1
alroc
  • 27,574
  • 6
  • 51
  • 97
  • From a little experimentation a moment ago, `"$d_$($_.name)"` attempts to get `$d_ + $($_.name)`. I attempted to solve this by adding "` " - would be curious about a better method, though. You are right about `.`. – James Ruskin Dec 19 '14 at 12:25
  • cheers, I tried the + and that didn't work, but it didn't 'look' right without some kind of concatenation operator. And I thought I had read about using . to join strings (apparently I hadn't). Unfortunately none of the options above worked. The first two did nothing (didn't update the filename), and the last one gave various 'unexpected token' errors. – IGGt Dec 19 '14 at 13:25
1

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') }
js2010
  • 23,033
  • 6
  • 64
  • 66