Replacing folder/file using wildcard - powershell

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.

Related

Renaming a Directory with wildcard on 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

How to prevent PowerShell -Recurse from renaming first file twice?

When using powershell to rename files with their directory name and file name, my code works, except in the first file in a directory, it gives it two copies of the directory name. So the file book1.xlsx in folder folder1 should become folder1book1.xlsx but it becomes folder1folder1book1.xlsx. The remaining files in folder1 are correctly named folder1book2.xlsx, folder1book3.xlsx, etc.
I have a directory, with many sub-directories. In each sub-dir are files that need their sub-dir name added in.
I've been following this code. For me it looks like:
dir -Filter *.xlsx -Recurse | Rename-Item -NewName {$_.Directory.Name + "_" + $_.Name}
I've also tried
--setting the Recurse -Depth 1 so that it doesn't keep looking for folders in the sub-folders.
--using ForEach-Object {$_ | ... after the pipe, similar to this.
--running it in Visual Studio Code rather than directly in PowerShell, which turns it into:
Get-ChildItem "C:\my\dir\here" -Filter *.xls -Recurse | Rename-Item -NewName {$_.DirectoryName + '_' + $_.Name}
--putting an empty folder inside the sub-directory, setting -Depth 2 to see if that will "catch" the recurse loop
I would expect the files to be named folder1_book1.xlsx, folder1_book2.xlsx, folder1_book3.xlsx.
But all of the attempted changes above give the same result. The first file is named folder1_folder1_book1.xlsx [INCORRECT], folder1_book2.xlsx[CORRECT], folder1_book3.xlsx[CORRECT].
A workaround might be writing an if statement for "not files that contain the sub-directory name" as suggested here. But the link searches for a text string not an object (probably not the correct term) like #_.Directory.Name. This post shows how to concatenate objects but not something like #_.Directory.Name. Having to put in an if statement seems like an unnecessary step if -Recurse worked the way it should, so I'm not sure this workaround gets at the heart of the issue.
I'm running windows 10 with bootcamp on a 2018 iMac (I'm in Windows a lot because I use ArcMap). Powershell 5.1.17134.858. Visual Studio Code 1.38.0. This is a task I would like to learn how to use more in the future, so explanations will help. I'm new to using PowerShell. Thanks in advance!
This was a script I created for one of my customers that may help
<##################################################################################################################################
This script can be used to search through folders to rename files from their
original name to "filename_foldername.extension". To use this script
please configure the items listed below.
Items to Congfigure
-$Original
-$Source
-$Destination
-$Files
Also please change the Out-File date on line 29 to today's date ****Example: 2019-10-02****
We've also added a change log file that is named "FileChange.txt" and can be found in the location identified on line 30
>
$Original="C:\temp\test" #Location of ".cab" files copied
$Source="C:\temp\Test" #Location were ".cab" files are stored
$Destination="C:\temp\Test\2019-10-02" #Location were you want to copy ".cab" files after the file name change. Be sure to change the date to the date you run this script. The script creates a folder with todays date
$Files=#("*.cab") #Choose the file type you want to search for
$ErrorActionPreference = "SilentlyContinue" #Suppress Errors
Get-ChildItem $Original -Include "*.cab" -File -Recurse | Rename-Item -NewName {$_.BaseName+"_"+$_.Directory.Name +'.cab'}
New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"; Get-ChildItem -recurse ($Source) -include ($Files) | Copy-Item -Destination ($Destination) -EA SilentlyContinue
Get-ChildItem $Original | Where {$_.LastWriteTime -ge [datetime]::Now.AddMinutes(-10)} | Out-File C:\temp\test\2019-10-02\FileChange.txt

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

Rename parts of file names that match a pattern

Say I have a list of file names like this:
some-file.ts
my-project-service.ts
other.ts
something-my-project.ts
I need to change the file names that have my-project in them to have just that part renamed to $appname$.
So my-project-service.ts would become $appname$-service.ts
And I need to do this recursively from a root directory.
I seem to be to be hopeless at PowerShell so I thought I would ask here to see if anyone can help me out.
Use the Get-ChildItem cmdlet with the -recurse switch to get all items recursivly from a root directory. Filter all items containing my-project using the Where-Object cmdlet and finally rename them using the Rename-Item cmdlet:
Get-ChildItem "D:\tmp" -Recurse |
Where {$_.Name -Match 'my-project'} |
Rename-Item -NewName {$_.name -replace 'my-project','$appname$' }

rename file.jpg.jpg to file.jpg using powershell

I inadvertently named over a thousand files as filename.jpg.jpg. My desired end state is to have the file name as filename.jpg. How can I use PowerShell to fix this?
I have tried many examples from blogs and find that the first .jpg is apparently being seen as the file extension. Any assistance would be greatly appreciated as my only alternative is to manually rename all the files.
Try this,
Get-childItem constant* | % {rename-item $_.name ($_.name -replace '.jpg.jpg','.jpg')}
it will replace .jpg.jpg to .jpg
You could use the Get-ChildItem cmdlet to retrieve the files and prefilter it using the -Filter parameter.
You should use the FullName property instead of the Name for the Rename-Item cmdlet whenever your working directory is a diffrent one.
The regexI use here escapes the periods (mentioned by Matt) and also ensures to match the end of the filename ($).
Get-ChildItem -Path 'YOUR_PATH_HERE' -Filter '*.jpg.jpg' |
foreach { Rename-Item $_.FullName ($_.FullName-replace '\.jpg\.jpg$','.jpg') }
Note: If you need to recursively rename the files, you just need to add -recurse to the Get-ChildItemcmdlet.