Renaming a Directory with wildcard on Powershell? - powershell

I have a folder that needs to be renamed.
$home/dir1/dir_to_rename_12344343
$value=abcd-1234
I know the beginning pattern of the folder but not the whole name.
I was trying to do something like this
cd $home/dir1
Rename-Item 'dir_to_rename*' '$value'
but it keeps giving me an error.
I am also happy if I can just trim the last few chars of the folder name.
i.e., $home/dir1/dir_to_rename_12344343 --> $home/dir1/dir_to_rename

I was finally able to get it to work
Get-ChildItem -Path $home/dir1 | Rename-Item -NewName $Value

Related

Can't delete Windows folder that contains a subfolder named "..." which contains a subfolder named "..." etc

I'm stumped.
I have a folder called c:/data/ToDelete. It used to contain files as well as a single subfolder called ..., which contained the same files as well as single subfolder named ... and this went on ad nauseum.
I moved the contents of the ToDelete folder elsewhere, but the subfolder/subfolder structure remains, and I can't get rid of it.
This is what it looks like in Explorer
This PC > Data > ToDelete > ... > ... > ... > ... > etc
clicking on the above reveals the path: C:\Data\ToDelete\
I was able to rename the first folder (from c:/data/temp/ to c:/data/ToDelete/) but whenever I try to rename the subfolder I get the "This folder is already open" (probably because it is just referring back to it's parent folder).
Things I've tried (none worked)
the basics: Delete key, Shift Delete, drag to recycle bin, send to recycle bin
command prompt delete, including trying the ASCII code for . just in case.
I tried zipping it with 7zip
I did a chkdsk and got no errors
I tried removing with Revo Uninstaller Pro (the Delete folder option).
I tried renaming the subfolders with this powershell script that I found online thinking I could then delete them but got an error:
Get-ChildItem -Recurse | where {$_.attributes -eq "Directory"} | Where-Object {$_.Name -match '...'} | Rename-Item -NewName { $_.Name -replace '...' , 'ToDelete' }
The funny thing is I found the folder in my backup directory as well, so somehow the entire thing can get copied (which means I have to remove it in multiple places now).
You need to do two things to enable PowerShell to process files or folders literally named ...:
Use -LiteralPath instead of -Path parameter where possible, to prevent PowerShell from parsing the path.
Prepend \\?\ to the full path to tell the underlying Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.
Example:
# For demonstration, create two nested sub folders literally named '...'
New-Item -ItemType Directory -Path '\\?\c:\data\ToDelete\...\...'
# Rename all '...' sub folders to 'ToDelete'
Get-ChildItem -LiteralPath '\\?\c:\data\ToDelete' -Directory -Recurse -Filter '...' |
Sort-Object { $_.Fullname.Length } -Desc |
Rename-Item -NewName ToDelete
Before renaming the folders, we must sort by path length (descending) to make sure that deepest nested folders will be renamed first. Otherwise paths enumerated by Get-ChildItem would be incorrect after renaming the first folder.
For what you have tried:
Note that -match '...' doesn't find folders named literally like "...". The -match operator's RHS operand is a regular expression pattern and as such would match the first sequence of any three characters in the string. The same can be said about -replace '...'. To literally match "...", use -eq '...' or escape the dots like this when used in RegEx pattern: \.\.\.

Replacing folder/file using wildcard

I am new to programming and need a bit of a hint.
I would need to replace a folder/file in a certain location that has a date as a prefix, hence the name of the folder is dynamic like:
C:\20200825_Folder1 I d like to rename this folder in C:\A_Folder1
I tried the following:
get-item "C:\*Folder1 | Rename-Item -NewName {$_.Name -replace "*Folder1", "A_Folder1"}"
or
Rename-Item "C:\*Folder1" -NewName "A_Folder1"
As a solution I ve copied the "*Folder1", renaming it to "A_Folder1" and deleting "*Folder1".
However, I am not sure if that is the BestPractice to do, so I ve thought I asked a bit more experienced developers.
I do not expect code if you do not wish, but I would be thankful for any advice.
If you don't want to make the new name of the folder depending on the old name of the folder you can simply provide the new name of the folder .... like this:
Get-Item -Path 'C:\*Folder1' |
Rename-Item -NewName 'A_Folder1'
BTW: Rename-Item does not allow wildcards for the -Path parameter.

Get-ChildItem piped to Copy-Item with rename to same directory

I am trying to bulk copy/rename files of a certain extension in a specific folder to the same folder with a prefix. I am certain this is a problem with my own Powershell inexperience. I simply do not know how to set up the command.
I am trying to take all the files of extension .mxd within a folder and rename them from something.mxd to _prefix_something.mxd_
I am pretty sure the first section is fine: Get-ChildItem *.mxd
But I don't know how to work with Copy-Item well enough. My understanding is that -Destination is where you would put the new file name, so how do I input ("current-folder\prefix" + $_.Name + ".mxd") ??
I am sure I am just missing a few key points to get this to work. All I am getting is a single "prefix_.mxd" file. It is the same size as the last file in the Get-ChildItem list. But if I bring the foreach loop back in, it asks for path values. What am I missing?
Like this? The doc for rename-item has a similar example.
dir *.mxd | copy-item -destination { 'prefix_' + $_.name } -whatif
Stupid. I have to force scriptblock with curlybraces {} instead of using parenthesis (), and I can skip the path to the current folder by starting with the prefix.

Rename bulk files with given name, in directory and subfolders using PowerShell

I’m looking for a solution to rename all files with given name in the directory and subfolders.
E.g.
There is a directory called c:\blah and in this folder, there is one file called BaR.txt and one folder called foo. In the foo folder, there is another file called bAr.txt.
I want to find all files with name bar.txt and change the name of each file to “neo.txt”. It must also be possible to rename the files to lower case as well e.g. bar.txt.
So far, I’ve tried to do this manually using the Windows Explorer in Windows10 but for some weird reason when I try to rename the bulk files there is extra “(1)” sign added to the end of each file name.
I’ve tried to play with PowerShell and I created a command
$_.name -replace 'bar.txt','neo.txt'
But it didn’t work for me.
To do this, you need 2 cmdlets: Get-ChildItem and Rename-Item.
This should work:
Get-ChildItem -Path 'C:\Blah' -Filter 'bar.txt' -Recurse | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'neo.txt'
}
However, if inside the foo folder there are more subfolders, this code will rename all files called bar.txt (case-insensitive) it finds in all of these subfolders.
If you do not want to go any deeper than the first subfolder in C:\Blah (the one called foo), you can use the -Depth parameter on the command like this:
Get-ChildItem -Path 'C:\Blah' -Filter 'bar.txt' -Recurse -Depth 1 | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'neo.txt'
}

powershell- recursive call to rename file names in bulk

I am trying renaming the files recursively.
My sample file name is:
2011.02.21 Work Plan - Greg Graham_v1.0__977a6c84-340a-442f-997e-aea94308b382.pdf
I want to delete the string __977a6c84-340a-442f-997e-aea94308b382 which starts with two underscore + 36 characters of identifier.
So result filename will be :
2011.02.21 Work Plan - Greg Graham_v1.0.pdf
All the files are in the mentioned folders or subfolders.
I am using following PowerShell :
Get-ChildItem -Path E:\Recover\test -Recurse | Rename-Item -NewName{$_.name -replace{$_.name.SubString({$_name.IndexOf("__")},38)},""}
When I was using -WhatIf then it shows all the files. But if I use without -WhatIf . It doesn't delete anything.
With-WhatIf it shows both target and destination filenames same.
Appreciate your help.
I think you'd be better off with a regex match. Something like:
GCI $path -recurse | Where{$_.BaseName -match "(.+?)__.{36}$"} | ForEach{Rename-Item -Path $_.FullName -NewName "$($Matches[1])$($_.extension)"}
That will capture the beginning of the file's name (assuming the file name without extension ends in two underscores followed by 36 characters), and then rename the file based on that capture, and the file's original extension.