如何通过foreach循环将当前目录设置为文件的位置

发布于 2025-01-22 21:09:35 字数 1033 浏览 0 评论 0 原文

我一直在尝试找到一种将当前的工作目录设置为文件的父文件夹的方法,以便我可以复制居住在特定文件夹上方文件夹中的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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2025-01-29 21:09:35

通过使用 select-object -property fullname,Directory 您正在转换a fileInfo 对象进入 pscustomObject 在这种情况下,您不想要。我们可以通过调用 fileInfo 对象)的父文件夹。 。 =“ https://learn.microsoft.com/en-us/dotnet/api/system.io.directoryinfo?view=net-6.0” rel =“ nofollow noreferrer”> directoryinfo 具有 .parent 属性

总结一下,首先您可以进行过滤:

$ClxmlFile = Get-ChildItem $ClxmlPath.FullName -Recurse -Filter "*.clxml" |
    Where-Object { Select-String Newton $_ -Quiet }

然后,假设仅返回一个文件,您可以 set-location ,或者如果要将目录更改为父文件夹然后返回,您可以使用 Push-Location pop-Location

if($ClxmlFile) { # if the above filtering returned a result
    Set-Location $ClxmlFile.Directory
    # rest of the logic here
}

如果要将目录更改为父母的父目录:

Set-Location $ClxmlFile.Directory.Parent

另外,您可以简单地将父级文件夹的 fullname \*。 :

$source = $ClxmlFile.Directory.FullName + '\*.PDF'
Copy-Item $source -Destination "C:\test uploads\Uploaded" -Recurse -Force

By using Select-Object -Property Fullname, Directory you're converting a FileInfo object into a PSCustomObject which in this case, you do not want. We can get the parent folder of a file (a FileInfo object) by calling it's .Directory property and furthermore, this would return a DirectoryInfo object which has a .Parent property.

Summarizing, first you can do your filtering:

$ClxmlFile = Get-ChildItem $ClxmlPath.FullName -Recurse -Filter "*.clxml" |
    Where-Object { Select-String Newton $_ -Quiet }

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 use Push-Location and Pop-Location.

if($ClxmlFile) { # if the above filtering returned a result
    Set-Location $ClxmlFile.Directory
    # rest of the logic here
}

If you want to change directory to the parent's parent directory:

Set-Location $ClxmlFile.Directory.Parent

Alternatively, instead of changing your current directory, you could simple combine the FullName of the Parent folder with \*.PDF:

$source = $ClxmlFile.Directory.FullName + '\*.PDF'
Copy-Item $source -Destination "C:\test uploads\Uploaded" -Recurse -Force
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文