2012-07-10 50 views
1

我把它們一起粘在一起,但有點卡住了。代碼將項目放置在根目錄中,而不是它來自的文件夾,即如果它位於測試文件夾中,它將它直接放在用戶文件夾中,我希望它創建一個測試文件夾並將項目放在那裏。在它複製了這些項目之後,我希望它將該位置壓縮。有一次,我想我可以創建一個txt文件,然後使用它來複制項目,但後來我迷路了。PowerShell:複製文件,然後zip

$PCName = Read-Host "Enter Machine Name" 
$User = Read-Host "Enter User Name" 
$NAS = "\\<nas>\users\$user" 
$Dir = get-childitem \\$PCName\C$\users\$User -force -recurse | where { 
$_.extension -match "doc|xls|docx|xlsx|pdf|pst|txt|jpg|tif"} | ForEach { 
Copy-Item $_.FullName -Destination $NAS -force -ErrorAction:SilentlyContinue 
} 


#$Dir | ft fullname | out-file C:\Scripts\logs\FileList\$User.txt 
$Dir | format-table name# PowerShell script to list the DLL files under the system32 folder 

回答

0

你需要在目的地的路徑到達目的地的根目錄替換源根目錄:

$PCName = Read-Host "Enter Machine Name" 
$User = Read-Host "Enter User Name" 
$NAS = "\\<nas>\users\$user" 
$sourceRoot = "\\$PCName\C$\users\$User" 
$Dir = get-childitem $sourceRoot -force -recurse | 
    where { $_.extension -match "doc|xls|docx|xlsx|pdf|pst|txt|jpg|tif"} | 
    ForEach { 
     $destFile = $_.FullName.Replace($sourceRoot, $NAS) 
     New-Item -ItemType File -Path $destFile -Force 
     Copy-Item $_.FullName -Destination $destFile -force -ErrorAction:SilentlyContinue } 
+0

我收到了一個錯誤的路徑的一部分沒存在。我能夠使用這裏描述的觸摸方法:http://stackoverflow.com/a/7523632/1515942。上面和鏈接的組合給了我這剛剛複製品行上面New-Item -ItemType File -Path $ destFile -force – 2012-07-11 12:18:51