Solved

Winbatch script to check a file's size and overwrite

Posted on 2009-04-01
9
828 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
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

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

Author Comment

by:Johnnynoname
Comment Utility
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
Comment Utility
I'll get started for you. In the meantime, are you running this against a single file or multiple files?
0
 

Author Comment

by:Johnnynoname
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

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

Expert Comment

by:Justin_W_Chandler
Comment Utility
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 125 total points
Comment Utility
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
Comment Utility
Thanks!  I'll take a look and see what's what
0
 

Author Comment

by:Johnnynoname
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now