I have a batch file with this code:
#ECHO off
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
Exit
I want to set an expiration date for this batch file.
For example: The programs wont run on 11/30/2018
How do I set an expiration date on this code?
Try this. Example dates are used in the IF conditions in a format YYYYMMDD (you can set your own values):
#echo off
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
set "LocalDateTime=%LocalDateTime:~0,8%"
::echo %LocalDateTime%
:: EXPIRATION DATE ::
set "EXP_DATE=20181130"
:::::::::::::::::::::
if %LocalDateTime% GTR %EXP_DATE% (
echo this wont work anymore
exit /b
)
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\Epic
Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
Exit
It is very simple:
#echo off
set "expirationDate=30112018"
call:check_expire
if %var_return% EQU 0 (goto main_code) else (exit /b)
goto main_code
:main_code
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\EpicGames\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
pause
exit /b 0
:check_expire
for %%a in (%date%) do set dt=%%a
For /f "tokens=1-3 delims=/ " %%a in ('echo %dt%') do (
if "%expirationDate%" LEQ "%%a%%b%%c" (set "var_return=1") else (set "var_return=0")
)
Please check also https://social.technet.microsoft.com/Forums/scriptcenter/en-US/14804d47-5887-4119-a9fd-167d2a14df53/set-a-batch-file-expiration-date?forum=ITCG
Hope this helps!
Related
I want to count the ruby processes running every 30 seconds and if the count changed, alert me with "OH NO!" preferably. Thanks everyone!
but I can't even get this to work...not equal to 50
FOR /F "delims=" %%G IN ('tasklist ^|find /I /C "ruby.exe"')do set count=%%G
IF "%%G"=="50" ECHO OH NO!
timeout /t 10
You attempted to set the count variable, but though it was still wrong, you never tried to use it, so let's set it correctly and actually use the count:
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "ruby.exe"') do set count=%%a
if "%count%" equ "50" echo Oh No.
timeout /t 10
To match the previous variable, let's just save variable 1, run the check again after 30 seconds and compare the 2 variables in a loop.
#echo off
:start
set count=
set count2=
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "ruby.exe"') do set count=%%a
timeout /t 30
for /f "tokens=1,*" %%i in ('tasklist ^| find /I /C "ruby.exe"') do set count2=%%i
if "%count%" equ "%count2%" (
cls
echo Count the same
goto :start
) else (
cls
echo Current count %count2% has changed from %count%
pause
)
Im making a cool hidden folder passworded USB drive. I have a little image that opens and I want to play music using this code.
set "file=music.mp3"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
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) >sound.vbs
start /min sound.vbs
I want to end the music after the user types the password. Or just the ability to tell it to stop.
I found in another post on here a solution by a user but he doesnt explain how to use it. Here is the link to that post. Link to the post Im new to batch so just giving me a script wont help I kind of need to know how to use it.
This is the script I dont understand how to use that will stop the music. According to the post I linked.
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
Here is the password code im using.
#echo off
set pass= 123abc
echo Enter Password
set /p ui=
if %ui%==%pass% (goto open)
echo Wrong Password
pause
exit
:open
start folder
How to stop the music
Note taskkill command is echoed merely... Remove echo when debugged.
see instruction by #JosefZ here : https://stackoverflow.com/a/29271203/9222942
remove the 'echo' word from this line :
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID
will becomes :
for /F "tokens=2 delims==" %%H in ("%%~G") do taskkill /T /F /PID
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
I want to search all files with .xlsx extension, for now I'm using this:
for /R c:\ %%f in (*.xlsx) do set target=%%f
echo %target%
But, only searchs in "C" and does not includes the hidden files. So, my questions:
1) How can I search in all locations, I mean: C, D, E ... drives?
2) How I can search for hidden files too?
You can try something like that :
#echo off
Color 9A & Mode con cols=70 lines=5
Set "Ext=xlsx"
Title %~nx0 to search all *.%Ext% files
set "Log=%~dp0%Ext%_PATH.txt"
If Exist "%Log%" Del "%Log%"
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
set "Fixed_Drive=%%i"
Call :ShowMsg !Fixed_Drive!
(
#For /f "delims=" %%x in ('Dir /b /s /a "!Fixed_Drive!\*.%Ext%"') do (
#echo "%%x"
)
)>> "%Log%"
)
Start "" "%Log%"
Exit
::******************************************************************
:ShowMsg
Cls
echo(
echo ***********************************
Echo Please wait a while Scanning "%~1"
echo ***********************************
Timeout /T 2 /nobreak>nul
exit /b
::******************************************************************
Edit :
To make a multiple search by extension like .xlsx .docx at the same time and get a separte log archive per each extension, you should try like this way :
#echo off
Color 9A & Mode con cols=70 lines=5
Set "Ext=xlsx docx"
For %%a in (%Ext%) Do (
if exist "%~dp0%%a_PATH.txt" del "%~dp0%%a_PATH.txt"
Call :Search "%%a" "%~dp0%%a_PATH.txt"
)
For %%a in (%Ext%) Do (
If Exist "%~dp0%%a_PATH.txt" Start "" "%~dp0%%a_PATH.txt"
)
Exit
::**********************************************************************************
:Search <Ext> <Log>
Cls
Title %~nx0 to search all "*.%~1 files"
echo(
echo ***********************************
Echo Please wait a while Scanning "%~1"
echo ***********************************
Timeout /T 2 /nobreak>nul
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
set "Fixed_Drive=%%i"
(
#For /f "delims=" %%x in ('Dir /A:-D /b /s "!Fixed_Drive!\*.%~1"') do (
#echo "%%x"
)
)>> "%~2"
)
exit /b
::**********************************************************************************
At "for in", hidden files cannot be searched. Using option "/a" of "dir", hidden files can be srarched.
To search all of files with the extension of "xlsx" on a drive, I thought following method. But about this, each drive name has to input by user.
Drive C:
dir c:\*.xlsx /b /s /a
Drive D:
dir d:\*.xlsx /b /s /a
If you want to use data from "dir" at a batch file, how about following script?
#echo off
setlocal enabledelayedexpansion
set ct=0
for /f "usebackq tokens=*" %%a in (`dir c:\*.xlsx /b /s /a`) do (
set target[!ct!]=%%a
set /a ct=!ct!+1
)
set /a ct=%ct%-1
for /l %%i in (0,1,%ct%) do echo !target[%%i]!
The Where command searches 'hidden files', so using a method similar to Hackoo's:
#Echo(Searching...&#(For /F "Skip=1" %%A In ('WMIC LogicalDisk Where^
"DriveType>1 And DriveType!=5 And FreeSpace Is Not Null" Get DeviceID'
) Do #For %%B In (%%A) Do #For /F "Delims=" %%C In (
'Where/F /R %%B\ *.xlsx') Do #Echo=%%C)&Timeout -1
I have recently started diving into writing Batch files and I have a question. I'm trying to create a file that checks to see if my secondary monitor is connected and than if it is switches the primary display to the secondary screen. (Yes I do know about the windows+P shortcut)...
So far I have figured out that "DisplaySwitch.exe /external" sets the default display to the secondary monitor but I cannot find out how to detect whether the display is there first.
-Cheers, Luke
With Windows 10, may be just a call to:
wmic desktopmonitor get DeviceID
output:
DeviceID
DesktopMonitor1
DesktopMonitor2
More details about monitor with :
wmic desktopmonitor get
By calling a powershell from the cmd the following will get the number of monitors:
powershell -Command "exit (Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams | Select-String -Pattern 'InstanceName').length"
set nMons=%ERRORLEVEL%
One possible way is to use dxdiag though it is not the fastest way:
#echo off
del ~.txt /q /f >nul 2>nul
dxdiag /t ~
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:3 >nul 2>&1
setlocal enableDelayedExpansion
set currmon=1
for /f "tokens=2 delims=:" %%a in ('find "Current Mode:" ~.txt') do (
echo Monitor !currmon! : %%a
set /a currmon=currmon+1
)
endlocal
del ~.txt /q /f >nul 2>nul
this will print the resolutions of all monitors.
Update:
dxdiag prints info about all monitors so you can check if there are more than one monitors:
#echo off
del ~.txt /q /f >nul 2>nul
start "" /w dxdiag /t ~
for /f "tokens=1* delims=:" %%a in ('find /c "Current Mode:" "~.txt"') do (
set /a "number_of_monitors=%%b"
rem echo #%%b#
)
rem exit /b 0
echo %number_of_monitors%
rem :---- if it needed -----:
if defined number_of_monitors ( if %number_of_monitors% GTR 1 ( echo second monitor connected ) else (echo only one monitor connected ))
del ~.txt /q /f >nul 2>nul
#npocmaka's answer didn't quite work for me, but this variation of his code did (Windows 10):
rem #echo off
del %TEMP%\dxdiag.txt /q /f >nul 2>nul
start "" /w dxdiag -64bit -t %TEMP%\dxdiag.txt
for /f "tokens=3" %%f in ('find /c"Monitor Name:" %TEMP%\dxdiag.txt') do set MONITOR_COUNT=%%f
if defined MONITOR_COUNT ( if %MONITOR_COUNT% GTR 1 ( echo second monitor connected ) else (echo only one monitor connected ))
del %TEMP%\monitors.txt /q /f >nul 2>nul
SET monitors=monitors.txt
SET nMons=0
MultiMonitorTool.exe /scomma "%monitors%"
FOR /F "skip=1 tokens=9 delims=," %%a IN (%monitors%) DO IF %%a GTR 0 SET /A nMons += 1
echo Number of monitors: %nMons%
MultiMonitorTool
Here is quick way to get connected monitors count via powershell
$m = Get-PnpDevice -Class Monitor -Status OK | measure; $m.Count
I have a batch file in my Startup folder. How Can I add a condition in the batch file that only execute the command if the system date is between 5th-25th of the current month and date is 6-11am.
I cant use task scheduler for this task.
Thanks in advance!
#echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
set "current-date=%%e-%%b-%%c"
set "current-time=%%d"
set "weekday=%%a"
)
del ~.*
popd
rem echo %current-time%
for /f "tokens=1,2,3 delims=-" %%a in ("%current-date% ") do (
set year=%%a
set mon=%%b
set day=%%c
)
echo %day%
for /f "tokens=1,2,3 delims=:" %%a in ("%current-time% ") do (
set hh=%%a
set mm=%%b
set ss=%%c
)
echo %hh%
if %day% LEQ 25 if %day% GEQ 5 if %hh% LEQ 11 if %hh% GEQ 6 (
echo execute my thing
)