Powershell move and rename files - powershell

I need to swap the names of some files. The files are in the same location so I planned to move them to a staging ground to avoid having two files with the same name. I am attempting to identify the file, based on name parameters, move it to the staging ground and rename it.
I would like to use something similar to the following:
Get-ChildItem ".\" -Recurse | Where-Object { $_.Name -like "*XYZ*"} | Move-Item -Force -Destination "C:\new\" | Rename-Item -NewName { $_.Name -replace 'XYZ','ABC' }
The file moves, but does not rename. Am I not able to pipe the move-item to rename-item?
I would be happy to know if there is a better way to swap the file names of two files without moving, but also would like to know why the above isn't working.
Thanks!

By default Move-Item will not pass the current object along the pipeline.
You can use the -Passthru switch to get that functionality:
Move-Item -Force -Destination "C:\new\" -Passthru
Alternatively, you could cut out Rename-Item by having the Move-item destination be a file:
Get-ChildItem ".\" -Recurse |
Where-Object { $_.Name -like "*XYZ*"} |
Move-Item -Force -Destination "C:\new\$($_.Name -replace 'XYZ','ABC')"

Swapping implies two renames which only can take place with one temporary location or a temporary name.
IMO one temporary name is easier.
#Requires -Version 3.0
(Get-ChildItem ".\*XYZ*" -File -Recurse) | ForEach-Object {
$Swap = $_.Replace("XYZ","ABC")
If (Test-Path $Swap){
Rename-Item $Swap -NewName "$Swap.bak"
$_ | Rename-Item -NewName $Swap
Rename-Item "$Swap.bak" -NewName $_.FullName
} else {
$_ | Rename-Item -NewName $Swap
}
}

Related

How to include folders in Powershell -include string

I am doing some batch file name updates and am having trouble including folders. I have it currently set to target only specific file types, but I also want to include folders. Since folders don't have an extension I am unsure how to specify folders in the "-include" string. Any help would be greatly appreciated.
Here is what I am currently working with, but it ignores folders when I would like them included.
Get-ChildItem k:/toolbox/powershell -Include *.gif, *.jpg, *.png, *.xls,
*.xlsx, *.ppt, *.pptx, *.doc, *.docx, *.pdf -recurse | where {$_.name -match
"_"} | foreach {
$New=$_.name.Replace("_","-")
Rename-Item -path $_.Fullname -newname $New -passthru
}
(Get-Item '~\Desktop\*') | foreach { $FolderName = $_.name.Replace("_","-"); Rename-Item -path $_.fullname -newname $FolderName -passthru }
I would just add 2nd statement:
Get-ChildItem "k:/toolbox/powershell" -Recurse |where mode -eq d----- | where {$_.name -match "_"} | foreach {$New=$_.name.Replace("_","-")
Rename-Item -path $_.Fullname -newname $New -passthru }
If you run Get-ChildItem "k:/toolbox/powershell" you will receive a list of items where the "Mode" is "d-----" for directories, hence you can use that filter criteria for directories

PS to rename directory of files using only the first 4 characters

I have a directory c:\test with files 0001 test.pdf, 0002ssssit.pdf, 0003llllllllllll.pdf
My goal is to use PS to use a a loop to go through the directory and rename the files to:
0001.pdf
0002.pdf
0003.pdf
I keep getting path errors
$List = get-childitem "C:\test"
$List |Format-Wide -Column 1 -property name
ForEach($File In $List)
{
$First4 = $File.name.substring(0,4)
Rename-Item -newname $First4".pdf"
}
You need to pass the original file path to Rename-Item, otherwise it won't know what to rename!
Either:
$file | Rename-Item -NewName "${First4}.pdf"
or
Rename-Item -LiteralPath $file.FullName -NewName "${First4}.pdf"
inside the foreach body.
You could also use a single pipeline to accomplish the same (-NewName supports pipeline binding):
$List | Rename-Item -NewName { $_.Name.Substring(0,4) + $_.Extension }
try Something like this:
Get-ChildItem "c:\temp" -file "*.pdf" |
where Name -match "^[0-9]{4}" |
rename-item -NewName {"{0}{1}" -f $_.BaseName.Substring(0, 4), $_.Extension}

Overwrite existing file and Rename

I am trying to rename a file Members.csv.
Get-ChildItem ".\" -Filter '*Members*.csv' | where {
$_.LastWriteTime.GetDateTimeFormats()[44] -eq $today
} | Rename-Item -NewName "hello2.csv" -Force
-Force is not working. If hello2 exists already the renaming will not take place.
I found this thread saying that we have to use Move-Item. but I am not sure how to incorporate it in my code.
rename-item and override if filename exists
Get-ChildItem index.*.txt | ForEach-Object {
$NewName = $_.Name -replace "^(index\.)(.*)",'$2'
$Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
Move-Item -Path $_.FullName -Destination $Destination -Force
}
I am not trying to replace, I'm renaming the entire name so I don't know what to do for this part:
$NewName = $_.Name = "hello2"
The -Force parameter of Rename-Item does not allow you to replace an existing file by renaming another. From the documentation:
-Force
Forces the cmdlet to rename items that cannot otherwise be changed, such as hidden or read-only files or read-only aliases or variables. The cmdlet cannot change constant aliases or variables. Implementation varies from provider to provider. For more information, see about_Providers.
You need to use Move-Item -Force, as you already found out, and it's used in exactly the same way you're trying to use Rename-Item in your first code snippet:
Get-ChildItem -Filter '*Members*.csv' | Where-Object {
$_.LastWriteTime.GetDateTimeFormats()[44] -eq $today
} | Move-Item -Destination "hello2.csv" -Force
Beware that if after the Where-Object you have more than one item, each of them will replace the previous one, so you'll effectively end up removing all but one of them.

I want to remove recursively square brackets from file names

I have a lot of files in a directory containing square brackets for example:
Filename 1 [12454365].txt
I tried the following script but it's giving me the an error.
get-childitem -recurse | foreach { move-item -literalpath $_.name ($_.name -replace '\[.*\]', '')}
Error message
move-item : A device attached to the system is not functioning.
Only want to remove square brackets not everything in between!
If you specify -Recurse, you will need to specify the file with FullName because it will be targeted other than the current directory.
(Get-ChildItem -File -Recurse) | foreach {
$dest = Join-Path $_.DirectoryName ($_.Name -replace "[\[\]]")
Move-Item -LiteralPath $_.FullName $dest
}
Also, it is better to use Rename-Item for file renaming.
(Get-ChildItem -File -Recurse) | Rename-Item -NewName { $_.Name -replace "[\[\]]" }
I believe the issue is that you are replacing the brackets and everything in between.
Get-ChildItem * -Filter "*`[*`]*" | Rename-Item -NewName { $_.name -replace '\[','' -replace '\]','' }

Recursively removing/renaming files using powershell

I have to go through many levels of child folders and remove special characters that are invalid in SharePoint, mainly '#&'
I have scoured the internet trying different commands; rename-item/move-item, variations of the two, all to no avail. The closest i've gotten is using:
Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name -replace'[!##&]','_'}
but i keep getting this error: Rename-item: Source and destination path must be different.
Could someone point me in the right direction?
Regards
That error only happens when you attempt to rename a directory to the same NewName as the current name, you can safely ignore it.
Add -ErrorAction SilentlyContinue to silently suppress the error message:
Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name -replace'[!##&]','_'} -ErrorAction SilentlyContinue
You need to filter out the files that you're not planning to rename:
Get-ChildItem -Recurse |
Where-Object { $_.Name -match '[!##&]' } |
Rename-Item -NewName {$_.Name -replace '[!##&]','_'}
something like this may work
dir -Recurse -File | ? basename -Match '[!##&]' | % {
# if the file.txt already exists, rename it to file-1.txt and so on
$num = 1
$base = $_.basename -replace'[!##&]', '_'
$ext = $_.extension
$destdir = Split-Path $_.FullName
$newname = Join-Path $destdir "$base$ext"
while (Test-Path $newname) {
$newname = Join-Path $destdir "$base-$num$ext"
$num++
}
ren $_.fullname $newname
}