问题描述
我有一个目录,里面有一堆文本文件.我正在目录中创建一个新文件夹以将文本文件移入,当我完成合并它们时,我只想将输出文件移回原始目录.
I have a directory with a bunch of text files in it. I am creating a new folder inside of the directory to move the text files into and when I am done merging them I want to only move the output file back out to the original directory.
当我创建文件夹并将文本文件移动到其中时,它不会让我进入文件夹并执行我的操作.我坚持这个.
When I create the folder and move the text files into it, it will not let me go inside of the folder and perform my actions. I am stuck on this.
我的代码:
$Path = '*.RemoveFirst\txt'
$PathDump ='C:RemoveFirst\DumpARoo'
$Output = 'C:RemoveFirst\TestingFile.txt'
if(!(Test-Path -Path $PathDump)) {
New-Item -ItemType Directory $PathDump
}
elseif (Test-Path -Path $PathDump){
Move-Item $Path -Destination $PathDump # move (not copy) files into new directory to concat
Get-Item $PathDump | ForEach-Object {
Get-Content $_ |
Select-Object -Skip 1 |
Select-Object -SkipLast 1 |
Add-Content $OutPut
}
Write-Host 'This already exists'
}
推荐答案
您的路径似乎有误.$Path
应该是 'C:\RemoveFirst\*.txt'
并且您定义的其他两个路径在驱动器冒号后缺少反斜杠.
Your paths seem wrong. $Path
should be 'C:\RemoveFirst\*.txt'
and the other two paths you define are missing a backslash after the drive colon.
接下来,我没有看到 elseif
中的逻辑,因为当您第一次测试并得出结论 $PathDump
路径不存在时,您创建了它.这应该足以继续执行代码,无需使用 elsif needed 测试路径是否存在.
Next, I don't see the logic in the elseif
because when you first test and conclude the $PathDump
path does not exist, you create it. That should be enough to continue with the code, no testing if the path exists using elsif needed .
试试
$Path = 'C:\RemoveFirst\*.txt'
$PathDump = 'C:\RemoveFirst\DumpARoo'
$Output = 'C:\RemoveFirst\TestingFile.txt'
if(!(Test-Path -Path $PathDump)) {
# create the folder if it does not yet exist
$null = New-Item -ItemType Directory $PathDump
}
# move all *.txt items from 'C:\RemoveFirst\txt' to 'C:\RemoveFirst\DumpARoo'
Move-Item $Path -Destination $PathDump # move (not copy) files into new directory to concat
Get-ChildItem -Path $PathDump -Filter '*.txt' -File | ForEach-Object {
$_ | Get-Content |
Select-Object -Skip 1 |
Select-Object -SkipLast 1 |
Add-Content -Path $OutPut
}
这篇关于尝试读取新创建的文件夹中的文件时,Powershell“权限被拒绝"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!