我的任务是使用Windows Batch或PowerShell脚本创建存档过程。我在StackExchange上看到了一些例子,但没有任何东西可以完全满足我的需要而且我遇到了一些问题。
这是背景: 我有3个文件夹
我们的主系统将xml文件放入Incoming目录中,我必须创建一个每5分钟运行一次的脚本并执行以下操作....
当然,如果任何阶段失败,那么它不应该转到下一个阶段,因为我们不想删除那些尚未正确存档的文件。
我看过Move-Item等的文件夹迭代,但我遇到了很多问题。这真的不是我的主要工作领域,所以任何帮助都会受到赞赏。
由于
- 编辑 -
这是我创建的PowerShell脚本:
$SCRIPT_DIRECTORY = "d:\Temp\archive\"
$IPS_OUTPUT_DIRECTORY = "d:\Temp\archive\one\"
$ARCHIVE_DIRECTORY = "d:\Temp\archive\two\"
$BIZTALK_INCOMING_DIRECTORY = "d:\Temp\archive\three\"
$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"
}
以下是执行的截图:(好的,我没有足够的重复点来添加屏幕截图,所以这里是输出的txt转储)...
PS D:\Temp\archive> .\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:\Temp\archive>
在这个脚本中唯一可行的是文件夹'one'中的.xml文件被正确地重命名为.ready_to_archive',但没有其他事情发生。
答案 0 :(得分:0)
错误来自Remove-Item $IPS_OUTPUT_DIRECTORY + "*.ready_to_archive" -force
使用以下代码:Remove-Item -Path "$($IPS_OUTPUT_DIRECTORY)*.ready_to_archive" -force
。