移动具有相同名称但不同扩展名的文件。电源外壳
Move a files that have the same name but different extension. Powershell
我是一名初级技术人员,负责编写一个简短的 powershell 脚本。问题是我在 5 小时前开始学习 PS - 一旦我的老板告诉我被分配到这个任务。我有点担心明天无法完成,所以希望你们能帮帮我。任务是:
我需要根据某些条件将文件移动到不同的文件夹,让我从他的文件夹结构开始:
c:\LostFiles: This folder includes a long list of .mov, .jpg and .png files
c:\Media: This folder includes many subfolders withe media files and projects.
工作是将文件从 c:\LostFiles 移动到 c:\Media 文件夹树中的适当文件夹 if
c:\LostFiles 中的文件名对应于 C:\media 的其中一个子文件夹中的文件名 我们必须忽略扩展名,例如:
C:\LostFiles 有我们需要移动的这些文件(如果可能):imageFlower.png、videoMarch.mov、danceRock.bmp
C:\Media\Flowers\ 已经有这个文件: imageFlower.bmp, imageFlower.mov
imageFlower.png 应移至此文件夹 (C:\media\Flowers),因为存在或存在基本名称完全相同的文件(必须忽略扩展名)
只应移动具有相应文件(相同名称)的文件。
到目前为止,我已经编写了这段代码(我知道它并不多,但会在我正在处理它时更新这段代码(格林威治标准时间 2145)。我知道我遗漏了一些循环,嘿,是的,我错过了很多!
#This gets all the files from the folder
$orphans = gci -path C:\lostfiles\ -File | Select Basename
#This gets the list of files from all the folders
$Files = gci C:\media\ -Recurse -File | select Fullname
#So we can all the files and we check them 1 by 1
$orphans | ForEach-Object {
#variable that stores the name of the current file
$file = ($_.BaseName)
#path to copy the file, and then search for files with the same name but only take into the accont the base name
$path = $Files | where-object{$_ -eq $file}
#move the current file to the destination
move-item -path $_.fullname -destination $path -whatif
}
您可以从媒体文件构建哈希表,然后遍历丢失的文件,查看丢失文件的名称是否在哈希表中。类似于:
# Create a hashtable with key = file basename and value = containing directory
$mediaFiles = @{}
Get-ChildItem -Recurse .\Media | ?{!$_.PsIsContainer} | Select-Object BaseName, DirectoryName |
ForEach-Object { $mediaFiles[$_.BaseName] = $_.DirectoryName }
# Look through lost files and if the lost file exists in the hash, then move it
Get-ChildItem -Recurse .\LostFiles | ?{!$_.PsIsContainer} |
ForEach-Object { if ($mediaFiles.ContainsKey($_.BaseName)) { Move-Item -whatif $_.FullName $mediaFiles[$_.BaseName] } }
我是一名初级技术人员,负责编写一个简短的 powershell 脚本。问题是我在 5 小时前开始学习 PS - 一旦我的老板告诉我被分配到这个任务。我有点担心明天无法完成,所以希望你们能帮帮我。任务是:
我需要根据某些条件将文件移动到不同的文件夹,让我从他的文件夹结构开始:
c:\LostFiles: This folder includes a long list of .mov, .jpg and .png files
c:\Media: This folder includes many subfolders withe media files and projects.
工作是将文件从 c:\LostFiles 移动到 c:\Media 文件夹树中的适当文件夹 if
c:\LostFiles 中的文件名对应于 C:\media 的其中一个子文件夹中的文件名 我们必须忽略扩展名,例如:
C:\LostFiles 有我们需要移动的这些文件(如果可能):imageFlower.png、videoMarch.mov、danceRock.bmp
C:\Media\Flowers\ 已经有这个文件: imageFlower.bmp, imageFlower.mov
imageFlower.png 应移至此文件夹 (C:\media\Flowers),因为存在或存在基本名称完全相同的文件(必须忽略扩展名)
只应移动具有相应文件(相同名称)的文件。
到目前为止,我已经编写了这段代码(我知道它并不多,但会在我正在处理它时更新这段代码(格林威治标准时间 2145)。我知道我遗漏了一些循环,嘿,是的,我错过了很多!
#This gets all the files from the folder
$orphans = gci -path C:\lostfiles\ -File | Select Basename
#This gets the list of files from all the folders
$Files = gci C:\media\ -Recurse -File | select Fullname
#So we can all the files and we check them 1 by 1
$orphans | ForEach-Object {
#variable that stores the name of the current file
$file = ($_.BaseName)
#path to copy the file, and then search for files with the same name but only take into the accont the base name
$path = $Files | where-object{$_ -eq $file}
#move the current file to the destination
move-item -path $_.fullname -destination $path -whatif
}
您可以从媒体文件构建哈希表,然后遍历丢失的文件,查看丢失文件的名称是否在哈希表中。类似于:
# Create a hashtable with key = file basename and value = containing directory
$mediaFiles = @{}
Get-ChildItem -Recurse .\Media | ?{!$_.PsIsContainer} | Select-Object BaseName, DirectoryName |
ForEach-Object { $mediaFiles[$_.BaseName] = $_.DirectoryName }
# Look through lost files and if the lost file exists in the hash, then move it
Get-ChildItem -Recurse .\LostFiles | ?{!$_.PsIsContainer} |
ForEach-Object { if ($mediaFiles.ContainsKey($_.BaseName)) { Move-Item -whatif $_.FullName $mediaFiles[$_.BaseName] } }