Saturday, 22 July 2017

Copy files and change name with PowerShell

Copy files and change name with PowerShell




A friend of mine asked if I could copy files from one folder to another and rename them according to the following format

YYYYMMDDHHMMSS.ext

here  source code:


 cls 
$source="C: ext72source"
$destination="C: ext72destination"
set-location $source
$files=get-childitem -Recurse
foreach ($file in $files)
{
if (!$file.PsIsContainer)
{
$newName=$destination+""+[string]$file.LastWriteTime.Year +
[string]$file.LastWriteTime.Month +
[string]$file.LastWriteTime.Month +
[string]$file.LastWriteTime.Day +
[string]$file.LastWriteTime.Hour +
[string]$file.LastWriteTime.Minute +
[string]$file.LastWriteTime.Second +
[string]$file.Extension
Copy-Item $file.FullName $newName
}
}

Then change source and destination variable in the script.
download
alternative link download

Like the Post? Do share with your Friends.