Renaming Recursively with if-condition - powershell

I'm having big troubles combining the rename with an if-condition. PS cannot rename when the file has the same name as before and won't do anything afterwards.
Plus it's unable to rename recursively.
I wanna do sth like:
$entrys = Get-ChildItem -Recurse $myPath
foreach ($name in $entrys) {
$newName=$name.FullName -replace('[\,\/\*\+\#\~\-\=\^\°\$\%\&\<\>\|]','')
if (-not ($newName -like $name)) {
Rename-Item $name -NewName $newName
}
}
I found something similar here but they dont have any conditions there.
So how do I pipe into an if-condition before renaming, so I can pipe directly from get-childitem -recurse to if() then rename()? Or is there another possibility?
Thanks for help

When I try your code I have the same issue that it reports it's unable to rename the file. However, I modified a bit of your code, and now it seems to work:
$entrys = Get-ChildItem -Recurse $myPath -File
foreach ($name in $entrys) {
$newName = $name.Name -replace('[\,\/\*\+\#\~\-\=\^\°\$\%\&\<\>\|]','')
if ($newName -ne $name.Name) {
Rename-Item -LiteralPath $name.FullName -NewName $newName
}
}
First you have to only check for files and not folders. Because once you renamed a folder the files are no longer found when you recurse into a non existing folder name (one that has changed).
If you need to rename folders also. It needs some more work as the folder names might change.
Hope this helps you out.

My PowerShell doesn't throw any error on the Rename-Item cmdlet if the file has the same name. But you could try to use -ErrorAction 0.
However, this oneliner works for me:
gci $myPath -r | % { Rename-Item $_.FullName ($_.FullName -replace('[\,\/\*\+\#\~\-\=\^\°\$\%\&\<\>\|]','')) }

Related

replacing files names with split output

I am trying to use PowerShell to read filenames from a dir;
then within a for loop:
split names using a delimiter; store desired output in a new variable. Now I want to replace the original filenames in the directory with this new variable. So far I have gathered the following with the expected outputs shown:
$files = Get-ChildItem -Path C:\Test
write-output $files
Directory: C:\Test
1_N04532L_LEFT.JPG
2_N04532R_RIGHT.JPG
code continues
foreach ($file in $files)
{
$nameArray = $file -split "_"
$newName = $nameArray[1]
write-output $newName
}
N04532L
N04532R
Any Ideas on how to accomplish this. I am not a programmer and there is lots of data on this, but it's not working for me.
As both commenters already explained, there is the Rename-Item cmdlet for renaming files.
Since this cmdlet can take a scriptblock in its NewName parameter, you can use that to create a new filename.
# adding switch -File makes sure you do not also try to rename subfolders
$files = Get-ChildItem -Path 'C:\Test' -File
foreach ($file in $files) {
$file | Rename-Item -NewName { '{0}{1}' -f ($file.BaseName -split '_')[1], $file.Extension }
}
You can shorten this by piping the results from Get-ChildItem trhough one-by-one to the Rename-Item cmdlet.
Because we're piping the FileInfo objects here, we can make use of the $_ automatic variable
# enclose the Get-ChildItem cmd in brackets so this will enumerate the files to completion
# before passing them on to te Rename-Item cmdlet.
# if you don't, files you already have renamed could be picked up and processed again..
(Get-ChildItem -Path 'C:\Test' -File) |
Rename-Item -NewName { '{0}{1}' -f ($_.BaseName -split '_')[1], $_.Extension }
Note: when renaming files, you can always run into naming collisions, upon which you will receive an exception

Removing parts of filenames in Powershell from a file list

I have a bunch of directories with files in them and my goal is to loop through a directory structure and write each of the files one by one to a new location.
The files on disk look like this:
- DATAFILE1_DATE_20210101_RUNDATE_20210101.csv
- DATAFILE1_DATE_20210102_RUNDATE_20210102.csv
- DATEFILE2_DATE_20210103_RUNDATE_20210103.json
- DATEFILE2_DATE_20210104_RUNDATE_20210104.json
I'm trying to pass the contents of the directory to a variable $fileSystemItems and then to remove everything after _DATE so that I could build a new directory structure in the target as:
- /target/DATAFILE1/DATAFILE1_DATE_20210101_RUNDATE_20210101.csv
- /target/DATAFILE1/DATAFILE1_DATE_20210102_RUNDATE_20210102.csv
- /target/DATAFILE2/DATEFILE2_DATE_20210103_RUNDATE_20210103.json
- /target/DATAFILE2/DATEFILE2_DATE_20210104_RUNDATE_20210104.json
The PS code I have so far takes the files from a specified directory and outputs them:
$sourcePath = "\\files\data"
$fileSystemItems = Get-ChildItem $sourcePath -Recurse | Where { ! $_.PSIsContainer }
foreach ($file in $fileSystemItems) {
Write-Host "Writing name of the file is $($file.BaseName)"
}
I have tried using the Rename-Item and regex but renaming the files the source is not an option as other programs are accessing the same data, for example:
Get-ChildItem $sourcePath -Recurse | Rename-Item -NewName { $_.Name -replace "^_DATE+","" }
How do I modify the filename in the $file variable in the foreach loop to output both a edited version of the file name (e.g. DATEFILE1) and also the full file name DATAFILE1_DATE_20210101_RUNDATE_20210101.csv ?
Rename-Item is a great tool to begin with! First, let's optimize your code snippet a little bit:
Instead of piping Get-ChildItems output to Where { ! $_.PSIsContainer }, we'll use the -File switch.
Let's also change your regex from ^_DATE+ to _DATE.* and use a pipeline with ForEach-Object:
Get-ChildItem $sourcePath -Recurse -File | ForEach-Object {
Rename-Item -Path $_.Fullname -NewName (($_.Name -replace "_DATE.*","") + $_.Extension)
}
Looking at your destination structure it looks like you might also want to move files to a target directory.

Rename files in subfolders

I am trying to rename files in subfolders in a certain pattern, but I am stuck.
The situation is as follows: I have multiple folders which are sometimes named as the target filename depending on the length, but the name does not really matter.
In each folder are always 2 files: the Target-File with a random name and the correct extension, and the Source-File which is always the correct BaseName with a txt-extension.
For example:
Folder1\7393028473.docx
Folder1\January.txt
Folder2\9373930843.pdf
Folder2\February.txt
My goal is to rename every not-txt-file with the Basename of the txt-file. Executed, it should be like:
Folder1\January.docx
Folder1\January.txt
Folder2\February.pdf
Folder2\February.txt
With gci I was able to create both lists but didn't find a good way for the renaming.
$SourceName = gci -File -Recurse | Where {$_.Extension -ne ".txt"}
$TargetName = gci -File -Recurse | Where {$_.Extension -eq ".txt"}
I did also try to use gci for renaming, but was not able to tell it to use the newname based on the txt-file:
gci -File -Recurse | Where {$_.Extension -ne ".txt"} | Rename-Item -NewName {$_.extension -eq ".txt"}
This only renamed the .docx-file to "FALSE" because the filename already exists.
What I did not try (but would be ok) is to not only rename the file, but also move it to the parent directory.
This is one way to do it but it would fail as soon as there are 2 or more files with a different extension than .txt but having the same extension. It would also fail as soon as one folder has more than one .txt file.
# Get all folders under 'TargetDirectory'
Get-ChildItem TargetDirectory -Directory -Recurse | ForEach-Object {
# For each sub-folder, get their files
$childs = $_.EnumerateFiles()
# Filter and split the child files by their extension
$txt, $notTxt = $childs.Where({ $_.Extension -eq '.txt' }, 'Split')
# Use the BaseName of the '.txt' File but the Extension of
# the file being renamed
$notTxt | Rename-Item -NewName { $txt.BaseName + $_.Extension }
}
Thanks for your reply and sorry for my late reply.
I tried your code but its not working correctly:
The NewName is created correctly, but the problem is the rename-function or rather the notTxt list because it only contains the item itself but not hte full path.
When I copy the file which should be renamed into the parent-directory your code does work in the file in the parent-directory.
There was another answer which apperntly was deleted but did work.
I also tried a foreach-loop in one of my tries but didn't get the NewName to work.
I don't know why, but I didn't consider creating the NewName with a variable, which was done in the deleted answer:
$folders = gci -Directory -Recurse
foreach ($folder in $folders) {
$targetFile = gci $folder | Where {$_.Extension -ne ".txt"}
$sourceFile = gci $folder | Where {$_.Extension -eq ".txt"}
$newName = $sourceFile.BaseName + $targetFile.Extension
Rename-Item $targetFile.FullName $newName
}
Of course you can try and get your code to work, but I can make do with this code.
Thank you very much for your help.

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 - Recursivly change path containing a value

I recently started using powershell, cause i find it very usefull and powerfull :)
So this is the scenario :
Customer have a folder structure that used to have commas in it.
Because of their latest program addition, this fails, due to the commas.
Essentially, they just want to replace the commas with for example " - "
This is what i have.. And it works, but i feel it's inefficient.
The script does what it's supposed to, but as soon as one path is changed, the remaining will fail because the original path have been changed, you follow me? :)
$folderpath = "C:\pathcontainingcomma"
foreach ($i in get-childitem $folderpath -Recurse) {
$name = $i.name.replace(","," - ")
Rename-Item -Path $i.fullname -NewName $name -Force -Verbose
}
i hope i explained well enough, please ask questions if you have any :)
You would need to rename from the inside out to avoid changing paths you still need to process, so I guess the following should work:
$folderpath = 'C:\pathcontainingcomma'
$items = Get-ChildItem -Recurse $folderpath
[Array]::Reverse($items)
$items | ForEach-Object { Rename-Item -Path $_ -NewName ($_ -replace ',', '-') -Force -Verbose }
Try this:
Get-ChildItem $folderPath -r '*,*' -name | foreach {
$p = "$folderPath\$_"; rename-item $p ($p -replace ',',' - ') -whatif }
The -Name paramete is handy here because the output order with -Name would allow you to rename the higher level dirs first and the replace fixes up all the elements of the path as you foreach through the output.