繁体   English   中英

如何根据文件大小将文件从目录移动到另一个目录

[英]How to move files from a directory to another directory based on file size

我目前正在从事一个自动化项目。 我需要将大于 6000KB 的文件与文件夹中的其他文件分开的程序步骤之一。 我正在寻找一个能够将大于 6000KB 的文件从当前目录移动到另一个目录的批处理文件。 我需要这个批处理文件来处理一批文件而不仅仅是一个文件。 关于如何在批处理文件中执行此操作的任何想法?

您可以将 vbs 与文件 object 一起使用:文件 object 参考

或者您可以尝试使用此命令制作一个 .bat 文件来提取文件大小。

for %%R in ([filepath and name]) do if %%~zR GTR [file size]

从此页面: 检查文件大小的批处理文件

我会推荐 VBS 选项以获得更高的安全性。


编辑:这是一个用于移动文件的批处理文件。 请将<move file>更改为更合适的命令。

@echo off
for /F "usebackq tokens=3,4" %%i IN (`dir /-C`) DO CALL :MOVE_FILES %%i %%j 
exit /b 0

:MOVE_FILES
if %1.==. exit /b 0
if %1 GEQ 6000000 <move file>
ROBOCOPY c:\From c:\To /E /MOVE /MIN:6144000  /MT:32

将“c:\From”和“c:\To”路径替换为文件的真实路径

/MIN:n :最小文件大小 - 排除小于 n 字节的文件。

/MT[:n] :多线程复制,n = 否。 要使用的线程数 (1-128) ### 默认 = 8 个线程,与 Windows7 的 /IPG 和 /EFSRAW 不兼容

/E :复制子文件夹,包括空子文件夹。

/S :复制子文件夹。

Robocopy 是一个标准的Windows7命令,要在Windowx XP下使用它下载并安装Microsoft Resource Kit

Robocopy 的详细信息和其他参数在这里

如果要使用VBScript,可以使用这个脚本作为基础:

' use a default source path or get one from the command line parameters
dim sourcepath: sourcepath = "some\default\path"
if WScript.Arguments.Named.Exists("source") then
    sourcepath = WScript.Arguments.Named("source")
end if

' use a default destination path or get one from the command line
dim destinationpath: destinationpath = "some\default\path"
if WScript.Arguments.Named.Exists("destination") then
    destinationpath = WScript.Arguments.Named("destination")
end if

' use a default file size limit or get one from the command line
' we accept in kbytes so we convert this to bytes
dim sizelimit: sizelimit = 6000 * 1024 ' default 6000 kbytes
if WScript.Arguments.Named.Exists("sizelimit") then
    sizelimit = WScript.Arguments.Named("sizelimit")
end if

' use a Scripting.FileSystemObject to get the file objects of each file
' in the source directory. The file object has a Size property, which
' has the file size in bytes
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
dim sourcefolder: set sourcefolder = fso.GetFolder(sourcepath)
if not fso.FolderExists(destinationpath) then
     ' we'll throw an error if the path is not found but you could instead
     ' create the directory automatically
     err.raise 1,,destinationpath & " not found"
end if

' loop through each file in the directory, compare size property against
' the limit and copy as appropriate
dim file, count: count = 0
for each file in sourcefolder.Files
    if file.size > sizelimit then
         file.Move destinationpath
         count = count + 1
    end if
next

WScript.Echo("complete: " & count & " file(s) moved")

您可以从批处理文件中运行它(称为move-files.vbs ):

cscript move-files.vbs /source:"C:\Documents and Settings\Username\Desktop" /destination:"F:\backup" /sizelimit:1000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM