Powershell复制文件脚本

时间:2014-02-19 21:17:03

标签: powershell scripting

我正在尝试复制,并将多个文件重命名为miltiple文件夹。文件夹将像文件一样按顺序标记。例如,我需要复制Reports.txt将名称更改为Reports_NW01_20120219.txt到名为NW01的文件夹,然后将相同的过程更改为NW02-NW18。文件夹和文件名增加1.我是powershell的新手,所以请你好好保留。我粘贴了下面的内容。我试图读取日期,然后将其附加到文件名。我现在完全失去了。所以任何帮助都表示赞赏:

$date = read-host "Enter date of the report YYYYMMDD"
write-host $date  

for ($Counter = 1; $Counter -le 1; $Counter++) 
{     

    #Destination for files 
    $DropDirectory = "C:\Users\dh0520\Documents\Powershell Staging\"  
    #Current Location of the files
    $file = Get-ChildItem -Path "C:\Users\dh0520\Documents\Powershell Test\NW01\Reports.txt" 

    if ($Counter  -lt 10) {

        $Destination = $DropDirectory+'NW0' + $Counter  
        $file = Get-ChildItem -Path "C:\Users\dh0520\Documents\Powershell Test\NW0" + $Counter + "\Reports.txt" 
        Copy-Item $file.FullName ($Destination + $file.BaseName + "_NW0" + $Counter + "_" + $date +".txt")

    }

    If ($Counter -ge 10) { 

        $Destination = $DropDirectory+'NW' + $Counter 
        $file = Get-ChildItem -Path "C:\Users\dh0520\Documents\Powershell Test\NW" + $Counter + "\Reports.txt" 
        Copy-Item $file.FullName ($Destination + $file.BaseName + "_NW" + $Counter + "_" + $date +".txt")

    }   

}

1 个答案:

答案 0 :(得分:1)

我假设您在使用get-childItem时遇到错误。这是因为你无法建立这样的道路。试试这种方式:

$file = Get-ChildItem -Path "C:\test\NW0$($Counter)\Reports.txt"

这样:

$file = Get-ChildItem -Path "C:\test\NW$($Counter)\Reports.txt"