2014-02-19 169 views
0

我想复制,并将多个文件重命名为多个文件夹。这些文件夹将按照文件的顺序进行标记。例如我需要复制Reports.txt将名称更改为Reports_NW01_20120219.txt到一个名为NW01的文件夹,然后将NW02-NW18的过程相同。文件夹和文件名称增加1.我是PowerShell的新手,所以请放手。我粘贴了下面的内容。我试图读取日期,然后将其附加到文件名。我现在处于完全失落状态。所以任何帮助表示赞赏: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") 

    } 

} 
+0

有什么错误讯息/问题? – Raf

回答

1

我假设你得到错误的get-childItem。这是因为你不能建立这样的路径。试着这样说:

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

和这样:

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

一个整洁的解决方案。 –