Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/6910af03-f58c-800b-a67e-c1aff06d11d4
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{DISPLAYTITLE:Powershell rename command}} ''This conversation was exported from ChatGPT''<ref>{{cite web|title=Powershell rename command|url=https://chatgpt.com/share/6910af03-f58c-800b-a67e-c1aff06d11d4|publisher=ChatGPT|access-date=2025-11-10}}</ref> == gpt-5 == === User: Help me with an inline Powershell command to rename bunch of files, which has 'Snapchat-' in it while keeping the rest … === Help me with an inline Powershell command to rename bunch of files, which has 'Snapchat-' in it while keeping the rest as it is. I want the 'Snapchat-' to be replaced by 'IMG_' followed by the date in the YYYYMMDD format and then the rest as it is. You can take the date from the files metadata individually. === Assistant: Got it 👍 — you want a single-line PowerShell command that: === Got it 👍 — you want a single-line PowerShell command that: * Finds all files with "Snapchat-" in the name. * Extracts each file’s metadata date (from the Date column, not creation/modification date). * Renames the file so that "Snapchat-" becomes "IMG_<YYYYMMDD>_" while keeping the rest of the original name. Here’s the exact inline PowerShell command you can run in the folder: <syntaxhighlight lang="powershell">Get-ChildItem -File | Where-Object { $_.Name -like '''Snapchat-''' } | ForEach-Object { $date = (Get-ItemProperty $_.FullName -Name Date).Date.ToString('yyyyMMdd') $newName = $_.Name -replace 'Snapchat-', "IMG_$date<code>_" Rename-Item $_.FullName -NewName $newName } </syntaxhighlight> ===== - Get-ChildItem -File — lists all files in the current directory. ===== '' Where-Object { $_.Name -like '''Snapchat-*' } — filters only the files that contain “Snapchat-”. * (Get-ItemProperty $_.FullName -Name Date) — reads the metadata “Date” column (the one visible in File Explorer, which reflects content date for photos/videos). * .ToString('yyyyMMdd') — formats that date to YYYYMMDD. * -replace 'Snapchat-', "IMG_$date"</code>— replaces the “Snapchat-” prefix with<code>IMG_YYYYMMDD</code>. * Rename-Item — renames the file in place. ===== If you have: ===== <syntaxhighlight>Snapchat-12345.jpg (Date: 2023-07-14) Snapchat-98765.mp4 (Date: 2024-02-01) </syntaxhighlight> After running: <syntaxhighlight>IMG_20230714_12345.jpg IMG_20240201_98765.mp4 </syntaxhighlight> Would you like it to also search through subfolders recursively? If yes, I’ll tweak it to handle that too. == gpt-5-t-mini == === User: It's giving this error: === It's giving this error: Get-ItemProperty : The property Date does not exist or was not found. At line:2 char:14 + $date = (Get-ItemProperty $_.FullName -Name Date).Date.ToString(' ... + 05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)[[User:Lukegao|Lukegao]] ([[User talk:Lukegao|talk]]) 05:24, 10 November 2025 (UTC) + CategoryInfo : ReadError: (Date:String) [Get-ItemProperty], IOException + FullyQualifiedErrorId : GetValueError,Microsoft.PowerShell.Commands.GetItemPropertyCommand You cannot call a method on a null-valued expression. At line:2 char:5 + $date = (Get-ItemProperty $_.FullName -Name Date).Date.ToString(' ... + 05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC)05:24, 10 November 2025 (UTC) + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull Does Powershell doesn't have the capability of reading metadata of photo files? === Assistant: Good question — PowerShell can read photo metadata, but Get-ItemProperty -Name Date is not the right way to read EXIF /… === Good question — PowerShell can read photo metadata, but Get-ItemProperty -Name Date is not the right way to read EXIF / shell metadata. The Date property you tried to read doesn't exist as a filesystem property, so Get-ItemProperty returned $null and you hit the null-method error. Photos and videos store metadata in different places (EXIF tags for JPEGs, different shell properties for MP4/HEIC, etc.), so a robust approach tries a few common sources and falls back to a filesystem timestamp if metadata isn’t present. Below is a robust inline PowerShell snippet you can run in the folder. It: * Finds files with Snapchat- in the name. * Attempts to read System.Photo.DateTaken (EXIF Date Taken) via the Shell COM ExtendedProperty. * Falls back to System.ItemDate/System.DateModified or the file LastWriteTime if needed. * Renames Snapchat- → IMG_<YYYYMMDD>_ keeping the rest of the filename. Run this (you can paste it as a single block into PowerShell): <syntaxhighlight lang="powershell">Get-ChildItem -File | Where-Object { $_.Name -like '''Snapchat-''' } | ForEach-Object { # try to get EXIF / shell metadata first, otherwise file timestamp $dt = $null try { $shell = New-Object -ComObject Shell.Application $folder = $shell.Namespace($_.DirectoryName) $item = $folder.ParseName($_.Name) # try common metadata properties $candidates = @( "System.Photo.DateTaken", "System.Media.DateEncoded", "System.ItemDate", "System.DateModified", "Date taken", "Date" ) foreach ($prop in $candidates) { if (-not $dt) { $val = $null try { $val = $item.ExtendedProperty($prop) } catch {} if ($val -and $val -ne '') { # Some values include time; attempt to parse if ($val -is [datetime]) { $dt = $val } else { try { $dt = [datetime]::Parse($val) } catch {} } } } } } catch { $dt = $null } if (-not $dt) { $dt = $_.LastWriteTime } # final fallback $datestr = $dt.ToString('yyyyMMdd') $newName = $_.Name -replace 'Snapchat-', "IMG_${datestr}_" Rename-Item -LiteralPath $_.FullName -NewName $newName } </syntaxhighlight> Notes & tips * This covers most JPGs/HEICs and many video formats, but some files may use different metadata fields (camera apps vary). In those rare cases the script will fall back to the file’s last write time. * If you want it to run recursively through subfolders, add -Recurse to Get-ChildItem. * If filenames can contain special characters, I used -LiteralPath in Rename-Item to be safe. * If you prefer a one-liner (no newlines), you can collapse it, but readability suffers — I kept it formatted for clarity. Try that and tell me if any files still keep the old name — paste one example filename and I’ll show why it didn’t pick up a date. === User: These are .jpg files and date taken is not present in the metadata of those files. === These are .jpg files and date taken is not present in the metadata of those files. So can you try Date or Content created instead? === Assistant: Got it — thanks for the clarification. === Got it — thanks for the clarification. Below is a single-line PowerShell command you can paste into the folder. It will: * Look for files with Snapchat- in the name. * Try several shell/EXIF properties in this order: System.Photo.DateTaken, Date, Content created, System.DateCreated, System.FileSystem.DateCreated, System.ItemDate, System.DateModified. * If none of those are present, it falls back to the file system CreationTime. * Renames Snapchat- → IMG_<YYYYMMDD>_ and keeps the rest of the name. One-liner (paste as-is): <syntaxhighlight lang="powershell">Get-ChildItem -File | Where-Object { $_.Name -like '''Snapchat-''' } | ForEach-Object { $dt=$null; try { $sh=New-Object -ComObject Shell.Application; $f=$sh.Namespace($_.DirectoryName); $i=$f.ParseName($_.Name); $props=@("System.Photo.DateTaken","Date","Content created","System.DateCreated","System.FileSystem.DateCreated","System.ItemDate","System.DateModified"); foreach($p in $props){ if(-not $dt){ $v=$null; try{ $v=$i.ExtendedProperty($p) } catch{}; if($v -and $v -ne ''){ if($v -is [datetime]){ $dt=$v } else { try{ $dt=[datetime]::Parse($v) } catch{} } } } } } catch{}; if(-not $dt){ $dt=$_.CreationTime }; $datestr=$dt.ToString('yyyyMMdd'); $newName = $_.Name -replace 'Snapchat-', "IMG_${datestr}_"; Rename-Item -LiteralPath $_.FullName -NewName $newName } </syntaxhighlight> Notes * If you want to process subfolders, add -Recurse to Get-ChildItem. * CreationTime is used as the final fallback (this is the typical "Content created" filesystem value). If you prefer LastWriteTime instead, I can switch the fallback. * If any file still doesn't pick up the expected date, paste one example filename (and show dir /T:C "filename.jpg" output if possible) and I’ll diagnose why that file lacked the metadata property. Want me to return a version that runs recursively and logs original → new names to a CSV? == References == <references /> [[Category:ChatGPT Shares]] [[Category:openai]]
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Cite web
(
edit
)
Module:Citation/CS1
(
edit
)
Module:Citation/CS1/COinS
(
edit
)
Module:Citation/CS1/Configuration
(
edit
)
Module:Citation/CS1/Date validation
(
edit
)
Module:Citation/CS1/Identifiers
(
edit
)
Module:Citation/CS1/Utilities
(
edit
)
Module:Citation/CS1/Whitelist
(
edit
)
Module:Citation/CS1/styles.css
(
edit
)