Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 用于从文件中的名称列表创建文件夹的Powershell脚本_Windows_Powershell_Scripting_Directory_Copy Item - Fatal编程技术网

Windows 用于从文件中的名称列表创建文件夹的Powershell脚本

Windows 用于从文件中的名称列表创建文件夹的Powershell脚本,windows,powershell,scripting,directory,copy-item,Windows,Powershell,Scripting,Directory,Copy Item,我有一个文本文件,其中包含不同目录中的文件列表,例如 C:\FolderOne\Testing.jpg C:\FolderTwo\Test.jpg 我想把这些名字变成文件夹。如果一个文件夹中存在两个文件,例如FolderOne,则这两个文件应放在同一个文件夹中 我知道反斜杠也必须用下划线替换,冒号也必须用下划线替换 到目前为止,我能够从文件列表中获取内容并将其复制到指定的文件夹中。我想为文件列表中的每个文件创建一个文件夹,但文件夹名称需要包括C_FolderOne和C_FolderTwo等等

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

  • C:\FolderOne\Testing.jpg
  • C:\FolderTwo\Test.jpg
我想把这些名字变成文件夹。如果一个文件夹中存在两个文件,例如
FolderOne
,则这两个文件应放在同一个文件夹中

我知道反斜杠也必须用下划线替换,冒号也必须用下划线替换

到目前为止,我能够从文件列表中获取内容并将其复制到指定的文件夹中。我想为文件列表中的每个文件创建一个文件夹,但文件夹名称需要包括
C_FolderOne
C_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
}

你的问题不清楚。您到底想做什么?在“工作”文件夹下创建的路径(例如-“C:\Testing”)是否包含多个段(例如-C:\folder A\folder A-Sub folder 0\…\filename.jpg)?@TheIncorrigible1基本上,尝试为文件列表中的每个文件创建一个文件夹。创建的文件夹应命名为该文件夹的路径,例如,如果名为“Winter.jpg”的文件位于“C:\TestFolder\Winter.jpg”,则应创建名为“C_TestFolder”的文件夹。@codemaker您好,是的,在“work”下创建的路径“文件夹可以包含多个段。
(分割路径-路径$Path-父)-替换“:\”,“\”
@theman behindtheman正在制定逻辑来捕获剩余的“\\”。我欠你鸡翅,我的朋友或任何你喜欢的lol。经过一些调整,它完全符合我的要求!非常感谢。@Theman后面的人太棒了!请投票/标记答案。需要做哪些调整?我必须添加一个if语句,以便在文件夹不存在时创建它。然而,我已经向你投了更高的票,因为我的声誉还不到15,因为我只加入了,我收到一条消息,说它已经被记录,但不会改变公开显示的分数。亲切问候。@Theman如果您将
-Force
添加到
新项目
,它将自动创建文件夹结构。