Batch file with if statement to check file size

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jay_Dogg
Posts: 2
Joined: 03 Nov 2010 17:23

Batch file with if statement to check file size

#1 Post by Jay_Dogg » 03 Nov 2010 17:30

Ok I made this batch file that looks for a file on computers backs it up and replaces it. We can call the file we are looking for file.a and the file that is going to replace it file.b (they share the same name) So right now when the batch script finds file.a it makes a copy of file.a in the same director named its file.a.old then copys file.b over file.a. What I would like to do is add an if statement to this batch file that check the size of the original file.a. If file.a size is = 2.69MB or 2,826,240 bytes then make a backup file.a.old and replace file.a with file.b. if not leave file.a alone and do nothing.

There is my code right now that just searches, backups and replace file.a

for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
move /y %%a %%~DPa\file.a.old
move /y file.b %%a
)
)

It would be great if some one could help me add this if statement into this patch file.

Thanks

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Batch file with if statement to check file size

#2 Post by !k » 03 Nov 2010 21:24

Code: Select all

for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
   for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
      if "%%~za" == "2826240" (
         move /y %%a %%~DPa\file.a.old
         move /y file.b %%a
      )
   )
)

Jay_Dogg
Posts: 2
Joined: 03 Nov 2010 17:23

Re: Batch file with if statement to check file size

#3 Post by Jay_Dogg » 03 Nov 2010 22:56

Hey thanks so much for that code this will help me out so so much. I wanted to know if you or anyone for that matter wouldn't mind looking over the rest of my code checking for syntax errors or anything else that does not look right. I see 2 problems as it stands first would be the new files which are going to replace the file.a aka chkhedr.dll are located at C:\updater\32\ and C:\updater\64. These to paths need to be excluded in the loop and if statement cause as of right now the batch script finds them first and renames them to chkhedr.dll.old and then there is no chkhedr.dll in the directories to be copied to the other directories on the computer. Second would be I need help writing the code to undo the changes made. This bit of code would just rename the chkhedr.dll.old back to chkhedr.dll over writing the chkhedr.dll file in the directory putting everything back to the way it was before the batch script ran.

So here is my code.

@echo off
echo.
echo This batch script updates all the chkhedr.dll files on this computer
echo.
:menu
echo 1.Replace all chkhedr.dll file on 32 bit systems
echo 2.Replace all chkhedr.dll file on 64 bit systems
echo 3.Undo all system changes made by this batch script
echo 4.Exit
set /p choice=enter your choice 1 2 3 or 4:
if %choice%==1 goto replace 32 bit
if %choice%==2 goto replace 64 bit
if %choice%==3 goto undo
if %choice%==4 goto exit

:replace 32 bit
cd C:\updater\32\
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\chkhedr.dll 2^>nul') do (
if "%%~za" == "2826240" (
move /y %%a %%~DPa\chkhedr.dll.old
move /y chkhedr.dll %%a
)
)
)
goto menu

:replace 64 bit
cd C:\updater\64\
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\chkhedr.dll 2^>nul') do (
if "%%~za" == "3253670" (
move /y %%a %%~DPa\chkhedr.dll.old
move /y chkhedr.dll %%a
)
)
)
goto menu

:undo
??????????????????????????????????????????????????????????
goto menu

:exit
exit

Any Help would be great. And Thanks Again !k.

Post Reply