M BUZZ CRAZE NEWS
// news

PowerShell script to move files in a new directory and rename them - how to avoid duplicate collision?

By Gabriel Cooper

The situation is as follows. I have a folder where files are dumped on a daily basis (mostly PDF). I am trying to write a script that does the following:

  1. Go through all the files in the folder and move them to the specified new folder.
  2. While the files are being moved, they need to be renamed to DMF_3400_file_ID, so they are always unique. I figured out that for the ID in the end, I will have the current date + randomly generated 4 digit number to make sure the names are always unique. After the renaming, the files in the destination folder should have a name like: DMF_3400_file_140920211587. This is what I did so far:
$FileLimit = 200
$PickupDirectory = Get-ChildItem -Path "C:\test\Src1" | Select -First $FileLimit
$DropDirectory = "C:\test\Dest1"
$curDateTime = Get-Date -Format ddMMyyyy
foreach ($file in $PickupDirectory){ $Destination = Join-Path $DropDirectory ($file.Name) $Num = Get-Random -maximum 9999 $file | Move-Item -Destination $Destination -PassThru -Verbose | Rename-Item -NewName {"DMF_3400_file_" + $curDateTime + $Num + $_.Extension } -Force -Verbose
}

The script works as I want it to, but the problem is that I still get name duplications from time to time, specially when I move a larger amount of files. My question is, how can I write the script better to avoid duplications at all times and make sure that all the files will be moved and always have a unique name? I am new to PowerShell, so any help will be greatly appreciated!

1 Answer

You can adapt your code by using an arraylist that will hold uniquely generated random numbers in a While loop. Then, a counter inside your ForEach loop to extract them while renaming the files.

You code will look like this:

$FileLimit = 200
#Create an arraylist that will hold the random numbers
$myArrayList = New-Object -TypeName "System.Collections.ArrayList"
While( $myArrayList.Count -le $FileLimit){ $num = Get-Random -maximum 9999 #Check if the arraylist doea not contain the random number If (!$myArrayList.contains($num)){ #Add the random number if not found in the arraylist [void]$myArrayList.Add($num) }
}
$PickupDirectory = Get-ChildItem -Path "C:\users\reddylutonadio" -File | Select -First $FileLimit
$DropDirectory = "C:\test\Dest1"
$curDateTime = Get-Date -Format ddMMyyyy
#Counter to extract the random numbers
$counter = 0
foreach ($file in $PickupDirectory){ $Destination = Join-Path $DropDirectory ($file.Name) $Num = Get-Random -maximum 9999 $file | Move-Item -Destination $Destination -PassThru | Rename-Item -NewName {"DMF_3400_file_" + $curDateTime + $myArrayList[$counter] + $_.Extension } $counter += 1
}
6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy