Skip to content

Instantly share code, notes, and snippets.

@gioxx
Created Aug 6, 2020
Embed
What would you like to do?
Funzione di riorganizzazione dei file in base al nome del documento. Se il nome del file contiene nelle prime posizioni l'anno e il mese io posso utilizzarlo per riorganizzare tutto in cartella, vedi l'articolo sul blog: https://wp.me/pdQ5q-dlE
# Credits: https://stackoverflow.com/a/52605580/2220346
function DataReorg {
$files = Get-ChildItem $args[0] -Recurse | where {!$_.PsIsContainer}
$targetPath = $args[1]
foreach ($file in $files)
{
$bn = $file.basename.ToString()
$year = $bn.substring(0,4)
$month = $bn.substring(4,2)
$Directory = $targetPath + "\" + $year + "\" + $month
if (!(Test-Path $Directory)) { New-Item $directory -type directory }
$file | Move-Item -Destination $Directory
}
}
DataReorg 'C:\Folder1\*.csv' 'C:\Folder1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment