将数千个文件recursion移动到子文件夹窗口中

我在一个文件夹中有一个具有> 300,000图像的Windows Server。

我感觉文件系统的性能受到影响,我想把它们移到子文件夹中,正如其他许多人所build议的那样。

我的大多数文件是10个字符+ .jpg – 以1开头(例如:1234567890.jpg)

我想recursion地创build基于文件名(3个字符组)的子文件夹 – 并将文件移入它们。 所以1234567890.jpg会从c:\ images移动到c:\ Images \ 1 \ 234 \ 567 \ 890。

我怎样才能做到这一点与batch file或wscript?

PowerShell真的是一个很好的工具。 我不会为你写一个脚本,但我会指出你正确的方向。

您可以使用get-childitem列出目录中的所有内容,然后将其传递给foreach循环。 然后,使用.substring(0,2)来获得前三个字母。 将这三个保存到一个variables中,并使用new-item -Name <foldername> -ItemType folderreplace保存前三个数字的variables来创build文件夹。 testing这个文件夹的存在是一个好主意,只有在不存在的时候才会这样做。 泡沫冲洗重复添加一些逻辑,你的独特情况将可能决定。

然后,可以使用move-item将文件移动到基​​于模式匹配的文件夹中,该模式匹配查看文件的前三个字母。


我知道你想要一个人为你写一个脚本,但给一个男人一条鱼,教一个男人去钓鱼…

PowerShell也将是我的工具; 事实上,我已经拿出这个单行(sorting)在c:\ images中运行:

 dir *.jpg | %{ $dst = $_.basename -replace '(...)(...)(...)$','\$1\$2\$3'; md $dst >$null 2>&1; move-item $_ -Destination $dst } 

给自己安排一些PowerShell的时间,在ISE中玩弄并学习 。


扩展的等价物是这样的:

 dir *.jpg | foreach-object { $dst = $_.basename -replace '(...)(...)(...)$','\$1\$2\$3' md $dst >$null 2>&1 move-item $_ -Destination $dst } 

或者,更详细一点,并在评论中的一个例子:

 # if run from "c:\images\", we can get the image list $images = dir *.jpg # process images one by one foreach ($image in $images) { # suppose $image now holds the file object for "c:\images\1234567890.jpg" # get its file name without the extension, keeping just "1234567890" $filenamewithoutextension = $image.basename # group by 3 from the end, resulting in "1\234\567\890" $destinationfolderpath = $filenamewithoutextension -replace '(...)(...)(...)$','\$1\$2\$3' # silently make the directory structure for "1\234\567\890" md $destinationfolderpath >$null # move the image from "c:\images\1234567890.jpg" to the new folder "c:\images\1\234\567\890\" move-item $image -Destination $destinationfolderpath # the image is now available at "c:\images\1\234\567\890\1234567890.jpg" } 

我实际上结束了使用wscript – 因为我还没有玩过PowerShell。 它运行得非常快。 我假设10个字符的文件名(之前.ext)从1开始

这里是我跑的一个副本:

 'On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") length=10 maindir="C:\inetpub\wwwroot\InventoryImages\" dodir "C:\inetpub\wwwroot\InventoryImages" set files=nothing set folder=nothing set fso=nothing wscript.quit sub dodir(sFolder) Set folder = fso.GetFolder(sFolder) Set files = folder.Files For each file In files if len(file.name)=length+4 then dofile sfolder,file.name Next end sub sub dofile(dir,filename) wscript.echo dir & " - " & filename t_filename=left(filename,10) t_filename=mid(t_filename,2) 'ASSUMING 1 FIRST DIR 'NOT CHECKING FOR EXISTENCE t_dir=maindir & "1\" checkcreate t_dir & left(t_filename,3) t_dir=t_dir & left(t_filename,3) & "\" t_filename=mid(t_filename,4) checkcreate t_dir & left(t_filename,3) t_dir=t_dir & left(t_filename,3) & "\" t_filename=mid(t_filename,4) wscript.echo dir & "\" & filename & "->" & t_dir & filename fso.movefile dir &"\" & filename,t_dir & filename end sub sub checkcreate(dir) if not fso.FolderExists(dir) then wscript.echo dir & " does not exist" fso.CreateFolder(dir) end if end sub