Akbari
Akbari

Reputation: 2409

How to set file type for files without any type

In a directory, there are some files without a file type. So they don't have a .xxx, I know their type and I want to set that.

How can I get their list and add their type, for example .srt?

I can get a list of all files with a file type, with the following command:

Ls * | ? {$_.name -match '\.\w{3}$' } | FT

But, still the following command doesn't give the desired result:

Ls * | ? {$_.name -match '[^\.\w{3}]$' } | FT

Also, I need some help with renaming them and adding their type.

Upvotes: 1

Views: 140

Answers (1)

Bluecakes
Bluecakes

Reputation: 2159

If you want to set the extension of all the files in a folder without an extension to .srt you can use Rename-Item from the pipeline like this

Please be careful, this will change the extension of ALL files without an extension in the directory you choose to .srt

Get-ChildItem -Path C:\Path\To\Files | Where-Object -FilterScript {-not $_.Extension} | ForEach-Object { Rename-Item -Path $_.FullName -NewName ($_.FullName + ".srt") }

Explanation of this pipeline below

Get all files

Get-ChildItem -Path C:\Path\To\Files

Where the file doesnt have an extension

Where-Object -FilterScript {-not $_.Extension}

For each file in the directory

ForEach-Object { }

Rename the Item to what it was called before ($_.FullName) + the extension of our choice (.srt)

Rename-Item -Path $_.FullName -NewName ($_.FullName + ".srt")

Upvotes: 2

Related Questions