Overwrite existing file and Rename - powershell

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.

Related

Excluding Folders with Get-ChildItem - Need Help Debugging a Script

I've searched through both StackOverflow and SuperUser to try to figure this out, and I'm still getting plagued by a problem I can't figure out how to fix. I know it's something simple, but after playing with it for an hour I'm still stumped. Simple question: how the heck do I tell Get-Childitem to exclude folders?
Right up front here's the code that doesn't work:
$sourceDir="E:\Deep Storage"
$targetDir="W:\Deep Storage"
$excludeThese = 'Projects2','Projects3','Projects4';
Get-ChildItem -Path $sourceDir -Directory -Recurse |
where {$_.fullname -notin $excludeThese} |
Get-ChildItem -Path $sourceDir | ForEach-Object {
$num=1
$nextName = Join-Path -Path $targetDir -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $targetDir ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$_ | Move-Item -Destination $nextName -Force -Verbose -WhatIf
}
}
The underlying concept here already works:
$sourceDir="E:\Deep Storage"
$targetDir="W:\Deep Storage"
Get-ChildItem -Path $sourceDir -File -Recurse | ForEach-Object {
$num=1
$nextName = Join-Path -Path $targetDir -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $targetDir ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$_ | Copy-Item -Destination $nextName -Verbose
}
Basically what this does is it moves folders from one place to another, and if files exist in both places, it renames files coming in. It helps keep my archive drive clear. But there are three folders there that I want to exclude because I still pull assets from them regularly, so I don't need those files moved.
Hence the difference between the two code samples: in the first one, I'm trying to get Get-Childitem to exclude a specific trio of folders, while this second one just grabs everything all at once.
I tried just doing a straight -Exclude with $excludeThese as the variable, without success; I tried skipping the variable approach altogether and just putting the folder names in after -Exclude. Still didn't work. I also tried putting in the entire path to the folders I wanted to exclude. No good--no matter what I did, the -WhatIf showed that the script was trying to move everything, including the folders I was theoretically excluding.
The last trick I tried was one I came across here on SO, and that was to go a gci with the exclude argument first, then do another gci after it. That still failed, so now I have to turn to the experts for help.
I would use a regex string created from the (escaped) directory names to exclude to make sure files withing these folders are ignored.
Also, by using a lookup Hashtable of all file names already present in the target folder, figuring out if a file with a certain name already exists is extremely fast.
$sourceDir = 'E:\Deep Storage'
$targetDir = 'W:\Deep Storage'
$excludeThese = 'Projects2','Projects3','Projects4';
# create a regex string with all folder names to exclude combined with regex OR (|)
$excludeDirs = ($excludeThese | ForEach-Object { [Regex]::Escape($_) }) -join '|'
# create a lookup Hashtable and store the filenames already present in the destination folder
$existingFiles = #{}
Get-ChildItem -Path $targetDir -File | ForEach-Object { $existingFiles[$_.Name] = $true }
Get-ChildItem -Path $sourceDir -File -Recurse |
Where-Object {$_.DirectoryName -notmatch $excludeDirs} |
ForEach-Object {
# construct the new filename by appending an index number if need be
$newName = $_.Name
$count = 1
while ($existingFiles.ContainsKey($newName)) {
$newName = "{0}_{1}{2}" -f $_.BaseName, $count++, $_.Extension
}
# add this new name to the Hashtable so it exists in the next run
$existingFiles[$newName] = $true
# use Join-Path to create a FullName for the file
$newFile = Join-Path -Path $targetDir -ChildPath $newName
$_ | Move-Item -Destination $newFile -Force -Verbose -WhatIf
}
Assuming the excluded directories are at the top:
$sourceDir="E:\Deep Storage"
$excludeThese = 'Projects2','Projects3','Projects4'
get-childitem $sourcedir -exclude $excludethese | get-childitem -recurse

Find Files in Subdirectories with Specific Text and Rename

From root folder, I am trying to search all files in subdirectories with "-poster" and rename those files to only poster without changing the file extension. All these files have other text in front of the "-poster" that I want to remove, and all the files are .jpgs.
Would a command such as this work:
Get-ChildItem -filter *-poster* -recurse | ForEach {Rename-Item $_ -NewName "poster.jpg"}
I haven't tried it, as don't want to risk changing other files.
Use the -WhatIf common parameter to preview an operation without actually performing it:
Get-ChildItem -Filter *-poster* -Recurse |
Rename-Item -NewName poster.jpg -WhatIf
Note:
-WhatIf does not test actual runtime conditions.
That is, if a poster.jpg file already exists in the target directory, the Rename-Item call will fail once -WhatIf is removed.
To handle this case, you have the following options, depending on your use case:
Allow the failure, if you don't expect a preexisting file.
Ignore the failure, if a preexisting file implies that no action is needed: add -ErrorAction Ignore to the Rename-Item call.
Blindly overwrite the existing file, by adding -Force to the Rename-Item call.
Create a file with a different name, such as by appending a sequence number, using a delay-bind script block - see below.
Get-ChildItem -Filter *-poster* -Recurse |
Rename-Item -NewName {
$newBaseName = 'poster.jpg'
$newBaseName = [IO.Path]::GetFileNameWithoutExtension($newName)
$newExtension = [IO.Path]::GetExtension($newName)
$suffix = ''
if ([array] $preexisting = Get-ChildItem -File -LiteralPath $_.DirectoryName -Filter "$newBaseName*$newExtension")) {
$highestNumSoFar = [int[]] ($preexisting.BaseName -replace '^.*\D') | Sort-Object -Descending | Select-Object -First 1
$suffix = $highestNumSoFar + 1
}
$newBaseName + $suffix + $newExtension
} -WhatIf

Rename all folders with given name in a directory using powershell

I want to find all folders in the directory by name e.g. Help and rename them to blah.
Ive tried this:
Get-ChildItem -Path 'C:\dev\foo' -Filter 'Help' -Recurse | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'blah'
}
but it doesn't seem to work. Any ideas? Cheers
$path = "C:\dev\foo"
$oldName = "Help"
$newName = "blah"
Get-ChildItem -Path $path -Filter "*$oldName*" -Recurse |
Rename-Item -NewName { $_.name -Replace $oldName, $newName } -WhatIf
You were missing the wild card - "*$oldName*". This way you are searching for all folders containing Help in the name, not just the one named Help. The -WhatIf parameter will show you all of the folders that will be renamed without actually renaming them. Remove it when you check if the result will be correct.

Powershell move and rename files

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

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
}