简体   繁体   English

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

[英]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.

相关问题 VBscript 将文件从一个目录移动到另一个目录 - VBscript to move files from one directory to another 将文件夹内容从一个目录移动到另一个目录 - Move folder content from one directory to another VB 脚本 - 将超过 180 天的文件从修改日期移动到另一个目录 - VB Script - move files older than 180 days from modified date to another directory 将所有文件从一个目录复制到另一个目录 - Copy all files from one directory to another 如何根据文件名模式将文件移动到不同的文件夹? - How to move files to different folders based on pattern of file name? VBS Active Directory(2003)将用户从一组组转移到另一组 - VBS Active Directory (2003) Move users from one set of groups to another VBscript从目录中删除多个.txt文件,文件大小为0kb,当前日期为 - VBscript to delete multiple .txt files from a directory with 0kb size and with current date 我想将文件从子目录(1)中的目录(2)移动到子目录(1),然后通过VBScript删除文件夹(2) - I want to move the file from the directory (2) in the subdirectory (1) to a subdirectory (1) and delete the folder (2) by VBScript 有没有办法使用 oFSO.CopyFolder &amp; split() 将文件从多个目录复制到另一个 - Is there a way to use oFSO.CopyFolder & split() to copy files from more than one directory to another 从AS400中的根目录移动文件 - Moving files from root directory in AS400
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM