Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Winbatch script to check a file's size and overwrite

Posted on 2009-04-01
9
Medium Priority
?
837 Views
Last Modified: 2012-05-06
I need a Winbatch/VB script that will check the size of a file.  If the file is the predetermined size, it overwrite it with another file.  If the file is larger than a the predetermined size, it will do nothing.  I need the script to look in 2 specific directories for the file.

Can this be done?  I have been unsuccessful in getting this to work with Winbatch, but I admit I am just a novice at scripting right now.

Thanks for any help!
0
Comment
Question by:Johnnynoname
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24044063
Do you mean that if the file is smaller than a set size, it should be overwritten?
0
 

Author Comment

by:Johnnynoname
ID: 24044270
I have a file that exists in both "C:\test" and "C:\test1" on the same machine.  

I need both files to be looked at and if they are the 2KB standard file size, then they get overwritten with the new 2KB file.  If they are any bigger then the 2KB size, they get ignored.
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24049963
I'll get started for you. In the meantime, are you running this against a single file or multiple files?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Johnnynoname
ID: 24050603
It's the same file, but in two separate directories on the local drive.  The filename is the same, but the paths are different
0
 

Author Comment

by:Johnnynoname
ID: 24152437
Well....  this has been helpful!  I guess we can close this issue
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24152681
My goodness I'm sorry I didn't click to track this question so I forgot to submit the script. I'm done with it, and I'll upload it for you within a few moments.

Justin Chandler
0
 
LVL 6

Accepted Solution

by:
Justin_W_Chandler earned 500 total points
ID: 24152942
Here you go! Again, sorry for the delay

Justin Chandler
@echo off
setlocal enabledelayedexpansion
 
:: Set constant here. const_targetFileSize is in bytes
set const_targetFileSize=2048
set const_fileToCheck=FILE2CHK.TXT
set const_newFileSource=c:\temp
 
for /f "skip=5 tokens=4" %%A in ('dir /-c c:\test\%const_fileToCheck%') do (
   set var_fileSize=%%A
   if !var_fileSize! gtr %const_targetFileSize% copy /y "%const_newFileSource%\%const_fileToCheck%" c:\test
   echo Old file size is !var_fileSize!
   goto :exit_test)
:exit_test
 
for /f "skip=5 tokens=4" %%A in ('dir /-c c:\test1\%const_fileToCheck%') do (
   set var_fileSize=%%A
   if !var_fileSize! gtr %const_targetFileSize% copy /y "%const_newFileSource%\%const_fileToCheck%" c:\test1
   echo Old file size is !var_fileSize!
   goto :exit_test1)
:exit_test1

Open in new window

24286570-Check-file-size-and-ove.txt
0
 

Author Comment

by:Johnnynoname
ID: 24207996
Thanks!  I'll take a look and see what's what
0
 

Author Comment

by:Johnnynoname
ID: 24270411
Sorry about the delay, I was out of town for a few days.  Thanks for the script!  It works like a charm!
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question