Change multiple folder name from %-% to % (delete everything after the hyphen) - windows

I have a folder containing multiple sub folders named like : 12345 - textfoldername
I want to rename all these sub folders by keeping just the first number (12345) and delete all the rest ( - textfoldername).
How can I build the windows script for that.
Thanks for your help !

With powershell, use Get-ChildItem to discover all the subfolders, then use Rename-Item to rename:
Get-ChildItem path\to\root\directory -Directory |Rename-Item -NewName {$_.Name -replace ' - .+$'}
The -replace operator with remove - and anything thereafter in the existing name (or, if - something isn't found, ignore it)

Related

How to get nested path of all empty windows directory using cmd

I have a folder and numerous nested folders are present inside that. Many of the folders are empty and I have to copy an empty.property file in those empty folders. So that the end result will be no folder will be completely empty, either it contains another folder, any other file(s) or this empty.property. I have tried to get all the paths using dir /b /s, but it is returning all the paths, not only the empty one. Can anyone help me to get that very efficiently. Thanks.
You can use powershell to do it several ways with one being:
Get-ChildItem -Path C:\Temp -Directory | Where-Object {$_.GetFileSystemInfos().Count -eq 0} |
ForEach-Object -Process { Copy-Item -Path "My\File\to\copy\Path" -Destination $_.FullName }
Basically checks to see which directory doesnt have no files or folders in it, then pipes it to a foreach to a process a Copy-Item request for whatever file/folder you want it to copy from, to the empty folder.

Writing a powershell command to combine specifically named folders

I'm looking for a command that will search through all of the subfolders of my main customer files folder, identify any folders labelled as "Quotes-Tenders" and merge them with the file called "Quotes and Enquiries".
The customer files folder is located Y:\Customer Files and contains 1,285 to search, the search only needs to go one level down.
I have tried the rename command on a test folder however this runs into a conflict error with having two of the same named file.
PS C:\Users\Gareth> Get-ChildItem C:\Users\Gareth\Desktop\Test -Recurse -Directory | Where-Objects-Tenders*"} | Rename-Item -NewName { $_.name-replace 'Quotes-Tenders', 'Quotes and Enquiries'}
Any help on how to get around this issue would be great.
i propose you this code
Get-ChildItem "C:\Users\Gareth\Desktop\Test" -Recurse -Directory |
Where Name -like "*Quotes-Tenders*" |
sort -Descending {($_.FullName -split '\\').Length } |
%{
$newname=Join-Path -Path $_.Parent.FullName -ChildPath $_.Name.Replace('Quotes-Tenders', 'Quotes and Enquiries')
Rename-Item $_.FullName -NewName $newname
}
For explain:
List item
I take all directory tree
I select with where clause,
I sort by number of directory section because you can have a diretory
like this
'C:\temp\Quotes-Tendres ggg\Quotes-Tendres. ffff'
and
'C:\temp\Quotes-Tendres ggg' in you list, in this case i want rename first 'C:\temp\Quotes-Tendres ggg\Quotes-Tendres. ffff'
I rename the directory

Renaming files by removing parts of the filename

I'm trying to go through all the files within a folder and rename them so that the name becomes whatever is between the 2nd and 3rd _.
e.g.: 1_2_3_4.pdf becomes 3.pdf
Having looked around, I was able to find the link below which helps with a very similar issue - but I am unable to make it work for my specific issue. Would anyone minding helping with this?
Find character position and update file name
Use the Get-ChildItem cmdlet to retrieve all files and rename it using the Rename-Item cmdlet (together with a regex replace):
Get-ChildItem 'c:\your_folder' |
Rename-Item -NewName { '{0}{1}' -f ($_.Name -replace '.*?_.*?_([^_]+).*', '$1'), $_.Extension }

Renaming Files with PowerShell

I need to rename a bunch of files at once in Windows PowerShell. I read the HTG article here and it helped a little.
My problem is that It will only rename files in the top of the directory, nothing deeper. For example: There is FOLDER A and inside FOLDERA is a document and FOLDER B. Inside FOLDER B is another document. Both folders and both documents need to be renamed. The way it is working now is that FOLDER A, the document in FOLDER A, and FOLDER B are being renamed, but not the document inside FOLDER B.
My current code is:
Dir | Rename-Item –NewName { $_.name –replace “ “,”_” }
Thanks for the help!
You need to specify the -Recurse parameter on Dir to get it to recurse e.g.:
Dir -recurse | Rename-Item -NewName {$_.Name -replace ' ','_'}
BTW this may run into a problem because you're renaming the folder (FOLDERB) that contains the document first but the item being piped that corresponds to the file in FOLDERB still has the old name. In this case, you want to rename from the bottom up. One very crude but effective (I think) way to do this is to sort the file items on their path length descending e.g.:
Dir -recurse | Sort {$_.FullName.Length} -Desc | Rename-Item {$_.Name -replace ' ','_'}

Rename multiple files in a folder and its subfolders to a fixed pattern

I have a large number of files to be renamed to a fixed pattern on my Windows 8 computer.
I am looking for a way so that I can rename the files in a quick way, any way: like any software that does it or any command on command window.
Following is what I need to do:
Original file name: Body Begger Power.docx.htm
Need this to be: body-begger-power.html
From the root directory you can try this:
Get-ChildItem -Force -Recurse | Move-Item -Destination {$_.FullName.ToLower() -replace ' ', '-'}
I don't see any pattern for removing the extension. If all files are docx.html and you want them changed to html, you could simply do another replace like this:
Get-ChildItem -Force -Recurse | Move-Item -Destination {($_.FullName.ToLower() -replace ' ', '-') -replace '.docx.htm$', '.html'}
for /r %i in (.) do if exist "%i\body begger power.docx.htm" ECHO ren "%i\body begger power.docx.htm" body-begger-power.html
from the prompt will scan the tree from . (the current directory - or substitute the directoryname from which you want to start). This will simply ECHO the filenames detected to the screen - you need to remove the ECHO keyword to actually rename the file.

Resources