Scan and check for locked files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Scan and check for locked files

#1 Post by Hackoo » 29 Aug 2017 17:55

Hi :)
I made this script thanks to dbenham's trick to check for locked file
I made some test on my windows 7 (32 bits) and it works for me like a charm :wink:
But, today, i test it on windows 7 (64 bits) , I noted that the script must be improved for the architecture 64 bits
So my question what's the best function to add in this script to check for the correct architecture (32 bits or 64 bits) and it will work of course on both ?

Code: Select all

@echo off
Rem This source is inspired from here
Rem hxxps://stackoverflow.com/questions/
Rem 10518151/how-to-check-in-command-line-if-a-given-file-or-directory-is-locked-used-by-any?answertab=active#tab-top
Rem Thanks for dbenham for this nice trick ;)
Mode con cols=90 lines=5 & color 9E
Title Scan and Check for Locked Files by Hackoo 2017
set "LogFile=%~dp0%~n0.html"
(
   echo ^<html^>
    echo ^<title^> Scan and Check for locked files by Hackoo 2017^</title^>
   echo ^<body bgcolor^=#ffdfb7^>
   echo ^<center^>^<b^>Log Started on %Date% @ %Time% by the user : "%username%" on the computer : "%ComputerName%"^</b^>^</center^>
)> "%LogFile%"
echo(
echo       --------------------------------------------------------------------------
echo         Please Wait a while ....... Scanning for locked files is in progress
echo       --------------------------------------------------------------------------
Rem We Play radio just for fun and in order to let the user be patient until the scan ended
Call :Play_DJ_Buzz_Radio
Timeout /T 3 /nobreak>nul
cls
Set Folders=^
^ "%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup"^
^ "%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"^
^ "%ProgramFiles%\Internet Explorer"^
^ "%ProgramFiles%\Skype"^
^ "%ProgramFiles%\TeamViewer"^
^ "%WinDir%\system32\drivers"^
^ "%Temp%"

@For %%a in (%Folders%) Do (
   ( echo ^<hr^>^<font color^=DarkOrange^>^<B^>Folder : %%a^</B^>^</font^>^<hr^>) >> "%LogFile%" 2>&1
   @for /f "delims=" %%b in ('Dir /A-D /s /b "%%~a\*.*"') do (
      Call :Scanning "%%~nxb"
      Call:Check_Locked_File "%%~b" "%LogFile%"
   )
)

(
   echo ^<hr^>
   echo ^<center^>^<b^>Log ended on %Date% @ %Time% on the computer : "%ComputerName%"^</b^>^</center^>
   echo ^</body^>
   echo ^</html^>
)>> "%LogFile%"
Start "" "%LogFile%" & Call :Stop_Radio & exit
::***********************************************************************************
:Check_Locked_File <File> <LogFile>
(
   2>nul (
   >>%1 (call )
   ) && ( @echo ^<font color^=green^>file "%~1"^</font^>^<br^>
   ) || (
      @echo ^<font color^=red^>file "%~1" is locked and is in use^</font^>^<br^>
   )
)>>%2 2>nul
exit /b
::***********************************************************************************
:Scanning <file>
cls
echo(
echo       --------------------------------------------------------------------------
echo          Please Wait a while... Scanning for %1
echo       --------------------------------------------------------------------------
exit /b
::***********************************************************************************
:Play_DJ_Buzz_Radio
Taskkill /IM "wscript.exe" /F >nul 2>&1
Set "vbsfile=%temp%\DJBuzzRadio.vbs"
Set "URL=http://www.chocradios.ch/djbuzzradio_windows.mp3.asx"
Call:Play "%URL%" "%vbsfile%"
Start "" "%vbsfile%"
Exit /b
::**************************************************************
:Play
(
echo Play "%~1"
echo Sub Play(URL^)
echo    Dim Sound
echo    Set Sound = CreateObject("WMPlayer.OCX"^)
echo    Sound.URL = URL
echo    Sound.settings.volume = 100
echo    Sound.Controls.play
echo    do while Sound.currentmedia.duration = 0
echo       wscript.sleep 100
echo    loop
echo    wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000
echo End Sub
)>%~2
exit /b
::**************************************************************
:Stop_Radio
Taskkill /IM "wscript.exe" /F >nul 2>&1
If Exist "%vbsfile%" Del "%vbsfile%"
::**************************************************************

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Scan and check for locked files

#2 Post by penpen » 31 Aug 2017 03:56

Hackoo wrote:But, today, i test it on windows 7 (64 bits) , I noted that the script must be improved for the architecture 64 bits
So my question what's the best function to add in this script to check for the correct architecture (32 bits or 64 bits) and it will work of course on both ?
You didn't mention why your script has to be improved, so one only could guess what you really need.
I see two possibilities.

(1) In case you want to know if your computer is running a 32 or 64 bit OS, then https://support.microsoft.com/en-us/help/556009 describes how to do it.
I don't like temporary files, so you might do something like that instead (untested):

Code: Select all

@echo off
(
   reg.exe Query "HKLM\Hardware\Description\System\CentralProcessor\0"
) | (
   find /i "x86" >nul && (
      echo "This is 32 Bit Operating system."
   ) || (
      echo "This is 64 Bit Operating System."
   )
)


(2) If you are interested in the current process information, then https://msdn.microsoft.com/en-us/library/aa384274.aspx gives the answer (untested):

Code: Select all

@echo off
if not defined PROCESSOR_ARCHITECTURE (
   echo "This is a 32 Bit Process."
) else if "%PROCESSOR_ARCHITECTURE%" == "x86" (
   echo "This is a 32 Bit Process."
) else (
   echo "This is a 64 Bit Process."
)


penpen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Scan and check for locked files

#3 Post by Compo » 31 Aug 2017 05:36

It seems to me that your issue is with paths using %ProgramFiles% instead of potentially %ProgramFiles(x86)% in the locations specified in %Folders%.

Why not use both locations and either use an If Exist "%%a\" or 2^>Nul where necessary.

The locations specified there should be user specified anyhow, I changed:

Code: Select all

^ "%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"^
to:

Code: Select all

^ "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup"^
Then removed

Code: Select all

^ "%ProgramFiles%\Internet Explorer"^
^ "%ProgramFiles%\Skype"^
^ "%ProgramFiles%\TeamViewer"^
Because I don't ever have any intention of using IE and do not have the other two programs installed.

The script worked perfectly, after I removed all of the colour and sound bloat on Windows 7 x64:

Code: Select all

@Echo Off
Set "LogFile=%~dp0%~n0.html"

(Echo ^<html^>
   Echo    ^<title^>Scan and Check for locked files by Hackoo 2017^</title^>
   Echo    ^<body bgcolor=#ffdfb7^>
   Echo       ^<center^>^<b^>Log Started on %Date% @ %Time% by the user : "%username%" on the computer : "%ComputerName%"^</b^>^</center^>
)>"%LogFile%"

Echo(
Echo    Please Wait a while ....... Scanning for locked files is in progress
Timeout 3 /NoBreak>Nul
ClS

Set Folders="%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup"^
 "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup"^
 "%SystemRoot%\system32\drivers"^
 "%Temp%"

For %%A In (%Folders%) Do (
   (Echo       ^<hr^>^<font color=DarkOrange^>^<b^>Folder : %%A^</b^>^</font^>^<hr^>)>>"%LogFile%" 2>&1
   For /F "Delims=" %%B In ('Dir/B/S/A-D-S-L "%%~A\*"') Do (
      ClS
      Echo(
      Echo    Please Wait a while... Scanning for "%%~nxB"
      Call :Check_Locked_File "%%~B" "%LogFile%"))

(Echo       ^<hr^>
   Echo       ^<center^>^<b^>Log ended on %Date% @ %Time% on the computer : "%ComputerName%"^</b^>^</center^>
   Echo    ^</body^>
   Echo ^</html^>)>>"%LogFile%"

Start "" "%LogFile%"
Exit/B

:Check_Locked_File <File> <LogFile>
((>>%1 (Call )
   ) || (Echo       ^<font color=red^>file "%~1" is locked and is in use^</font^>^<br^>
   ) 2>Nul)>>%2 2>Nul

Post Reply