# Specify input and output directories
$inputDir = "C:\Path\To\Input\Directory"
$outputDir = "C:\Path\To\Output\Directory"
# Get all DICOM files in the specified directory
$dicomFiles = Get-ChildItem -Path $inputDir -Filter *
foreach ($file in $dicomFiles) {
# Use dcmdump to extract the DICOM tags
$tags = & "dcmdump.exe" $file.FullName | Select-String -Pattern "(0010,0020|0010,0010|05af,0010)"
# Extract values in between square brackets []
$tagValues = $tags -replace '[^\[\]]*\[([^\[\]]*)\][^\[\]]*', '$1'
# Ensure "0010,0020" is in the first position
$sortedTagValues = $tagValues | Sort-Object {$_ -eq '0010,0020'} -Descending
# Create a timestamp in the format YYYYMMDD_HHMMSS
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
# Create a new filename based on the extracted values and timestamp
$newFilename = "$($sortedTagValues -join '_')_$timestamp.dcm"
# Construct the full path for the output file
$outputPath = Join-Path -Path $outputDir -ChildPath $newFilename
# Read the content of the original file as bytes
$fileContent = [System.IO.File]::ReadAllBytes($file.FullName)
# Write the content to the new file in the output directory
[System.IO.File]::WriteAllBytes($outputPath, $fileContent)
Write-Host "Copied and renamed $($file.FullName) to $outputPath"
}
Without timestamp
# Specify input and output directories
$inputDir = "C:\Path\To\Input\Directory"
$outputDir = "C:\Path\To\Output\Directory"
# Get all DICOM files in the specified directory
$dicomFiles = Get-ChildItem -Path $inputDir -Filter *
foreach ($file in $dicomFiles) {
# Use dcmdump to extract the DICOM tags
$tags = & "dcmdump.exe" $file.FullName | Select-String -Pattern "(0010,0020|0010,0010|05af,0010)"
# Extract values in between square brackets []
$tagValues = $tags -replace '[^\[\]]*\[([^\[\]]*)\][^\[\]]*', '$1'
# Ensure "0010,0020" is in the first position
$sortedTagValues = $tagValues | Sort-Object {$_ -eq '0010,0020'} -Descending
# Create a new filename based on the extracted values
$newFilename = "$($sortedTagValues -join '_').dcm"
# Construct the full path for the output file
$outputPath = Join-Path -Path $outputDir -ChildPath $newFilename
# Read the content of the original file as bytes
$fileContent = [System.IO.File]::ReadAllBytes($file.FullName)
# Write the content to the new file in the output directory
[System.IO.File]::WriteAllBytes($outputPath, $fileContent)
Write-Host "Copied and renamed $($file.FullName) to $outputPath"
}
With timestamp
Without timestamp