powershell - 如何读取内部文件值并创建目录然后移动文件 PowerShell?
问题描述
我需要从文件中读取,并根据第一行的值,将文件移动到值名称。
例如,如果它type = item
显示 ,将该文件移动到我可能需要创建的目录中item
,然后移动到下一个文件。
我怎样才能完成这项任务?
解决方案
通过您的评论与原始帖子。至于这个……
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
推荐阅读
- algorithm - 如何平衡多个消息队列
- r - 我想用标记的椭圆和组的部分向量绘制 FactoMineR MFA
- multithreading - Python线程一次不运行2个进程
- react-native - 如何在 react-native-signature-capture 中获取保存的签名图像
- shell - Shell:如果..elif 条件无法按要求创建文件
- image - 如何将类型“UnityEngine.Texture2D”转换为“UnityEngine.Sprite”?
- javascript - JS:传递的 JSON-Array 丢失引号
- javascript - JS:在for循环中调用异步函数不引用相同的迭代变量
- android - 使用喷气背包导航在操作栏中显示返回箭头
- linux - 端口扫描仪显示模块错误,这与什么有关?