问题
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. 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. 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?
回答1:
If you want to use VBScript, you can use this script as a basis:
' 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:
cscript move-files.vbs /source:"C:\Documents and Settings\Username\Desktop" /destination:"F:\backup" /sizelimit:1000
回答2:
ROBOCOPY c:\From c:\To /E /MOVE /MIN:6144000 /MT:32
Replace "c:\From" and "c:\To" paths to your real paths to files
/MIN:n
: MINimum file size - exclude files smaller than n bytes.
/MT[:n]
: Multithreaded copying, n = no. of threads to use (1-128) ###
default = 8 threads, not compatible with /IPG and /EFSRAW
for Windows7
/E
: Copy Subfolders, including Empty Subfolders.
/S
: Copy Subfolders.
Robocopy is a Standard Windows7 command, to use it under Windowx XP download and install Microsoft Resource Kit
Details and other parameters for Robocopy are here
回答3:
you can use vbs with file object: File object reference
or you can try to make a .bat file using this command to extract the filesize.
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.
EDIT: this is a batch file for moving the files. Please change the <move file>
to a more appropriate command.
@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>
来源:https://stackoverflow.com/questions/7030565/how-to-move-files-from-a-directory-to-another-directory-based-on-file-size