Renaming Files with PowerShell - windows

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

Related

How can I get a list of folders/subfolders that DOES NOT contain a specific type of file?

For example, I wanted to print a list of folders which contain an .html file. Correct me if I am wrong, but this worked for me:
dir /S *.html > C:\Users\PC\Desktop\with-html.txt
Now I would like to find the folders which do not contain .html files.
How do I go about that?
EDIT:
The folders are structured in a way that only the child folders (last subfolder) have any kind of files. I would like to get a list of directories to those subfolders. So the above command line is giving me:
C:\...\ml\regression\lasso-regression
C:\...\ml\regression\linear-regression
There is not output just C:\...\ml or C:\...\ml\regression.
The folder structure looks like this:
C:\...\ml
classification
regression
lasso-regression
linear-regression
There are about 10 folders in folder ml and no files. There are again about 10 folders in second folder level where C:\...\ml\regression\linear-regression contains an HTML file while C:\...\ml\regression\lasso-regression does not contain a file with file extension .html. Only the folders in last level of the folders tree have files at all.
I'd be grateful getting just the list of the last folders in folders tree not containing a file with file extension .html.
I basically output the above command line into a .csv file, filtered it with MS Excel, and have now a list of folders with .html file(s). I'm basically working with R-markdown files, and it'll be a status report, the folders list with .html files is what I have completed already. So in need now only the opposite folders list.
Not difficult using PowerShell.
Get-ChildItem -Recurse -Directory |
ForEach-Object {
if ((Get-ChildItem -File -Path $_.FullName -Filter '*.html').Length -eq 0) { $_.FullName }
}
If you must run this in a .bat file script, the following might be used.
#powershell.exe -NoLogo -NoProfile -Command ^
"Get-ChildItem -Recurse -Directory |" ^
"ForEach-Object {" ^
"if ((Get-ChildItem -File -Path $_.FullName -Filter '*.html').Length -eq 0) { $_.FullName }" ^
"}"

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.

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

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)

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 }

Resources