我需要读取文件,然后根据第一行中的值将文件移至值名称。
例如,如果显示type = item
,则将该文件移至我可能需要创建的名为item
的目录中,然后移至下一个文件。
我如何完成此任务?
答案 0 :(得分:0)
至于这个...
get-childitem "<SourceFolder>" -filter 7*.txt - recurse | select-string -list -pattern "test" | move -dest "<DestinationFolder>"
…它永远不会起作用,因为它会返回完整的文件名以及您作为集合搜索的字符串。因此,您正在执行的操作可能需要循环并清除所有随机内容。
示例-照原样使用您的帖子评论,并撤消移动/撤回内容:
Get-ChildItem '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
因此,基于以上内容,您可以看到,尽管您在匹配字符串,但是您没有沿管道传递合法的完整文件名。因此,您想采用其他方法或使用现有的方法来清理结果,但要修剪掉不必要的内容。
根据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