zoukankan      html  css  js  c++  java
  • (PowerShell) 文件操作

    (PowerShell) 文件操作

    • Get current ps1 file's parent path
    $x = Split-Path -Parent $MyInvocation.MyCommand.Definition
    •  Create a folder if it does not exist.
    $myNewFolder = $x +"myTestFolder\"
    if(!(Test-Path $myNewFolder))
    {
    new-item -path $x -name myTestFolder -type directory
    }
    • Open a XML file , and get what we want.
    [String]$xmlDocPath = "F:/TestData/TestXml.xml"
    [String]$nodeName = "Property"
    
    $xmlDoc = New-Object "System.Xml.XmlDocument"
    $xmlDoc.Load($xmlDocPath)
    $nodelist = $xmlDoc.GetElementsByTagName($nodeName);
    foreach($node in $nodelist)
    { 
      # Use Attributes will throw error.
      #$namestr = $node.Attributes[0].InnerXml
      #Write-Host "$namestr" 
      $namestr = $node.GetAttribute("Value")
      Write-Host "$namestr" 
      $childnodes= $node.ChildNodes;
      foreach($childnode in $childnodes )
      {
        $textstr = $childnode.InnerXml.ToString()
        Write-Host "$textstr"
      }
    }

    Xml file

    <?xml version="1.0" encoding="gb2312"?>
    <Root>
    <Property Name = "First" Value ="value">
    <GUID>xxoxx</GUID>
    <Tel>123456</Tel>
    <Start>5.95</Start>
    </Property>
    <Property Name = "Second">
    <GUID>xxoxx</GUID>
    <Tel>123456</Tel>
    <Start>5.95</Start>a
    </Property>
    </Root>
    • Get special file from a directory , and copy them into local folder
    $fileName = "test123"
    $fileList = Get-ChildItem -path $LogServer -Filter "$fileName*"
    foreach($file in $fileList)
    {
    source = $LogServer+$file 
    Copy-Item $source $targetFolder
    Write-Host "$file"."is copied. "
    }

     获取指定文件夹下的所有文件夹大小

    $rootDirectory = "D:\TFS"
     
     
    $colItems = (Get-ChildItem $rootDirectory  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
    foreach ($i in $colItems)
        {
            $subFolderItems = (Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum)
            $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
        }
     
  • 相关阅读:
    MongoDB集群与LBS应用系列(一)
    Docker下构建centos7容器无法使用systemctl命令的解决办法
    重新诠释的OSGi规范
    交互细节中那些变形金刚
    UX黑名单!有哪些常见的移动端UX设计误区需要规避?
    那些门户网站的兼容问题(持续更新)
    Vmware虚拟机网络模式及虚拟机与物理机通信方法
    交互设计阅读(一)
    笑傲江湖:完美解决205:您所选择的线路不存在问题
    笑傲江湖 外网
  • 原文地址:https://www.cnblogs.com/fdyang/p/2890515.html
Copyright © 2011-2022 走看看