iT邦幫忙

DAY 29
1

強而有力的 Windows PowerShell系列 第 29

使用 Windows PowerShell 建立與移除檔案和資料夾

在 Windows PowerShell 裡,要怎麼建立或移除檔案和資料夾呢?看下去,便知道!

強而有力的 Windows PowerShell 系列文章列表如下:
http://ithelp.ithome.com.tw/event/ironmanarticle2/id/20005121
對 Windows PowerShell 熟悉之後,您就會發現在 Windows PowerShell 裡,要建立新項目(不論是檔案、物件...等)的做法幾乎都是套用同樣的模式。在此先提醒大家,要進行建立或複製檔案與資料夾的操作,要有權限才行哦!

例如,下面的命令會使用 New-Item cmdlet 在使用者的暫時目錄裡,建立一個新的資料夾 New Folder,以及在這個新建立的 New Folder 資料夾中,產生新的空檔案 file.txt:

New-Item -Path "$env:Temp\New Folder" -ItemType "directory"
New-Item -Path "$env:Temp\New Folder\file.txt" -ItemType "file"

請注意,因為新資料夾 New Folder 的名稱有空格,所以必須要用一對雙引號。

使用 Remove-Item 可以用來移除項目,如果項目中還包含其他的項目,便會提示您確認是否要全部移除。例如,下面的程式碼會刪除內有其他項目的 C:\temp\DeleteMe 資料夾,Windows PowerShell 會在刪除資料夾之前,顯示確認提示:

Remove-Item "$env:Temp\New Folder"

如果不想看到這樣的確認提示,可以加上 Recurse 參數:

Remove-Item "$env:Temp\New Folder" -Recurse

上一篇
使用 Windows PowerShell 列出檔案與資料夾的資訊
下一篇
使用 Windows PowerShell 複製檔案和資料夾
系列文
強而有力的 Windows PowerShell33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言