敲下一段感悟 2019-08-23 15:40 采纳率: 100%
浏览 623
已采纳

有没有大神会powershell,可不可以将我的shell脚本改成powershell?

我现在要实现一个功能,要求使用powershell脚本实现。
要求:

1. 在不同的路径有两个目录例如:d:/aa 和d:/bb
2. aa目录要和bb目录进行一个遍历对比,相同的目录和相同的文件进行备份到d:/cc目录(按照同样的目录树结构),不同的目录和文件不做处理。这里需要注意的是,windos下的目录名和文件名可能中间有空格,也要进行备份。遍历时,这个会出现问题。主要是处理这个问题。

功能:

    实现遍历备份

shell脚本如下

#! /bin/bash
jendir=/aa
webdir=/bb
backdir=/cc
dir ()
{
for file1 in `ls $1`;do  
for file2 in `ls $2`;do
      if [ $file1 == $file2 ]
          then
                if [ -d "$1/$file1" ]
                     then
                         mkdir -p $3/$file1

                        dir $1"/"$file1 $2/$file1 $3/$file1
                else
                         cp  $2/$file2 $3
                fi
        fi
done
done
}
dir $jendir $webdir $backdir 
  • 写回答

4条回答 默认 最新

  • flashercs 2019-09-18 16:08
    关注

    $dir1 = "E:\FTP\1\aa"
    $dir2 = "E:\FTP\1\bb"
    $dirdst = "E:\FTP\1\cc"
    $ref = Get-ChildItem -LiteralPath $dir1 -Recurse | ForEach-Object { $_.FullName.Replace($dir1, '') }
    $dif = Get-ChildItem -LiteralPath $dir2 -Recurse | ForEach-Object { $_.FullName.Replace($dir2, '') }
    Compare-Object -ReferenceObject $ref -DifferenceObject $dif -ExcludeDifferent -IncludeEqual | ForEach-Object {
    $strRelPath = $_.InputObject
    if (Test-Path -LiteralPath "$dir2$strRelPath" -PathType Container) {
    New-Item -Path "$dirdst$strRelPath" -ItemType Directory | Out-Null
    }
    else {
    Copy-Item -LiteralPath "$dir2$strRelPath" -Destination "$dirdst$strRelPath"
    }
    }

    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集