[solved] Set file size as Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dwngrt
Posts: 26
Joined: 07 Oct 2013 05:14
Location: The Netherlands

[solved] Set file size as Variable

#1 Post by dwngrt » 08 Nov 2013 02:54

Hello again peepz ^^
i am still fine-tuning my LAN chat, what i got as refresh in the reader is nothing more then a

Code: Select all

ping loacalhost -n 2 >NUL

but this makes the chat sort of blink what can be quiet annoying when working with 2 screens and the blinking chat at the second screen.

So i has this idea to make the reader check the size of the chat.txt, let it set as a variable, something like this:

Code: Select all

:set
set size=[file size thingy]
goto check

:check
if %size%==[file size thingy] goto refresh
if not %size%==[file size thingy] ping localhost -n 2 >NUL
goto check

:refresh
cls
type chat.txt
goto set


i have no idea if this is possible, but if it is then it would be perfect ^_^
lemme know if you has any idea :3

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: [solved] Set file size as Variable

#2 Post by Magialisk » 08 Nov 2013 03:48

You could call a subroutine and pass it the name of the file, along the lines of 'CALL :GETSIZE chat.txt'

The :GETSIZE would then do 'set size=%~Z1', which expands to the size of the file it was passed as parameter 1. Type 'CALL /?' into the CMD window to see these kinds of special expansions.

Also, a FOR /F %%i IN ('some command') DO set size=%%i would work, but off the top of my head I can't think of what command would be most appropriate to return the filesize. The best I could think of would be the 'DIR' command, piped to a Find on filename, and extract the fourth delimiter which is size.

So basically something like this: FOR /F %%i IN ('DIR /-C ^| Find "chat.txt"') DO set size=%%i

There are definitely other ways of doing this, many of them probably better, but this should get you started.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: [solved] Set file size as Variable

#3 Post by foxidrive » 08 Nov 2013 03:49

I'm not sure but see if this is what you need:

Code: Select all

:set
for %%a in (filesize.txt) do set "size=%%~za"
if not "%size%"=="0" (goto refresh) else (ping localhost -n 2 >NUL)
goto set

:refresh
cls
type chat.txt
goto set

dwngrt
Posts: 26
Joined: 07 Oct 2013 05:14
Location: The Netherlands

Re: [solved] Set file size as Variable

#4 Post by dwngrt » 08 Nov 2013 06:25

Thank you peepz for the great replies, i got it working but on another way .-.
I'l give a short explanation on how i achieved what i needed for those who might concern :)

My plan was to make my chat reader refresh when new changes where made to the chat.txt file instead of a refresh interval of 2 pings.

Here the bit of code from the chat script:

Code: Select all

set C=
set /p C=[msg]:

echo %C%>update.txt


here the bit of code from the reader script:

Code: Select all

:read
if exist 1.txt del 1.txt && goto update
(
set /p check=
)<update.txt >NUL


:check
if exist 1.txt goto update

(
set /p check2=
)<update.txt >NUL
if not "%check2%"=="%check%" goto update
goto check


if anyone is curious to the full project, just let me know ^^

Post Reply