0

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 1

0

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
  • thanks for the idea. I have already tried this and although it decreases the duplicates significantly, I still get them when moving 300 files for example. This also makes the names of the files too long and hard to read, which I want to avoid. I also tried to increase the max of the random number to 99999 for example, but it still didn't work as well as I hoped. I am thinking of something like checking, if the file name in destination already exists and if it does, add something until it's unique, but I can't manage to implement it in practice. Commented Sep 14, 2021 at 8:18
  • Checking if the file name already exists is a good idea. You can regenerate the random number and re-check, this will be in a loop and if the new file does not exist, exit the loop and continue with your current loop. OR you can generate all 300 random numbers and store them in an array/list before even moving the file. You will need to check that all of them are unique, then retrieve them when moving the files. Commented Sep 14, 2021 at 9:38
  • Any chance you could help me implement these changes? I tried, but my code ends up doing nothing at the end. I am pretty new to PowerShell and this seems out of my league at this point. Commented Sep 14, 2021 at 13:48
  • @imphetuss See edit. Commented Sep 14, 2021 at 18:19
  • @imphetuss Remove the -WhatIf in case you have not already done it. Commented Sep 15, 2021 at 12:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.