首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脚本适用于一个目录路径,但不用于多个目录路径

脚本适用于一个目录路径,但不用于多个目录路径
EN

Stack Overflow用户
提问于 2015-09-19 19:44:37
回答 1查看 181关注 0票数 0

我在试着

  1. 在每个CD_TMP目录中创建一个WE*.MS文件
  2. 通过处理AHD*.TPL和ADT*.TPL文件设置内容
  3. 将AHD*.TPL重命名为AHD*.TPL.Done,将ADT*.TPL重命名为AHD*.TPL.Done。

当只有一个WE.20150408.MS目录时,脚本可以正常工作,但是当有多个目录(即WE.20150408.MS、WE.20151416.MS、WE.20140902.MS)时,它不能工作,并给出错误消息:

代码语言:javascript
复制
Get-Content: An object at specified path AHD*TPL does not exist of has been filtered by the -Include or -Exclude parameter.
At C:\Temp\Script\Script.ps1:24 Char:14
+ $content = Get=Content -path $AHD
+ CatagoryInfo  :ObjectNotFound: (System.String[]:Strint[1) [Get-Content], Exception
+ FullyQualifiedErrorID:    ItemNotFound,Micorsoft.Powershell.Commands.GetContentCommand

剧本:

代码语言:javascript
复制
$SOURCE_DIR = "C:\Work"
$Work_DIR = "WE*MS"
$WE_DIR = "$SOURCE_DIR\$Work_DIR"
$AHD = "AHD*TPL"
$ADT = "ADT*TPL"
$AHD_FILES = $SOURCE_DIR
$CD_TMP = "CD_TMP"
$Str1 = "TEMP"
##############
Set-Location $WE_DIR
New-Item -Path "CD_TMP" -type file  -force 
#############           
foreach ( $File in ( get-childitem -name $WE_DIR))
                {

        $content = Get-Content -path $AHD
           $content | foreach {

            If ($_.substring(0,4) -NotLike $Str1)
            {
            '0011' + '|' + 'HD' + '|' + 'AHD' + $_
             }
        } | Set-Content $CD_TMP
}

Get-ChildItem AHD*.TPL| ForEach {Move-Item $_ ($_.Name -replace ".TPL$",
".TPL.Done")}
##############
foreach ( $File in ( get-childitem -name $WE_DIR))
                {
        $content = Get-Content -path $ADT
           $content | foreach {

            If ($_.substring(0,4) -NotLike $Str1)
            {
            '0022' + '|' + 'DT' + '|' + 'ADT' + $_
             }
        } | Set-Content $CD_TMP
}

Get-ChildItem ADT*TPL| ForEach {Move-Item $_ ($_.Name -replace ".TPL$",
".TPL.Done")}

PAUSE
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-19 23:03:58

它是否首先给出了错误Set-Location : Cannot set the location because path 'C:\Work\WE*MS' resolved to multiple containers.?当它失败的时候,我希望它能这样说。

然后,因为它不能更改为文件夹,它找不到任何AHD文件。

它对一个文件夹正常工作吗?它为AHD文件编写CD_TMP文件,然后为ADT文件覆盖它。这似乎不对。

此外,您还可以通过更改以下内容来使其更直接:

  • 在$CAPITAL变量中添加很多东西,然后使用它们一次,否则永远不会使用它们。
  • 使用.substring() -notlike测试使用.startswith()
  • 将++++的字符串构建成单个字符串
  • 重命名为具有-NewName脚本块的重命名项。

我在想:

代码语言:javascript
复制
$folders = Get-ChildItem "C:\Work\WE*MS" -Directory

foreach ($folder in $folders) {

    # AHD files    
    $content = Get-Content "$folder\AHD*.TPL"
    $content = $content | where { -not $_.StartsWith('TEMP') } 
    $content | foreach {"0011|HD|AHD$_"} | Set-Content "$folder\CD_TMP" -Force

    Get-ChildItem "$folder\AHD*.TPL" | Rename-Item -NewName {$_.Name + '.Done'}

    # ADT files
    $content = Get-Content "$folder\ADT*.TPL"
    $content = $content | where { -not $_.StartsWith('TEMP') } 
    $content | foreach {"0011|HD|ADT$_"} | Add-Content "$folder\CD_TMP"

    Get-ChildItem "$folder\ADT*.TPL" | Rename-Item -NewName {$_.Name + '.Done'}

}

虽然我不知道输入或输出应该是什么,所以我不能测试它。注意:它现在执行Add-Content来附加到CD_TMP文件,而不是覆盖它。

$content中仍然有很多冗余,但是这些行大多是这样独立的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32672497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档