Check if file size is changing with Windows batch - batch-file

I have a little problem I'm trying to sort out. I'm recording sound 24/7 and they are split in .wav files that have 2 hour duration. Since .wav files are pretty large I would like to encode them to .mp3. Everything works fine with this script
#echo off
cd path/to/folder
for %%i in (*.wav) do (
lame.exe %%i
del %%i
)
Everything works as it should but there is one problem, I want to check if file is still recording, if that's true then I don't want to encode it. I tried to check file size now and 10 sec later but it gets same value. Here is example
#echo off
cd path/to/folder
for %%i in (*.wav) do (
echo %%~zi
ping -n 10 127.0.0.1 >nul
echo %%~zi
)
output is for example:
444444
and after 10 sec same
444444.
Can someone please tell me how to check file size if it's constantly changing?

Test this on your files.
It will get a filename (sorted alphabetically in NTFS) and test to see if it is locked, and loop if it is - testing every 60 seconds.
When the file is free it will launch lame and then check the next file.
#echo off
cd /d "path/to/folder"
for %%a in (*.wav) do call :next "%%a"
echo done
pause
goto :eof
:next
set "filename=%~1"
:loop
2>nul (call;>>"%filename%") && (
echo "%filename%" is free! & goto :lame
) || (
echo "%filename%" is in use or is read only!
)
timeout /t 60 /nobreak
goto :loop
:lame
lame.exe "%~1"
del "%~1"

Related

Check two pdf files and print if bigger than minimum size

I have two pdf files which generated daily (from another part of the script using wkhtmltopdf) then batch will print this pdf if the size bigger than 9000byte. I have batch script below but only able to check for one pdf file. How to check for both pdf and print this file if meet minimum size?
File structure:
folder1\dailyreport1.pdf
folder2\dailyreport2.pdf
Batch script:
#Echo off
setlocal enabledelayedexpansion
set file=folder1\dailyreport1.pdf
::set file=folder2\dailyreport2.pdf
set "minsize=9000"
For %%I In (%file%)Do (
set "size=%%~zI"
if %%~zI LSS 9000 call :NOPRINT
if %%~zI GTR 9000 call :PRINT
)
:NOPRINT
echo Size %size% ^< %minsize%, skipped print %file%
goto SHUTDOWN
:PRINT
echo Size %size% ^> %minsize%, print %file%
:SHUTDOWN
echo shutdown.exe -s -t 600
PAUSE
Thank you for the tips and comment. Ended up im doing something like and dicthing the :call because cant get it working haha. Is it OK? Any comment/ improvement or another version much appreciated. Thank you.
#Echo off
setlocal enabledelayedexpansion
Set "Exe1=%ProgramFiles(x86)%\Foxit Software\Foxit Reader\Foxit Reader.exe"
set "file=folder1\dailyreport1.pdf, folder2\dailyreport2.pdf"
For %%I In (%file%)Do (
if %%~zI LSS 9000 Echo Skipped printing %%I
if %%~zI GTR 9000 Echo Printing %%I &"%Exe1%" /p %%I
)
echo shutdown.exe -s -t 600
PAUSE

Doing a windows files count down of installing windows updates

This is the code I am working on with right now. The code run the *.msu file from a dir onto the system for update. Looking to add a number count down the files from 9 to 1 so can tell The code is using a bat file. where I am all most done with the install.
There is the code
#echo off
setlocal EnableDelayedExpansion
'This part code found the number of msu files
for /f %%A in ('dir *.msu') do (
set files=!dirs!
set dirs=%%A
)
'This part of code put the file msu file name
for /f %%A in ('dir /b *.msu') do (
echo == Installing Updates == "%files%" "%%A" ...
%%A /quiet /norestart
)
echo.
echo ########################################
echo.
echo == Updates installed ==
echo.
echo == Press any key to restart ==&pause>nul
echo.
shutdown.exe /r /t 0
I would the output to look like this if can
== Installing Updates == "9" "file" ...
== Installing Updates == "8" "File" ...
I just need help a way of every time install update it make the number go down by one. Any help would be good. Thank for all your help.
Krii's answer should work fine. However there are easier ways. Such as the timeout command. The following will wait for ten seconds, and display a timer:
timeout 10
Or if you didn't want any output you could add > nul at the end of the command.
Another possibility is to exploit the choice command:
choice /c a /n /d a /t 10 > nul
That example also won't have any output. Of course the user can press the a key to cancel the countdown and continue the script, but I don't see that being an issue.
There is probably a better way to do this, but is this what you want?
#echo off
for /f %%A in ('dir /b *.msu') do (
echo == Installing Updates == "%%A" ...
%%A /quiet /norestart
)
for /l %%A in (9,-1,1) do (
cls
echo %%A
ping 1.1.1.1 -n 1 -w 1 >nul
)
cls
echo.
echo ########################################
echo.
echo == Updates installed ==
echo.
echo == Press any key to restart ==&pause>nul
echo.
shutdown.exe /r /t 0

Batch file to check file existence by certain time

New to batch scripting. Trying to write a script which checks for files with filename starting with LEND by polling a directory and if the file is not received by 17:30, then write a log message to log file. I have written the below batch script, the script runs fine as long as there are no files in the directory. As soon as I put files in there, it stops running. And restarts if I delete the files from that directory. Could you please advise where I am going wrong?
Thanks
#echo off
set I=0
set log=C:\logs\alerting.log
:recurse
for /f %%P in ('dir /b "C:\incoming\LEND*"') do (call :countfiles)
set Time=%time:~0,5%
echo Filecount: %I% at Time: %Time% >> %log%
if %Time%==17:30 goto OUT
pause 60
goto :recurse
:countfiles
set /a I+=1
:OUT
if %I%==0 echo LEND Files not received >> %log%
EXIT
Your code goes from countfiles to exit. Try this:
:countfiles
set /a I+=1
goto:eof
There were actually several flaws - reusing the time variable, expecting pause to be used as a delay, the time frame could have been missed in the test, and other more minor things.
This is tested as far as the file counting goes but not the 17:30 branch
I changed the style of a few commands and variable names (I is too much like l and i and 1 in many fonts)
#echo off
set log=C:\logs\alerting.log
:loop
for /f %%P in ('dir /b "C:\incoming\LEND*" 2^>nul ^| find /c /v "" ') do set c=%%P
set now=%time:~0,5%
echo Filecount: %c% at Time: %now%
echo Filecount: %c% at Time: %now% >> %log%
if %now:~0,2%%now:~3,2% GTR 1730 goto :OUT
ping -n 60 localhost >nul
goto :loop
:OUT
if %c% EQU 0 echo LEND Files not received >> %log%
EXIT

Remotely check file size and get report

This script tries to:
get an input from a txt file, which is a list of computer names,
check if a log file on a computer from the list is bigger than 1000 bytes,
create a txt report with the names of those computers where the log file is more than 1000 bytes,
create another txt report with the names of the computers where the file is less than 1000 bytes.
However, something goes wrong. Any help could be nice.
#echo off
for /f "tokens=*" %%I in (computernames.txt)do call :checksz %%I
goto :eof
:checksz
setlocal
set file="\\%1\c$\data\info.log"
set min=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% GTQ %min% (
echo. %1 >>logsizemin.txt
) ELSE (
echo. %1>>logsizemax.txt
)
Hello everyone,
thanks for your valuable support. I congratulate whith those who conceived and built this site is really well done and useful.
I made some modifications to the script that you have kindly suggested to incorporate other features, but something is not working as I would like .. as you can view I use editv32 in order to hide password in my batch, the principle is the same but as you can see after checking the size of the log, "maxlongsize.txt" is used in order to take the names of the PCs on which do the next activity. I wish that the activities were performed sequentially on all PCs in the file "logsizemax.txt" with only one authentication at the beginning. I noticed that, for some reason sometimes the file "logsizemin.txt" is not created but i don't understand why. The maximum would be to put in another file such as "computer unreachable" those PCs that are not reached on the network but I have absolutely no idea how implement it. I hope I have sufficiently explained. Sorry for bad English! I think you understand my intention :). Following the batch
#setlocal
#echo off
editv32 -p "Enter username:" USR
editv32 -m -p "Enter password:" PWD
for /f "tokens=1" %%I in (computernames.txt) do call :checksz %%I
endlocal
goto :eof
:checksz
setlocal
set file="\\%1\c$\data\data.log"
set min=1000
type NUL>logsizemax.txt
type NUL>logsizemin.txt
if not exist %file% goto :eof
FOR /F "usebackq delims=" %%A IN ('%file%') DO set size=%%~zA
if %size% GEQ %min% ( echo %1>>logsizemax.txt ) ELSE ( echo %1>>logsizemin.txt )
endlocal
#setlocal
for /f "tokens=1" %%I in (logsizemax.txt) do call :sw %%I
endlocal
goto :eof
:sw
echo.
echo *****************************************
echo * VBS & Deploy script *
echo *****************************************
echo.
echo Run VBS and deploy script .....
psexec \\%1 -u %USR% -p %PWD% cscript "\\rep\Reg.vbs"
psexec \\%1 -u %USR% -p %PWD% cmd /c "\\rep\deploy.cmd"
echo.
echo **************************
echo * EXE Run *
echo **************************
echo.
echo Running exe .....
psexec -u %USR% -p %PWD% \\%1 "c:\Program Files\test.exe"
echo.
echo ***********************************
echo * Exe application launched *
echo ***********************************
echo.
goto END
:END
exit
You can avoid using environment variables and using the second FOR alltogether. Try this simpler version of your bat, with a more generic :checksz routine.
#echo off
for /f "tokens=*" %%a in (computernames.txt) do (
call :checksz "\\%%a\c$\data\info.log" 1000 "%%a"
)
goto :eof
:checksz
if %~z1 GTR %2 (
echo %~3 >> logsizemin.txt
) ELSE (
echo %~3 >> logsizemax.txt
)
goto :eof
see HELP CALL for more information.
Changes: GTG->GEQ, don't surround variable with quotes twice, remove leading space from echo, and a little clean up.
#setlocal
#echo off
for /f "tokens=1" %%I in (computernames.txt) do call :checksz %%I
endlocal
goto :eof
:checksz
setlocal
set file=\\%1\c$\data\info.log
set min=1000
if not exist %file% endlocal && goto :eof
FOR /F "usebackq delims=" %%A IN ('%file%') DO set size=%%~zA
if %size% GEQ %min% ( echo %1>>logsizemin.txt ) ELSE ( echo %1>>logsizemax.txt )
endlocal
goto :eof
edit: updated per comments from PA and Andriy M - endlocal if the file doesn't exist, and remove \ update note.

Compare Size of Same File in Batch File

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
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.
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
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)

Resources