Renaming only certain part of a file name - powershell

I need to write a powershell script to rename only a certain part of a file name.
MTR needs to be changed to MTE, only the R needs changing, all others letters/numbers needs to stay the same.
Original file name
FILEORIG.MTR001
File needs to be changed to
FILEORIG.MTE001
Any help will be much appreciated.

Since you did not provide code I cant get you a ready to go script but this is the command you are after once you locate the file and pipe it..
<# Your Code Here, Probably a Get-ChildItem #> | Rename-Item -NewName {$_.name -replace "MTR","MTE"}

Related

Powershell - Select File Name instead of full path and file name

I'm sure this is so simple you may roll your eyes at it. I cannot figure out how to suppress the full path of the file when I get a match.
I only need the file name, Line number and matched word.
I have a text file of 15K copybooks. I need to know what program references any of them.
The script takes forever to run but I know of only one way to search all files in a directory looking for ANY or ALL of the contents in my file.
Can someone point out the error? The full path is not needed and adds a step or two to my process to clean things up.
Regards,
-Ron
$searchWords=Get-Content "C:\Workspace\Missing.txt"
Foreach ($sw in $searchWords)
{
Get-Childitem -Path "C:\Workspace\src" -Recurse -include "*.cbl" |
Select-String -Pattern "$sw" |
Select Path,LineNumber,#{n='SearchWord';e={$sw}}
}

How to search for a specific file name then move all files after that file to another folder using PowerShell

Let's say I have 10 PDF files in a folder named c:\Temp
1440_021662_54268396_1.pdf
1440_028116_19126420_1.pdf
1440_028116_19676803_1.pdf
1440_028116_19697944_1.pdf
1440_028116_19948492_1.pdf
1440_028116_19977334_1.pdf
1440_028116_20500866_1.pdf
1440_028116_20562027_1.pdf
1440_028116_20566871_1.pdf
1440_028116_20573350_1.pdf
In my search, I know I am looking for a file that will match a specific number, for example 19676803 (I'm getting the number to search for from a SQL Query I'm running in my script)
I know how to find that specific file, but what I need to be able to do is move all the files after the searched file has been found to another pre-defined folder. So using the 10 PDFs above as the example files, I need to move all the files "after" the file named 1440_028116_19676803_1.pdf to another folder. I know how to move files using PowerShell, just do not know how to do it after/from a specific file name. Hope that makes sense.
$batchNumCompleted = 'c:\Temp\'
$lastLoanPrinted = $nameQuery.LoanNumber
$fileIndex = Get-ChildItem -path $batchNumCompleted | where {$_.name -match $lastLoanPrinted}
Can anyone provide suggestions/help on accomplishing my goal? I'm not able to provide all code written so far as it contains confidential information. Thank you.
Use the .Where() extension method in SkipUntil mode:
$allFiles = Get-ChildItem -path $batchNumCompleted
$filesToMove = $allFiles.Where({$_.Name -like '*19676803_1.pdf'}, 'SkipUntil') |Select -Skip 1
Remove the Select -Skip 1 command if you want to move the file with 19676803 in the name as well

How to return full path to both folders AND files?

I have been tasked with auditing a file server at work, to review directory and file ownership, and subsequent meta data. I have scowered the internet to enable me to string together a script that returns the data required. The script I have created so far is as follows:
Get-ChildItem -r -Path '\\SERVERNAME.wan.net\launshared\Training' |
Select DirectoryName, UNC_Path_Parent_DIR, Name, Type, Extension,
#{N='Owner';E={$_.GetAccessControl().owner}},
#{N='SizeInKb';E={$_.Length/1kb}}, CreationTime, LastAccessTime,
LastWriteTime |
epcsv 'C:\Users\USERNAME\Desktop\Information Management\TrainingTEST - 2018- SCRIPT_RESULT.xls' -En UTF8 -NoType -Delim ','
Please ignore the UNC_Path_Parent_DIR entry, this simply inserts a field header with no returned data in the exported CSV file.
The script works fine, albeit with one exception...it does not return the full UNC path for the directories! Please note, the full UNC path is returned for the files but NOT the directories!
I have searched the internet for a solution, enquired with our IT guys, however cannot for the life of me locate a solution that will list the full UNC path to both directories AND files.
The UNC path is not a property of the objects coming from Get-ChildItem. What this means is that it needs to be added manually. One can do this, just like you did, by adding a custom property with an expression in Select-Object.
Please keep in mind that when others read your code it's always nicer to format things and use the full parameter name. It makes it easier to read and understand, also for you when you read it again in 10 years time ;)
I'll help you on your way by letting you know that you can find the code that gets the UNC path here and here. The only thing left is to implement it in the correct place within the Select-Object. I leave that as an exercise for you:
Get-ChildItem -Recurse -Path '\\fsabbey12.wan.net\launshared\Training' | Select-Object FullName, DirectoryName,
Name, Type, Extension,
#{N='UNC';E= {
# Code to get the UNC Path
}},
#{N='Owner';E= {$_.GetAccessControl().owner}},
#{N='SizeInKb';E={$_.Length/1kb}},
CreationTime, LastAccessTime, LastWriteTime
I think this will get you started.
I'll leave it here too as additional possible way (although #DarkLite1 answer covers the question quite fully).
It's possible that FullName will fulfill your needs without any custom expression.
If you use Get-ChildItem like this:
Get-ChildItem -Path \\servername.domain.com\C$\foldername\
Then using | Select FullName will give you path in the UNC format:
\\servername.domain.com\C$\foldername\\FirstSubfolder
Keep in mind that it applies only to the situation when you reference the remote folder, not folder from the machine you run the script on. If you use FullName on local folder you'll get format like this:
C:\FolderName\FirstSubfolder

Renaming files in bulk and in ascending order in CMD

I know this question was already asked by someone but I will ask again.
Can someone tell me how to rename in bulk and in ascending order if possible in CMD. I already tried renaming in powershell but to no avail. It only let me use once and I need to rename another folder files but to no avail. It didn't let it rename files in another folder. This is the code I use in powershell:
$i = 1
Get-ChildItem *.mkv | %{Rename-Item $_ -NewName ('Haikyuu - {0:D2}.mkv' -f $i++)}
I'm renaming my anime series per folder and some of my copies have 100+ videos. and somehow you could teach me what each code mean (the code that must use in CMD). The ones I've searched can't understand it in layman's term or doesn't tell the user how it's supposed to work. Thank you in advance. by the way, the folder is placed in an external drive.
so from the beginning:
$i= variable for storing the initial value 1
Get-ChildItem = is like "dir" which lists the files and folder under a certain path.
In this case, it is listing all the files which starts with anything but have the extension .mkv
* indicates wildcard.
| = pipeline which passes the output of the first command as an input of the next command.
% = ForEach-Object is iterating each object one by one coming from the pipeline.
$_= Current pipeline object . Here it is taking each object one by one and renaming it using Rename-Item
-NewName = is the parameter of the Rename-Item which asks for the new name to pass.
Hope it clarifies your need.
The reason why I can't rename my video files is there were [brackets] on the filename.
So I use this:
Get-ChildItem -Recurse -Include *.mkv | Rename-Item -NewName { $_.Name.replace("[","").replace("]","").replace("(","").replace(")","") }
Which on the same directories, I can access subfolders too to omit brackets and parethesis. then I proceed using the code above in the question to rename my files in every folder. The Reason why I'm doing the 'renaming' per folder is that, each folder is different anime series. but the code above is working.
if anyone can give me less code than repeating the 'replace' and concatenating it, I will gladly accept and choose that as the best answer. :)
If you use the parameter -LiteralPath for the source, no prior renaming is necessary.
%i = 1
Get-ChildItem *.mkv |
ForEach {Rename-Item -LiteralPath "$_" -NewName ('Haikyuu - {0:D2}.mkv' -f $i++)}
A hint on sorting, I hope the present numbering of the source files has a constant width, otherwise the result is mixed up as an alphabetic sort (which is inherent to ntfs formatted drives) will sort the number 10 in front of 2.
To check this append the parameter -whatif to the Rename-Item command

How to rename files in batch, remove all characters after ".mp3" in file name?

I think that windows powershell can be used to do this. I have a stack of mp3 files which have problem with their file name. There is a stream of characters in file name of each file after the terms ".mp3" which prevents them from being identified as mp3 files. I have tried renaming each file manually but am fed up now. I want to automate the process.
It should be possible to use powershell for this and perhaps there are other options which I am not aware of. The script needs to read each file name in the folder and if the name has any more characters after ".mp3" in the filename, all the subsequent characters shall be deleted. How to do this?
$files=ls -Name *.mp3*
foreach ($file in $files) {
Rename-Item $file $file.Substring(0, $file.LastIndexOf(".mp3")+4)
}
However, it's too long solution.
First I'm sure the Community would have liked to have seen you try something so that we could have helped you fix it. However other users have provided solutions so I would like to chime in from a PowerShell perspective
Get-ChildItem C:\temp -Filter "*.mp3*" | Rename-Item -NewName {$_.Name -replace "\.mp3.*",".mp3"}
This will take all files with ".mp3" somewhere in the file name. Then it will rename them by taking everything after and including ".mp3" with just that. If you have a file with ".mp3" more than once this would be an issue. Easily fixed but I will keep it terse by accepting that potential.
UPDATE
ok. so the wildcards are weird with the rename command.
this DOES work
ren "*.mp3*" "??????????????????????????????????????????????????????????.mp3"
as long as there are as many ? as the longest filename
refer to this if you want details
https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards