Skip to content

Instantly share code, notes, and snippets.

@darallium
Last active September 1, 2024 12:18
Show Gist options
  • Select an option

  • Save darallium/32c6cca1492f36f6cb9278ab2aef71d4 to your computer and use it in GitHub Desktop.

Select an option

Save darallium/32c6cca1492f36f6cb9278ab2aef71d4 to your computer and use it in GitHub Desktop.
ファイルを整理するパワーシェルスクリプト ↑2行を良い感じに書き換えてください
$sourceFolder = "D:\path\from\copy"
$destinationFolder = "C:\path\to\extract"
$files = Get-ChildItem -Path $sourceFolder -Recurse -File
Write-Output "$files"
foreach ($file in $files) {
$Date = $file.CreationTime
$year = $Date.Year
$month = $Date.ToString("MM")
$relativePath = $file.FullName.Substring($sourceFolder.Length).TrimStart("\\")
$parentFolder = Split-Path -Parent $relativePath
$targetFolder = Join-Path -Path $destinationFolder -ChildPath "$year\$month\$parentFolder"
Write-Output "$year/$month/$File"
if (-not (Test-Path -Path $targetFolder)) {
New-Item -Path $targetFolder -ItemType Directory | Out-Null
}
$targetPath = Join-Path -Path $targetFolder -ChildPath $file.Name
Copy-Item -Path $file.FullName -Destination $targetPath -Force
}
Write-Output "ファイルの分類とコピーが完了しました。"
$sourceFolder = "D:\path\from\copy"
$destinationFolder = "C:\path\to\extract"
$files = Get-ChildItem -Path $sourceFolder -Recurse -File
Write-Output "$files"
foreach ($file in $files) {
$Date = $file.LastWriteTime
$year = $Date.Year
$month = $Date.ToString("MM")
$relativePath = $file.FullName.Substring($sourceFolder.Length).TrimStart("\\")
$parentFolder = Split-Path -Parent $relativePath
$targetFolder = Join-Path -Path $destinationFolder -ChildPath "$year\$month\$parentFolder"
Write-Output "$year/$month/$File"
if (-not (Test-Path -Path $targetFolder)) {
New-Item -Path $targetFolder -ItemType Directory | Out-Null
}
$targetPath = Join-Path -Path $targetFolder -ChildPath $file.Name
Copy-Item -Path $file.FullName -Destination $targetPath -Force
}
Write-Output "ファイルの分類とコピーが完了しました。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment