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:
- Go through all the files in the folder and move them to the specified new folder.
- 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!