0

I have a script that renames all the files in a directory from fields/columns after importing a .CSV. My problem is that PS is renaming the files asynchronously and not synchronously. Is there a better way to accomplish get the result I want?

Current file name = 123456789.pdf

New File Name = $documentID_$fileID

I need to new file name to rename the files in order to make the script viable.

Here's my code (I'm new at this):

$csvPath = "C:\Users\dougadmin28\Desktop\Node Modify File Name App\test.csv"

$filePath = "C:\Users\dougadmin28\Desktop\Node Modify File Name App\pdfs"

$csv = Import-Csv $csvPath Select-Object -Skip 0 

$files = Get-ChildItem $filePath

    foreach ($item in $csv) {
        foreach($file in $files) {
            Rename-Item $file.fullname -NewName "$($item.DocumentID +"_"+ ($item.FileID)+($file.extension))" -Verbose
        }
    }
  • Before Windows 10 version 1903, the Windows filesystem APIs were inherently asynchronous, as described in [this answer](https://stackoverflow.com/a/53561052/45375). Is this the problem you're experiencing? What problem, specifically, are you experiencing? (Please update _your question_ with clarifications, don't respond in any comment.) – mklement0 Sep 16 '19 at 21:43
  • 1
    as an aside, this >>> `$csv = Import-Csv $csvPath Select-Object -Skip 0` <<< is not valid code. you are missing a '|' to send the import to the `Select-Object` cmdlet AND there is no point in doing `-Skip 0` ... [*grin*] – Lee_Dailey Sep 17 '19 at 02:24
  • In my test CSV I have 14 unique document numbers and fileIDs that need to become part of the naming convention of 14 pdfs that are unique. I've marked a few of the pdfs with a stamp to see if the file I wanted to be renamed "123456_f22q" was being renamed correctly. The problem is that the files are seemingly being renamed out of order so the docuementID and the fileID's are being added ambiguously to the files I need to be renamed. If the first pdf in the directory needs to be renamed with the value of the documentID column of my csv file. Thank you for your help! – Douglas Henderson Sep 17 '19 at 13:01

2 Answers2

0

you may try using workflows, which would allow you to execute tasks in parallel: https://learn.microsoft.com/en-us/powershell/module/psworkflow/about/about_foreach-parallel?view=powershell-5.1

Have in mind that PowerShell Workflows, have some limitations: https://devblogs.microsoft.com/scripting/powershell-workflows-restrictions/

Hope it helps!

Ivan Mirchev
  • 829
  • 5
  • 8
0

I thought synchronous meant sequentially as in 'one after the other', Which is what your script is doing now.

If you mean to say 'in parallel' as in Asynchronously or independent of each other, you can look at using

  1. Background Jobs. They include Start-Job, wait-job and receive-job. Easiest to work with but not efficient in terms to performance. It is also available in some cmdlets as an -AsJob switch.
  2. PowerShell Runspaces. [Most efficient but hard to code for]
  3. PowerShell Workflows [Balanced but has limitations]
Sid
  • 2,586
  • 1
  • 11
  • 22