0

I am new to Powershell and I am having an issue within a loop that I need assistance with. I am attempting to rename some files that are created as part of the process within the loop. I have tested the code OUTSIDE of the loop and it works fine. However, when I try to put it in the loop, nothing seems to happen.

The files I need to rename are in the following locations… (1)“\MYSERVER\MYCOMPANY\MYFOLDER\ MyPrintouts\EstimateImport\ImportPrintout.txt” (2)“\MYSERVER\MYCOMPANY\MYFOLDER\ MyPrintouts\PostEntries\ImportPostEntries.txt”

I need to tack the date and time on the end. This code works for me OUTSIDE of the loop. I put it in a file I named RenameFiles.ps1

#File location
$ImportPrintout = “\\MYSERVER\MYCOMPANY\MYFOLDER\MyPrintouts\EstimateImport\ImportPrintout.txt”
$ImportPostEntries = “\MYSERVER\MYCOMPANY\MYFOLDER\ MyPrintouts\PostEntries\ImportPostEntries.txt”

#Find and rename the import printouts
Get-ChildItem $ImportPrintout -Filter "ImportPrintout.txt" | ForEach-Object {          
            Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')$(Get-Date -Format "MMddyyyy-HHmmss").txt"}

Get-ChildItem $ImportPostEntries -Filter "ImportPostEntires.txt" | ForEach-Object {          
            Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')$(Get-Date -Format "MMddyyyy-HHmmss").txt"}

This is how I added it to the loop as I want the files renamed BEFORE the next file is processed…

#Define actions after an Event is Detected
$action = {$files = Get-ChildItem -Path $watcher.Path -Filter $watcher.Filter #-Recurse
        foreach ($file in $files)
        {
        #Define variables for log file
        $changeType = $Event.SourceEventArgs.ChangeType #Your event trigger "Created"
        $fileDate = $file.LastWriteTime #Date/Time Estimate was created
        #logline contains = Date/Time of import, Created, Date/Time Created, filename
        $logline = "$(Get-Date), $changeType, $fileDate, $file"

        #Actions to take ==============================================================
        #Write details to the log
        Add-Content "“\\MYSERVER\MYCOMPANY\MYFOLDER\EstimateImportLog.txt" -value $logline
        #Copy the estimate to the "ToBeProcessed" folder
        Copy-Item $file.FullName -Destination $copyTo
        #Move the estimate to the "EstimateHistory" folder
        Move-Item $file.FullName -Destination $moveTo -Force
        #Run the powershell script that launches the .bat file that launches the macro
        Invoke-Expression "& '\\MYSERVER\MYCOMPANY\MYFOLDER\PSscriptToRunBATfile.ps1'"
        #Pause the script for 30 seconds to allow the estimate to finish posting
        Start-Sleep -Seconds 30
        Invoke-Expression "& '“\\MYSERVER\MYCOMPANY\MYFOLDER\RenameFiles.ps1'"
        }
        }

This seems to “break” my loop. However, I need this to be done BEFORE going to the next file. How can I accomplish renaming these files before moving on. Any assistance would be greatly appreciated. Thank you!

mdgaw
  • 69
  • 7
  • 1
    The two code segments have no relationship to each other. I don't see any rename-item's in the second code section. Are you using copy-item/move-item to rename? If so then where are setting $copyTo and $moveTo? What, exactly, is breaking in your loop? Is it only executing once then stopping? – Arluin Sep 22 '16 at 20:49
  • The first piece of code I put in a script file and named it RenameFiles.ps1. I then call that file in the last line of my loop. However, the files do not get renamed and if there is more than one file in the folder when the $action begins, it does not seem to proceed to the next file. If I comment that line out, the $action loops to the next file as intended. If I run RenameFiles.ps1 independent of the loop it does what I want. Do you have a suggestion on how I might do this differently to rename the printouts before going to the next file? Thanks! – mdgaw Sep 22 '16 at 22:10

1 Answers1

0

As far as the loop failing, you're probably encountering an error. Either set your $ErrorActionPreference to Continue or set it to Stop and wrap try/catch blocks around your copy-item and move-item to detect and handle errors. That probably also addresses the failure of the copy-item/move-item to change the file name, it's running into an error trying to perform that action and failing.

Arluin
  • 594
  • 1
  • 8
  • 21
  • As had indicated, I am new to Powershell. Therefore, I may have learned a very valuable lesson as I had not changed anything in the code from yesterday to today. However, today the code works! Yeah and Ugh! – mdgaw Sep 23 '16 at 17:47
  • What I did different today is that I unregistered my event and then started the script again. Before, I would just make the change and save it. I am not sure why that changes anything....but it is working today! – mdgaw Sep 23 '16 at 17:48