簡體   English   中英

Powershell腳本從文件名稱列表創建文件夾

[英]Powershell script to create folders from list of names in a file

我有一個文本文件,其中包含不同目錄中文件的列表,例如

  • C:\\FolderOne\\Testing.jpg
  • C:\\FolderTwo\\Test.jpg

我想把那些名字變成文件夾。 而且,如果一個文件夾中存在兩個文件,例如FolderOne ,則這兩個文件都應放入同一文件夾中。

我知道反斜杠也必須用下划線代替,而冒號也必須用下划線代替。

到目前為止,我已經能夠從文件列表中獲取內容並將其復制到指定的文件夾中。 我想為文件列表中的每個文件創建一個文件夾,但是文件夾名稱需要包括C_FolderOneC_FolderTwo等。

任何幫助,將不勝感激!

到目前為止,我有:

Param (
    [String]$model
)

# Debug line
$model = "DEV2";

[String]$drive = "C:";

#Go to the PG_dump directory
CD "C:\Program Files\PostgreSQL\9.4\bin"

##Execute PG_dump with no password
.\pg_dump.exe --host localhost --port 5432 --username "postgres" --role "postgres" --no-password  --format custom --blobs --verbose --file     "C:\Test\$date-DEV-$X.backup" "DEV"

#Get the content from the file list
$file_list = Get-Content "$drive\Testing\DEV33.txt" 
foreach ($file in $file_list)
{    
    Copy-Item  $file -Destination "C:\Testing"
}

這是一種執行您要問的方法。 它將獲取文件的父路徑並創建目標文件夾。

## Get the content from the file list.
foreach ($file in Get-Content -Path "$drive\Testing\DEV33.txt") {
    $name = (Split-Path -Path $file -Parent) -replace '\\', '_' -replace ':'
    New-Item -Path "path\to\$name" -ItemType Directory
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM