Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been tasked with creating an archive process using either a Windows Batch or PowerShell script. I have seen a few examples here on StackExchange but nothing that does exactly what I need and I am running into some issues.

Here is the background: I have 3 folders

  • Incoming
  • Archive
  • Outgoing

Our main system puts xml files into the Incoming directory and I have to create a script that will run every 5 mins and do the following ....

  1. Iterate through all the xml files in the Incoming folder
  2. Rename them to OriginalFilename.ready_to_archive
  3. Copy all ready_to_archive_files into the Archive directory
  4. Once in the archive directory rename them back to OriginalFilename.xml
  5. Copy all ready_to_archive_files into the Outgoing directory
  6. Once in the Outgoing directory rename them back to OriginalFilename.xml
  7. Delete all ready_to_archive_files from the Incoming directory

Of course if any stage fails then it should not go to the next one since we do not want to delete files that have not been archived properly.

I have had a look at Folder iteration with Move-Item etc but I run into so many issues. This is really not my main working field so any help would be appreciated.

Thanks

-- EDIT --

Here is the PowerShell script I created:

$SCRIPT_DIRECTORY = "d:Temparchive"
$IPS_OUTPUT_DIRECTORY = "d:Temparchiveone"
$ARCHIVE_DIRECTORY = "d:Temparchive	wo"
$BIZTALK_INCOMING_DIRECTORY = "d:Temparchive	hree"
$ORIGINAL_EXTENSION = ".xml"
$PREP_EXTENSION = ".ready_for_archive"
$ARCHIVE_EXTENSION = ".archive"

#STEP 1
Try
{
"Attempting to rename file from .xml to .archive"       
Set-Location -Path $IPS_OUTPUT_DIRECTORY
Get-ChildItem *.xml | Rename-Item -NewName { $_.Name -replace $ORIGINAL_EXTENSION,$PREP_EXTENSION   
 }
}
Catch [system.exception]
{

$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
"Error in Step 1: $FailedItem : $ErrorMessage"
Set-Location -Path $SCRIPT_DIRECTORY    -ErrorAction Stop #Stop here and return to the script's
base directory - DO NOT CONTINUE!!!!!
Break
}
Finally
{
"end of step 1"

#STEP 2
Try
{
"Attempting to copy ready_to_archive file from One to Two"
Get-ChildItem -path $IPS_OUTPUT_DIRECTORY -recurse -include *.$PREP_EXTENSION |
Foreach-Object { Copy-Item -path $_ -destination { $_.FullName -replace
$IPS_OUTPUT_DIRECTORY,$ARCHIVE_DIRECTORY}}
 }
Catch [system.exception]
 {
  "caught an error in step 2"
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    "Error in Step 2: $FailedItem : $ErrorMessage"
    Set-Location -Path $SCRIPT_DIRECTORY    -ErrorAction Stop  #Stop here and return to the script's base directory - DO NOT CONTINUE!!!!!
    Break
 }
Finally
 {
  "end of step 2"

    #STEP 3

    Try
     {
        "Attempting to rename files from .ready_to_archive to .archive"     
        Set-Location -Path $ARCHIVE_DIRECTORY
        Get-ChildItem *.xml | Rename-Item -NewName { $_.Name -replace $PREP_EXTENSION,$ARCHIVE_EXTENSION }
     }
    Catch [system.exception]
     {

        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        "Error in Step 3: $FailedItem : $ErrorMessage"
        Set-Location -Path $SCRIPT_DIRECTORY    -ErrorAction Stop #Stop here and return to the script's base directory - DO NOT CONTINUE!!!!!
        Break

     }
    Finally
     {
        "end of step 3"

        #STEP 4
        Try
         {
          "Attempting to copy archive file from one to three"
            Copy-Item -path $IPS_OUTPUT_DIRECTORY -include "*.ready_to_archive" -Destination $BIZTALK_INCOMING_DIRECTORY
         }
        Catch [system.exception]
         {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            "Error in Step 4: $FailedItem : $ErrorMessage"
            Set-Location -Path $SCRIPT_DIRECTORY    -ErrorAction Stop
            Break
         }
        Finally
         {
          "end of step 4"

           #STEP 5  
           Try
             {
                "Attempting to rename file from .ready_to_archive to .xml"      
                Set-Location -Path $BIZTALK_INCOMING_DIRECTORY
                Get-ChildItem *.$PREP_EXTENSION | Rename-Item -NewName { $_.Name -replace $PREP_EXTENSION,$ORIGINAL_EXTENSION }
             }
            Catch [system.exception]
             {                  
                $ErrorMessage = $_.Exception.Message
                $FailedItem = $_.Exception.ItemName
                "Error in Step 5: $FailedItem : $ErrorMessage"
                Set-Location -Path $SCRIPT_DIRECTORY    -ErrorAction Stop
                Break

             }
            Finally
             {
                "end of step 5"

                #STEP 6
               Try
                 {
                  "Attempting to remove original file from One"
                    Remove-Item $IPS_OUTPUT_DIRECTORY + "*.ready_to_archive" -force #I need to specify the specific file not just any ready_to_archive file!!!!
                 }
                Catch [system.exception]
                 {
                    $ErrorMessage = $_.Exception.Message
                    $FailedItem = $_.Exception.ItemName
                    "Error in Step 6: $FailedItem : $ErrorMessage"
                    Set-Location -Path $SCRIPT_DIRECTORY    -ErrorAction Stop
                    Break
                 }
                Finally
                 {
                  "end of step 6"
                 } 
            }
        }
    }
 }
 "END OF SCRIPT"
}

And here is a screenshot of the execution: (Ok I do not have enough rep points to add a screenshot so here is a txt dump of the output) ...

PS D:Temparchive> .archive.ps1
Attempting to rename file from .xml to .archive
end of step 1
Attempting to copy ready_to_archive file from One to Two
end of step 2
Attempting to rename files from .ready_to_archive to .archive
end of step 3
Attempting to copy archive file from one to three
end of step 4
Attempting to rename file from .ready_to_archive to .xml
end of step 5
Attempting to remove original file from One
Error in Step 6:  : A positional parameter cannot be found that accepts argument '+'.
end of step 6
PS D:Temparchive>

The only thing that actually works in this script is that the .xml file in folder 'one' is properly renamed to .ready_to_archive' but nothing else happens.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

The error is from Remove-Item $IPS_OUTPUT_DIRECTORY + "*.ready_to_archive" -force.
Use this code: Remove-Item -Path "$($IPS_OUTPUT_DIRECTORY)*.ready_to_archive" -force.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...