I have 14,000 pictures sorted into files by year and month but taken with multiple cameras and I want the file name to reflect the date taken. For example October 16, 1998 p
$path="C:\..."
$files=Get-ChildItem $path -Filter *.gif
Foreach($obj in $files){
$pathWithFilename=$path + $obj.Name;
$newFilename= "file_"+$obj.Name;
Rename-Item -Path $pathWithFilename -NewName $name
}
You can use this code blocks in powershell ISE. Example output like this :
Old files :
1232.gif asd.gif
After run code :
file_1232.gif file_asd.gif
The syntax is way off. A few issues:
$($.19981016)
was intended to produce, but $( ) evaluates the expression in the parentheses and interpolates the result into the string, and you're getting the error because $.19981016
is not a valid expression. The error would be repeated for each .jpg file in the directory.{0:00000000#}
in a formatted string will create a zero-padded number of 9 digits, but a shorter way to do that is {0:D9}
. However, I thought you wanted the zero-padded number to have 4 digits, so that should be {0:0000#}
or {0:D4}
.Foreach {$i=1} { [...]
is supposed to do. The keyword foreach can mean a foreach loop, or it can be shorthand for Foreach-Object. In this context, receiving input from a pipeline, it's necessarily the latter, but this syntax is incorrect either way.This will do what you want, if I understand the description correctly:
$i = 1
Get-ChildItem *.jpg | %{Rename-Item $_ -NewName ('19981016_{0:D4}.jpg' -f $i++)}
The filenames will be 19981016_0001.jpg, 19981016_0002.jpg, 19981016_0003.jpg, etc.
A few notes:
This comes close to the original question. You can actually pass a script block array to foreach-object -process, as documented (barely). I only know this after Bruce Payette's book. The containing folder name is "19981016". The source filenames may not go in the order you expect.
ls *jpg |
Foreach {$i=1} {Rename-Item $_ -NewName ("$($_.directory.name)_{0:000#}.jpg" -f
$i++) -whatif}
What if: Performing the operation "Rename File" on target "Item: C:\users\js\19981016\file1.jpg Destination: C:\users\js\19981016\19981016_0001.jpg".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\19981016\file10.jpg Destination: C:\users\js\19981016\19981016_0002.jpg".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\19981016\file2.jpg Destination: C:\users\js\19981016\19981016_0003.jpg".