我一直在尝试找到一种将当前的工作目录设置为文件的父文件夹的方法,以便我可以复制居住在特定文件夹上方文件夹中的PDF。
基本上,我要做的就是这样。
检查在最后一天已修改了共享的哪些文件夹
递归搜索那些修改的文件夹中的clxml文件,其中包含一个我需要的单词
将我当前的位置设置为该路径,浏览一个文件夹,然后将任何PDF复制到新位置。
到目前为止,我的所有工作都可以正常工作,除了我无法弄清楚如何将当前目录设置为foreach循环中CLXML的位置。我不知道最终 Get-Childitem
和我的 copy-item
之间需要进行什么。
$ClxmlFolder = Get-ChildItem "\\clwsdrapsi02\BATCH STORAGE\" -Directory| Where-Object {$_.LastWriteTime -ge ((Get-Date).Date) -and $_.LastWriteTime -lt ((Get-Date).Date).AddDays(+1)}
$ClxmlPath = Get-ChildItem $ClxmlFolder.FullName -Recurse -Directory -Include 'clxml' | Select-Object -Property FullName, Directory
foreach ($Folder in $ClxmlPath)
{
$ClxmlPath.FullName
break
}
foreach ($File in $ClxmlPath)
{
$ClxmlFile = Get-ChildItem $ClxmlPath.FullName -Recurse -Filter "*.clxml" | Where-Object {Select-String Newton $_ -Quiet} | Select-Object -Property Fullname, Directory
$ClxmlFile.Fullname
Copy-Item .\*.PDF -destination "C:\test uploads\Uploaded" -Recurse -Force
Break
}
I have been trying to find a way to set my current working directory to a parent folder of a file so that I can copy a PDF that lives in a folder above a particular folder.
Basically what I am trying to do is this;
Check which folders on a share have been modified in the last day
Recursively search through those modified folders for a clxml file that contains a word I need
Set my current location to that path, browse back a folder and copy out any PDF's to a new location.
So far I have everything working except I can't figure out how to set my current directory to the location of the clxml within the foreach loop. I can't figure out what needs to go between the final Get-ChildItem
and my copy-item
$ClxmlFolder = Get-ChildItem "\\clwsdrapsi02\BATCH STORAGE\" -Directory| Where-Object {$_.LastWriteTime -ge ((Get-Date).Date) -and $_.LastWriteTime -lt ((Get-Date).Date).AddDays(+1)}
$ClxmlPath = Get-ChildItem $ClxmlFolder.FullName -Recurse -Directory -Include 'clxml' | Select-Object -Property FullName, Directory
foreach ($Folder in $ClxmlPath)
{
$ClxmlPath.FullName
break
}
foreach ($File in $ClxmlPath)
{
$ClxmlFile = Get-ChildItem $ClxmlPath.FullName -Recurse -Filter "*.clxml" | Where-Object {Select-String Newton $_ -Quiet} | Select-Object -Property Fullname, Directory
$ClxmlFile.Fullname
Copy-Item .\*.PDF -destination "C:\test uploads\Uploaded" -Recurse -Force
Break
}
发布评论
评论(1)
通过使用
select-object -property fullname,Directory
您正在转换afileInfo
对象进入pscustomObject
在这种情况下,您不想要。我们可以通过调用 fileInfo 对象)的父文件夹。 。 =“ https://learn.microsoft.com/en-us/dotnet/api/system.io.directoryinfo?view=net-6.0” rel =“ nofollow noreferrer”>directoryinfo
具有.parent
属性。总结一下,首先您可以进行过滤:
然后,假设仅返回一个文件,您可以
set-location
,或者如果要将目录更改为父文件夹然后返回,您可以使用Push-Location
和pop-Location
。如果要将目录更改为父母的父目录:
另外,您可以简单地将父级文件夹的
fullname
与\*。 :
By using
Select-Object -Property Fullname, Directory
you're converting aFileInfo
object into aPSCustomObject
which in this case, you do not want. We can get the parent folder of a file (aFileInfo
object) by calling it's.Directory
property and furthermore, this would return aDirectoryInfo
object which has a.Parent
property.Summarizing, first you can do your filtering:
And then, assuming this only returns one file, you can either
Set-Location
or if you want to change directory to the parent folder and then go back, you can usePush-Location
andPop-Location
.If you want to change directory to the parent's parent directory:
Alternatively, instead of changing your current directory, you could simple combine the
FullName
of the Parent folder with\*.PDF
: