[英]How to move files from a directory to another directory based on file size
I am currently working on an automation project at work.我目前正在从事一个自动化项目。 One of the steps to the program that I need to separate files that are greater than 6000KB from the other files in the folder.
我需要将大于 6000KB 的文件与文件夹中的其他文件分开的程序步骤之一。 I am looking for a batch file that is able to move files that are greater than 6000KB from their current directory to another directory.
我正在寻找一个能够将大于 6000KB 的文件从当前目录移动到另一个目录的批处理文件。 I need this batch file to do handle a batch of files and not just one.
我需要这个批处理文件来处理一批文件而不仅仅是一个文件。 Any thoughts on how to do this in a batch file?
关于如何在批处理文件中执行此操作的任何想法?
you can use vbs with file object: File object reference您可以将 vbs 与文件 object 一起使用:文件 object 参考
or you can try to make a.bat file using this command to extract the filesize.或者您可以尝试使用此命令制作一个 .bat 文件来提取文件大小。
for %%R in ([filepath and name]) do if %%~zR GTR [file size]
from this page: Batch file to check filesize从此页面: 检查文件大小的批处理文件
I would recommend the VBS options for more security.我会推荐 VBS 选项以获得更高的安全性。
EDIT: this is a batch file for moving the files.编辑:这是一个用于移动文件的批处理文件。 Please change the
<move file>
to a more appropriate command.请将
<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
Replace "c:\From" and "c:\To" paths to your real paths to files将“c:\From”和“c:\To”路径替换为文件的真实路径
/MIN:n
: MINimum file size - exclude files smaller than n bytes. /MIN:n
:最小文件大小 - 排除小于 n 字节的文件。
/MT[:n]
: Multithreaded copying, n = no. /MT[:n]
:多线程复制,n = 否。 of threads to use (1-128) ### default = 8 threads, not compatible with /IPG and /EFSRAW for Windows7要使用的线程数 (1-128) ### 默认 = 8 个线程,与 Windows7 的 /IPG 和 /EFSRAW 不兼容
/E
: Copy Subfolders, including Empty Subfolders. /E
:复制子文件夹,包括空子文件夹。
/S
: Copy Subfolders. /S
:复制子文件夹。
Robocopy is a Standard Windows7 command, to use it under Windowx XP download and install Microsoft Resource Kit Robocopy 是一个标准的Windows7命令,要在Windowx XP下使用它下载并安装Microsoft Resource Kit
Details and other parameters for Robocopy are here Robocopy 的详细信息和其他参数在这里
If you want to use VBScript, you can use this script as a basis:如果要使用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")
You could run this (call it move-files.vbs
) from a batch file thus:您可以从批处理文件中运行它(称为
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.