找回密码
 立即注册
搜索
查看: 4073|回复: 7

[软件] 用new bing帮我写powershell失败了

[复制链接]
     
发表于 2023-4-1 00:11 | 显示全部楼层 |阅读模式
本帖最后由 tyx776 于 2023-4-1 00:08 编辑

新时代的人机沟通,学习一下。我是一点不懂powershell,也不会代码。就想给new bing试试我的要求应该不复杂吧,就是某个文件夹下面有3层
xxxxxx---folder1--1--文件
              folder2--1--文件
              folder3--1--文件
              folder4--文件
我希望把文件都提到第二层就是folder那层,下面是newbing给我的代码。而且有写明第二层是自动识别的。我就按这个代码输入,destination填的是 目标文件夹xxxxxx\folder2。结果是失败,没有任何反应。这是问题出在哪?它还说有可能是没有权限,然后我用管理员运行重新来过也不行

如果您想要将第三层文件夹中的文件移动到第二层文件夹中,可以使用以下命令:
$FirstLevelFolders = Get-ChildItem -Path "C:\" -Directory
$SecondLevelFolders = $FirstLevelFolders | Get-ChildItem -Directory

$ThirdLevelFiles = $SecondLevelFolders | Get-ChildItem -Directory | Get-ChildItem -File

$ThirdLevelFiles | Move-Item -Destination "C:\Folder2"
这将获取“C:\”中的所有文件夹,并将它们存储在$FirstLevelFolders变量中。然后,它将获取第二层文件夹,并将它们存储在$SecondLevelFolders变量中。接下来,它将获取第三层文件夹中的所有文件,并将它们存储在$ThirdLevelFiles变量中。最后,它将移动$ThirdLevelFiles变量中的所有文件到“C:\Folder2”中。
请注意,这将移动第三层下的所有文件到第二层自动识别的文件夹中。如果您只想移动特定类型的文件,请在Get-ChildItem cmdlet中添加过滤器。
希望这可以帮助到您!












回复

使用道具 举报

头像被屏蔽
发表于 2023-4-1 00:36 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

     
发表于 2023-4-1 05:54 | 显示全部楼层
本帖最后由 泰坦失足 于 2023-4-1 06:04 编辑

Q: 写一个powershell命令.
xxxxxx文件夹有以下三层结构:
xxxxxx---folder1--1--文件
              folder2--1--文件
              folder3--1--文件
              folder4--文件
将所有子文件夹里的文件移动到第二层文件夹'folderX'中


Bing 侧栏-创意模式:
# 获取xxxxxx文件夹下的所有子文件夹
$folders = Get-ChildItem -Path xxxxxx -Directory -Recurse
# 遍历每个子文件夹
foreach ($folder in $folders) {
    # 获取子文件夹下的所有文件
    $files = Get-ChildItem -Path $folder.FullName -File
    # 遍历每个文件
    foreach ($file in $files) {
        # 移动文件到folderX文件夹
        Move-Item -Path $file.FullName -Destination xxxxxx\folderX
    }
}
Bing 网站-标准(零分):
Here’s a PowerShell command that should do what you’re asking for:

Get-ChildItem -Path "C:\xxxxxx" -Recurse -File | Move-Item -Destination "C:\xxxxxx\folder1\"
This command will move all files in the subfolders of the xxxxxx folder to the folder1 folder. You can replace C:\xxxxxx with the path to your folder and folder1 with the name of the folder you want to move the files to.

Let me know if you have any questions!



GPT4:
以下是一个PowerShell命令,用于将所有子文件夹中的文件移动到相应的第二层文件夹:

powershell
Copy code
$rootFolder = 'xxxxxx'

Get-ChildItem -Path $rootFolder -Directory | ForEach-Object {
    $secondLevelFolder = $_.FullName
    Get-ChildItem -Path $secondLevelFolder -Recurse -File | Move-Item -Destination $secondLevelFolder
}
这个命令首先获取根文件夹"xxxxxx"中的所有子文件夹,然后对每个子文件夹执行以下操作:

获取子文件夹的完整路径。
递归地获取子文件夹中的所有文件。
将找到的文件移动到相应的第二层文件夹

GPT-3 Plus的极速模式:
以下是一个可能的 PowerShell 命令:

php
Copy code
Get-ChildItem -Path "xxxxxx" -Recurse -Directory |
ForEach-Object {
    $files = Get-ChildItem -Path $_.FullName -File
    if ($files.Count -gt 0) {
        $targetPath = Join-Path $_.FullName "folderX"
        if (-not (Test-Path $targetPath)) {
            New-Item -ItemType Directory -Path $targetPath | Out-Null
        }
        Move-Item $files.FullName -Destination $targetPath
    }
}
这个命令会遍历 xxxxxx 文件夹下的所有子文件夹,将其中的所有文件移动到名为 folderX 的第二层文件夹中。具体来说:

Get-ChildItem -Path "xxxxxx" -Recurse -Directory 会遍历 xxxxxx 文件夹及其子文件夹,获取其中的所有子文件夹。
ForEach-Object 会对每个子文件夹执行一遍命令块。
Get-ChildItem -Path $_.FullName -File 会获取当前子文件夹中的所有文件。
如果子文件夹中有文件,则会创建一个名为 folderX 的目标文件夹,并使用 Move-Item 命令将文件移动到目标文件夹中。
值得注意的是,如果目标文件夹 folderX 已经存在,命令不会删除其中已有的文件。如果需要将文件合并到已有的 folderX 文件夹中,可以将 Move-Item 命令替换为 Copy-Item 命令
Google Bard:
Sure, here is a PowerShell command that will move all the files in the subfolders of the xxxxxx folder to the second level folder 'folderX':

Get-ChildItem -Path xxxxxx -Recurse -File | Move-Item -Destination xxxxxx\folderX
This command will first use the Get-ChildItem cmdlet to get all the files in the xxxxxx folder and its subfolders. Then, it will use the Move-Item cmdlet to move all of these files to the folderX folder.

看来还是花钱的GPT-4 ChatGPT最聪明, 问题是一个月20刀, 尝鲜一个月后不打算续费



评分

参与人数 1战斗力 +1 收起 理由
tyx776 + 1 好评加鹅

查看全部评分

回复

使用道具 举报

     
 楼主| 发表于 2023-4-1 13:53 | 显示全部楼层
大江户战士 发表于 2023-4-1 00:36
说明你的提示写的不好

$sourceFolder = "C:\母文件夹"

我试了,也不行。第一次运行,它把第一个文件夹的第三层复制到了第二层。不是移动。其他第二层文件夹没动。第二次运行,动的是第二层第二个文件夹。。。我还原原本的结构后再运行就没反应了
回复

使用道具 举报

     
 楼主| 发表于 2023-4-1 14:27 | 显示全部楼层
泰坦失足 发表于 2023-4-1 05:54
Q: 写一个powershell命令.
xxxxxx文件夹有以下三层结构:
xxxxxx---folder1--1--文件

gpt4的我试了,不行。我看代码好像没有涉及第三层。
gpt3的代码有“folderx”这是固定名称感觉也是不行的。
对比几家不同的,确实有帮助。人机沟通好难啊。感觉至少现阶段没法用,至少你自己不懂是没法识别正确与否。如果直接用,导致不可挽回的错误更麻烦。
回复

使用道具 举报

     
发表于 2023-4-1 14:32 | 显示全部楼层
变一下说法有时候会出完全不同的结果,反正你的问题
回复

使用道具 举报

头像被屏蔽
发表于 2023-4-1 15:11 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

     
 楼主| 发表于 2023-4-1 15:30 | 显示全部楼层
大江户战士 发表于 2023-4-1 15:11
复制完了删除原文件夹就好了,或者把Copy-Item改成Move-Item
我测试了一下没有问题
...

额,确实是我的问题。保存成sh1文件没有关联执行,我就自以为的用powershell打开就行。
直接复制代码执行就可以了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|上海互联网违法和不良信息举报中心|网上有害信息举报专区|962110 反电信诈骗|举报电话 021-62035905|Stage1st ( 沪ICP备13020230号-1|沪公网安备 31010702007642号 )

GMT+8, 2025-7-13 07:27 , Processed in 0.050769 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表