Move files from subfolders to parent folders windows 10 cmd powershell.

huangapple go评论84阅读模式
英文:

Move files from subfolders to parent folders windows 10 cmd powershell

问题

我一直在尝试通过运行以下的 .bat 和 .ps1 脚本将文件从多个子文件夹移动到它们的父文件夹:

1. forfiles /s /m *.* /c "cmd /c move @path %CD%"
2. powershell -Command "gci *.* -R | mv -D ."
3. Get-ChildItem -File -Recurse | Move-Item -Destination .
4. Get-ChildItem -File -Recurse | Move-Item -Destination ..

文件夹结构是 '顶层文件夹' 和两个子文件夹:Top_folder\Subfolder_1\Subfolder_2。目标是从主 '顶层文件夹' 运行脚本,将所有来自 Subfolder_2 的文件移动到 Subfolder_1。主文件夹有许多

Subfolder_1\Subfolder_2
Subfolder_1\Subfolder_2
...
Subfolder_1\Subfolder_2

文件夹。上面的代码将文件移动到脚本所在的当前文件夹,或者根据通配符 '..' 或 '.' 移动到父文件夹(从脚本位置向上移动)。

任何想法/建议将不胜感激。

英文:

I have been trying to move files from multiple subfolders to their parent folders by running the following .bat and .ps1 scripts:

1.  forfiles /s /m *.* /c "cmd /c move @path %CD%"
2. powershell -Command "gci *.* -R | mv -D ."
3. Get-ChildItem -File -Recurse | Move-Item -Destination .
4. Get-ChildItem -File -Recurse | Move-Item -Destination ..

The folders structure is 'Top folder' and two subfolders: Top_folder\Subfolder_1\Subfolder_2. The aim is to run script from main 'top folder' and having all the files from Subfolder_2 moved to Subfolder_1. The main folder have a numerous

Subfolder_1\Subfolder_2
Subfolder_1\Subfolder_2
...
Subfolder_1\Subfolder_2

folders. The above codes move files either to the current folder where script locates or into parent folder (up from the script location), depends on wildcard '..' or '.'

Any thoughts/ideas would be greatly appreciated.

答案1

得分: 0

以下是翻译好的代码部分:

# 根路径通过变量 $PSScriptRoot 引用到脚本的位置。这通常是我在大多数情况下使用的方式。
$RootPath = $PSScriptRoot

# 获取根路径中的父文件夹列表
$ParentFolders = Get-ChildItem -Path $RootPath | Where {$_.PSIsContainer}

# 对于每个父文件夹,递归获取所有文件并移动到父文件夹,附加数字以避免冲突
ForEach ($Parent in $ParentFolders) {
    Get-ChildItem -Path $Parent.FullName -Recurse | Where {!$_.PSIsContainer -and ($_.DirectoryName -ne $Parent.FullName)} | ForEach {
        $FileInc = 1
        Do {
            If ($FileInc -eq 1) {$MovePath = Join-Path -Path $Parent.FullName -ChildPath $_.Name}
            Else {$MovePath = Join-Path -Path $Parent.FullName -ChildPath "$($_.BaseName)($FileInc)$($_.Extension)"}
            $FileInc++
        }
        While (Test-Path -Path $MovePath -PathType Leaf)
        Move-Item -Path $_.FullName -Destination $MovePath
    }
}
英文:

> The solution has been kindly provided by @francishagyard2 [here][1]
>
>
> [1]:
> https://community.spiceworks.com/topic/2236930-way-to-move-all-files-from-multiple-levels-of-sub-folders

# Root path is referenced to script's location by means of variable $PSScriptRoot. That's how I use ones in most cases.
$RootPath = $PSScriptRoot

# Get list of parent folders in root path
$ParentFolders = Get-ChildItem -Path $RootPath | Where {$_.PSIsContainer}

# For each parent folder get all files recursively and move to parent, append number to file to avoid collisions
ForEach ($Parent in $ParentFolders) {
    Get-ChildItem -Path $Parent.FullName -Recurse | Where {!$_.PSIsContainer -and ($_.DirectoryName -ne $Parent.FullName)} | ForEach {
        $FileInc = 1
        Do {
            If ($FileInc -eq 1) {$MovePath = Join-Path -Path $Parent.FullName -ChildPath $_.Name}
            Else {$MovePath = Join-Path -Path $Parent.FullName -ChildPath "$($_.BaseName)($FileInc)$($_.Extension)"}
            $FileInc++
        }
        While (Test-Path -Path $MovePath -PathType Leaf)
        Move-Item -Path $_.FullName -Destination $MovePath
    }
}

huangapple
  • 本文由 发表于 2023年5月30日 06:01:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定