如何读取内部文件值并创建目录,然后移动文件PowerShell?

如何读取内部文件值并创建目录,然后移动文件PowerShell?,powershell,Powershell,我需要读取一个文件,根据第一行中的值,将文件移动到值名称 例如,如果它显示type=item,请将该文件移动到我可能需要创建名为item的目录,然后移动到下一个文件 我如何才能完成这项任务?通过您的评论和原始帖子。至于这个 get-childitem "<SourceFolder>" -filter 7*.txt - recurse | select-string -list -pattern "test" | move -dest "<DestinationFolder>

我需要读取一个文件,根据第一行中的值,将文件移动到值名称

例如,如果它显示
type=item
,请将该文件移动到我可能需要创建名为
item
的目录,然后移动到下一个文件


我如何才能完成这项任务?

通过您的评论和原始帖子。至于这个

get-childitem "<SourceFolder>" -filter 7*.txt - recurse | select-string -list -pattern "test" | move -dest "<DestinationFolder>"
因此,基于以上内容,您可以看到,虽然您正在匹配字符串,但您并没有在管道中传递合法的完整文件名。因此,您可能希望采用不同的方法或使用现有的方法来清理结果,但要删除不需要的内容

根据Ansgar建议进行更新

这里有一个选择,因为,正如你所说,你是新的,一个走过去的过程。注意:始终有不同的选项/方法来执行此操作

# Get the target files
Get-ChildItem -Path 'd:\temp' -filter '*.txt' | Format-Table -AutoSize

# Results

    Directory: D:\temp

Mode          LastWriteTime Length Name                    
----          ------------- ------ ----                    
-a----   7/6/2018  10:16 PM     24 1 passwordchangelog.txt 
-a----   7/6/2018  10:16 PM     24 10 passwordchangelog.txt
-a----   7/6/2018  10:16 PM     24 11 passwordchangelog.txt
-a----   9/4/2018   5:20 PM     26 TestFile0.txt           
-a----   9/4/2018   5:20 PM     26 TestFile1.txt           
-a----   9/4/2018   5:20 PM     26 TestFile2.txt 




# Get the target files, matching the string filter
Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -List -Pattern 'test'

# Results

D:\temp\TestFile0.txt:1:test
D:\temp\TestFile1.txt:1:test
D:\temp\TestFile2.txt:1:test


# Get the target files, matching the string filter, clearing the meta data
(Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -list -pattern 'test') -replace (':\d.*')

# Results

D:\temp\TestFile0.txt
D:\temp\TestFile1.txt
D:\temp\TestFile2.txt


# Get the target files, matching the string filter, clearing the meta data, test direcory create and file action
(Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -list -pattern 'test') -replace (':\d.*') |
Move-Item -Destination (New-Item -Type Directory -Verbose -Force ('D:\temp\NewFiles') -WhatIf) -Verbose -WhatIf

# Results

Move-Item : Cannot process argument because the value of argument "destination" is null. Change the value of argument "destination" to a non-null value.
At line:3 char:1
+ Move-Item -dest (New-Item -Type Directory -Verbose -Force ('D:\temp\N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Move-Item], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.MoveItemCommand




# Get the target files, matching the string filter, clearing the meta data, force direcory create if it does not exist and execute the file action
(Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -List -Pattern 'test') -replace (':\d.*') |
Move-Item -Destination (New-Item -Type Directory -Verbose -Force ('D:\temp\NewFiles')) -Verbose -Force

# Results

VERBOSE: Performing the operation "Create Directory" on target "Destination: D:\temp\NewFiles".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\TestFile0.txt Destination: D:\temp\NewFiles\TestFile0.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\TestFile1.txt Destination: D:\temp\NewFiles\TestFile1.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\TestFile2.txt Destination: D:\temp\NewFiles\TestFile2.txt".


Get-ChildItem -Path 'D:\temp\NewFiles' | Format-Table -AutoSize

# Results

    Directory: D:\temp\NewFiles

Mode          LastWriteTime Length Name         
----          ------------- ------ ----         
-a----   9/4/2018   5:20 PM     26 TestFile0.txt
-a----   9/4/2018   5:20 PM     26 TestFile1.txt
-a----   9/4/2018   5:20 PM     26 TestFile2.txt

听起来是可行的。去吧。如果我不是新来powershell的,我会的;)我们不为您编写代码。请复习以帮助你提出一个好问题,从而得到一个好答案。我很确定这接近我需要的,但我想做的不是搜索字符串,而是读取类型(type=)的变量并创建,将该文件移动到value目录,如item。获取childitem“”-filter 7*.txt-recurse | select string-list-pattern“test”| move-dest“”请不要将这样的代码放在注释中。编辑您的问题,以格式化的方式添加代码。您可能希望添加(代码)建议他们应该做什么。像
。|选择字符串…|选择对象-展开文件名|…
# Get the target files
Get-ChildItem -Path 'd:\temp' -filter '*.txt' | Format-Table -AutoSize

# Results

    Directory: D:\temp

Mode          LastWriteTime Length Name                    
----          ------------- ------ ----                    
-a----   7/6/2018  10:16 PM     24 1 passwordchangelog.txt 
-a----   7/6/2018  10:16 PM     24 10 passwordchangelog.txt
-a----   7/6/2018  10:16 PM     24 11 passwordchangelog.txt
-a----   9/4/2018   5:20 PM     26 TestFile0.txt           
-a----   9/4/2018   5:20 PM     26 TestFile1.txt           
-a----   9/4/2018   5:20 PM     26 TestFile2.txt 




# Get the target files, matching the string filter
Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -List -Pattern 'test'

# Results

D:\temp\TestFile0.txt:1:test
D:\temp\TestFile1.txt:1:test
D:\temp\TestFile2.txt:1:test


# Get the target files, matching the string filter, clearing the meta data
(Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -list -pattern 'test') -replace (':\d.*')

# Results

D:\temp\TestFile0.txt
D:\temp\TestFile1.txt
D:\temp\TestFile2.txt


# Get the target files, matching the string filter, clearing the meta data, test direcory create and file action
(Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -list -pattern 'test') -replace (':\d.*') |
Move-Item -Destination (New-Item -Type Directory -Verbose -Force ('D:\temp\NewFiles') -WhatIf) -Verbose -WhatIf

# Results

Move-Item : Cannot process argument because the value of argument "destination" is null. Change the value of argument "destination" to a non-null value.
At line:3 char:1
+ Move-Item -dest (New-Item -Type Directory -Verbose -Force ('D:\temp\N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Move-Item], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.MoveItemCommand




# Get the target files, matching the string filter, clearing the meta data, force direcory create if it does not exist and execute the file action
(Get-ChildItem -Path 'd:\temp' -filter '*.txt' | 
Select-String -List -Pattern 'test') -replace (':\d.*') |
Move-Item -Destination (New-Item -Type Directory -Verbose -Force ('D:\temp\NewFiles')) -Verbose -Force

# Results

VERBOSE: Performing the operation "Create Directory" on target "Destination: D:\temp\NewFiles".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\TestFile0.txt Destination: D:\temp\NewFiles\TestFile0.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\TestFile1.txt Destination: D:\temp\NewFiles\TestFile1.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\TestFile2.txt Destination: D:\temp\NewFiles\TestFile2.txt".


Get-ChildItem -Path 'D:\temp\NewFiles' | Format-Table -AutoSize

# Results

    Directory: D:\temp\NewFiles

Mode          LastWriteTime Length Name         
----          ------------- ------ ----         
-a----   9/4/2018   5:20 PM     26 TestFile0.txt
-a----   9/4/2018   5:20 PM     26 TestFile1.txt
-a----   9/4/2018   5:20 PM     26 TestFile2.txt