iT邦幫忙

0

PowerShell Move-Item檔案移動問題

Rax 2018-10-22 15:44:494165 瀏覽
  • 分享至 

  • xImage

我的目的是要將A資料夾裡的檔案轉移到B資料夾,目前已完成前述的腳本了,
但想請問各位前輩,我要怎麼設置例外,例如:A資料夾裡的TEST子目錄不要被移過去B的話,這段該怎麼寫呢?

$SourceDir = "C:\A"
$DestinationDir = "D:\B"
 
$files = get-childitem $SourceDir *
foreach ($file in $files) 
{
$Directory = $DestinationDir + "\" + $file.CreationTime.Date.ToString('yyyyMMddd')
if (!(Test-Path $Directory))
	{
	New-Item $directory -type directory
	}
	Move-Item $file.fullname $Directory
}
q00153 iT邦新手 3 級 ‧ 2018-10-22 15:58:49 檢舉
不知道是什麼原因要寫 PowerShell
如果不是為了學習,是實務上的需求
建議使用 robocopy 即可

參考
http://lordlyson.blogspot.com/2015/08/robocopy.html
https://tacoclement.blogspot.com/2017/09/robocopy.html
Rax iT邦新手 5 級 ‧ 2018-10-22 16:16:00 檢舉
是實務上的需求唷,其實已經使用robocopy寫出來了,但是還是想知道PowerShell的寫法,謝謝你!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
runan5678
iT邦研究生 1 級 ‧ 2018-10-22 17:18:45
最佳解答

foreach迴圈裡面再塞個條件判斷式

$file.Name -neq "TEST" 

再執行你要達成的工作目標即可

至於為什麼是$file.Name,可以在foreach這個迴圈內把$file印出來就清楚了

看更多先前的回應...收起先前的回應...
comhlp iT邦新手 4 級 ‧ 2018-10-22 17:29:33 檢舉

$files = get-childitem $SourceDir *

改成

$files = get-childitem $SourceDir | where name -ne "test"

Rax iT邦新手 5 級 ‧ 2018-10-23 17:18:11 檢舉

感謝解答!成功了,
另外想請教,如果我要將一個以上的目錄或檔案做排除該怎麼下語法呢?

runan5678 iT邦研究生 1 級 ‧ 2018-10-24 08:55:48 檢舉

如果是comhlp的方法 where 後面可以用 and 或是 -like的方式處理

Rax iT邦新手 5 級 ‧ 2018-10-26 09:08:15 檢舉

感謝回答!!

我要發表回答

立即登入回答