-1

I have 1 task where there is 1 file start generating through DB and took almost 1 hour to generate, there is 1 batch file which check this file whenever it is available it calls a new batch file and trigger "file sent", the issue is that this file which is generating continuously did not completely generated at the file picked the same and call the batch file

What i want to do to build a logic where i can compare the size of file within a loop with 2 variable sizeA and sizeB, and call another batch file when sizeA==sizeB now the only issue is i am not sure how to built this logic in a code.\

Here is what I have tried:

@echo off
setlocal EnableDelayedExpansion
set file = "C:\Users\rb54761\Desktop\New folder\File.txt"
set "size=0"
pause
:loop for /f "tokens=*" %%x in ('dir /s /a /b "%file%"2^>nul') do set /a size=%%~zx
echo !size!
PAUSE
if !size! == !size! goto call
goto loop 
:call echo Success
Gerhard
  • 22,678
  • 7
  • 27
  • 43
Rishabh Bhargav
  • 35
  • 1
  • 11
  • how big is the file? – Gerhard Aug 29 '18 at 13:51
  • @GerhardBarnard approx. 2 GB – Rishabh Bhargav Aug 29 '18 at 13:52
  • What have you tried, where are you stuck? Please share a [mcve]! – aschipfl Aug 29 '18 at 13:53
  • @echo off setlocal EnableDelayedExpansion set file = "C:\Users\rb54761\Desktop\New folder\File.txt" set "size=0" pause :loop for /f "tokens=*" %%x in ('dir /s /a /b "%file%"2^>nul') do set /a size=%%~zx echo !size! PAUSE if !size! == !size! goto call goto loop :call echo Success – Rishabh Bhargav Aug 29 '18 at 13:54
  • Please delete the comment, I posted your code into your question. Let's see if whoever downvoted will return and retract their downvote. – Gerhard Aug 29 '18 at 14:10
  • @aschipfl I added his code to his question, not sure if you added the close vote, but just informing you for incase you did, and decide to retract it. – Gerhard Aug 29 '18 at 14:21
  • batch file only works with 32-bit signed arithmetic, therefore files that are 2GB or more won't work. You need a different solution, or change to powershell – phuclv Aug 29 '18 at 15:09
  • @phuclv, you are basically right; however, for a simple equal-to comparison you could treat the numbers as strings (surround them by `""`, or use the `==` operator), so even greater numbers could be compared... – aschipfl Aug 30 '18 at 09:31
  • 1
    @GerhardBarnard, thanks for adding the code; I retracted the close-vote. I hope that the OP does that by himself next time though (he should know how to ask after being a member for over two years now)... – aschipfl Aug 30 '18 at 09:33
  • @aschipfl yes, comparisons of equality will work, however I saw Gerhard's answer below first which uses `LSS` so it won't work in that case and I put a notice here – phuclv Aug 30 '18 at 09:33
  • Thanks for posting the code in the question @GerhardBarnard, i have found the solution which i am posting in Answer Comment. – Rishabh Bhargav Aug 30 '18 at 10:55

3 Answers3

0

EDIT I only saw your comment now for being a 2gb file, then the below code will not work.

If the file were to be smaller..here is an example:

@echo off
set "myfile=C:\Users\rb54761\Desktop\New folder\File.txt"
:start
for /f %%I in ("%myfile%") do set size=%%~zI
if %size% LSS 100 (echo file not ready %size% & timeout /t 10 & goto :start)
echo copy file.

as per above, It will check the file for a size of 100kb, if not that size yet, it will timeout for 10 seconds and goto the beginning and test again until the file reaches 100kb, where it will no longer meet the if statement and pass that line and simply echo copy file.

Please note there are no spaces in my set commands. I would suggest you run from cmd the help for /? for more on this command.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

We can use below approach in order to compare the size of same file within a span of time

@echo off
setlocal EnableDelayedExpansion
set file="d:\File_Mask1\New_File\File.txt"
set "size=0"

If exist %file% GOTO loop
GOTO nofile

:loop
for /f "tokens=*" %%x in ('dir /s /a /b %file%') do set /a size=%%~zx
echo !size!

waitfor SomethingThatIsNeverHappening /T 120 >nul 2>&1

for /f "tokens=*" %%y in ('dir /s /a /b %file%') do set /a size1=%%~zy
echo !size1!
if !size! EQU !size1! goto call
goto loop

:call
echo Success
endlocal
Rishabh Bhargav
  • 35
  • 1
  • 11
  • waitfor is not what you want to use here. In any event that you might in future use errorlevel, it will cause an errorlevel of 1, though not the case here, but there is a batter method. Rather use `timeout /T 120 >nul` – Gerhard Aug 30 '18 at 12:05
  • You should read [this](https://stackoverflow.com/questions/35769581/weird-results-with-if) and linked questions before deciding to trust your `if`. – Stephan Aug 30 '18 at 12:31
  • @GerhardBarnard i tried to use timeout /t, but when i was running the batch through Autosys the batch throwing error "ERROR: Input redirection is not supported, exiting the process immediately." this is why i am using Waitfor – Rishabh Bhargav Sep 01 '18 at 06:33
0

This takes advantage of the archive attribute. Windows sets this attribute each time it writes to the file. So if we can unset it, wait for a time and check it (I use dir /aa which will not find the file, when the attribute is not set)

@echo off
set sec=10
set "file="C:\Users\rb54761\Desktop\New folder\File.txt""
set /a secs=sec+1
:loop
  attrib -a "%file%"
  timeout %sec% >nul
  dir /b /aa "%file%" >nul 2>&1 || goto :loop
echo %file% didn't change since %secs% seconds

(Note: your set file = ... line is wrong. The spaces around the = will become part of the variable name respective the value)

Advantage: no file size limit (files bigger 2GB will be handled fine too)

Stephan
  • 53,940
  • 10
  • 58
  • 91