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

时间:2011-08-11 17:53:58

标签: vbscript batch-file batch-processing

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

3 个答案:

答案 0 :(得分:2)

您可以将vbs与文件对象一起使用:File object reference

或者您可以尝试使用此命令创建.bat文件以提取文件大小。

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

从此页面:Batch file to check filesize

我建议使用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>

答案 1 :(得分:2)

ROBOCOPY c:\From c:\To /E /MOVE /MIN:6144000  /MT:32

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

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

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

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

/S:复制子文件夹。

Robocopy是标准 Windows7 命令,可在 Windowx XP 下载并安装Microsoft Resource Kit

下使用它

Details and other parameters for Robocopy are here

答案 2 :(得分:2)

如果要使用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
相关问题