I would like to run a batch only during a specific time of the day (range).
For instance the idea would be to have have it run from 07:00AM to 06:00PM.
I know that %TIME% returns current time, what's missing now is a way to check this value and if she's on the range provided the batch will execute, if not the batch will stop there.
This worked for me (Win7):
for /f "tokens=1-3 delims=:" %%a in ("%time%") do if %%a geq 7 if %%a leq 18 echo hello
You can change echo hello to anything like goto ok and the next two lines would be for example goto end and :ok.
Don't forget to paste it to a batch file or use %a instead of %%a if you try it directly in command prompt.
Related
Could you please help me with a command for batch file to delete the files with Name of the file.
I need to delete the files which are older than 6 months according to their File name. I need to schedule the script in Task scheduler, So it will delete the files in specific location every day which are older than 6 months from current date according to their file name.
Location of files:
K:\Bck
File Names:
Backup_2020-10-22.txt
Backup_2020-10-21.txt
Backup_2019-09-16.txt
Backup_2018-05-17.txt
Backup_2017-04-16.txt
Here is a modified version of Aacini's post, from back in 2013. I only amended it to use powershell to get today's date, instead of using the locale specific %date% variable and some changes to accomodate your environment. So I take no credit for this whatsoever.
#echo off
setlocal EnableDelayedExpansion
pushd "K:\Bck"
for /f "tokens=*" %%i in ('PowerShell -Command "& {Get-Date -format 'yyyyMMdd'}"') do set todayDate=%%i
call :DateToJDN %todayDate% todayJDN=
for /F "tokens=1-4* delims=_-" %%a in ('dir /B /A-D "Backup_*-*-*.txt"') do (
call :DateToJDN %%b%%c%%~nd fileJDN=
set /A daysOld=todayJDN - fileJDN
if !daysOld! gtr 180 (
echo File "%%a_%%b-%%c-%%d" is !daysOld! days old
del "%%a_%%b-%%c-%%d" /Q
)
)
popd
goto :eof
:DateToJDN yyyymmdd jdn=
set yyyymmdd=%1
set /A yyyy=%yyyymmdd:~0,4%, mm=1%yyyymmdd:~4,2% %% 100, dd=1%yyyymmdd:~6% %% 100
set /A a=(mm-14)/12, %2=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075
exit /B
This will NOT yet delete anything. It will simply echo the commands to the screen. If you are sure the echo'd results resemble what you want, you can remove the echo from the line echo del "%%a_%%b_%%c_%%d" /Q
You also need to set your days accordingly in the line if !daysOld! gtr 180 (
(TLDR see last paragraph)
Below code I have cobbled together with information from other threads in this forum and others. I am very new to this code/language so I don't have a clue how to optimize this.
Here is what I am trying to achieve with this code.
script outputs to a .log instead of command window
:top
script searches for a .dat file in "M:\AnalysisDrop\"
if a .dat file is detected (
script automatically creates a subfolder "M:\AnalysisDrop\title_date_time"
file is moved into the folder
file is opened with the program C:\Analysis\Analysis.exe
)
wait 5 seconds
goto top (repeat forever)
this script will continuously repeat for what I hope to be a very long time. Will I run into issues with too many "set" commands? I ran into some issue with doing "SETLOCAL EnableDelayedExpansion" and SETLOCAL DisableDelayedExpansion
#Echo off
echo Script 1 Initiated
#echo on
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set datetime1=%datetime:~0,8%_%datetime:~8,6%
SETLOCAL EnableDelayedExpansion
SET LOGFILE=Script1_LOG_%datetime1%.log
call :top >> %LOGFILE%
exit /b 0
:top
for %%f in (*.dat) do (
set datetime1=%datetime:~0,8%_%datetime:~8,6%
set foldername=%%~nf_%datetime1%
md "!foldername!"
move "M:\AnalysisDrop\%%~nf*.dat*" "M:\AnalysisDrop\%%~nf_%datetime1%\"
pushd M:\AnalysisDrop\%%~nf_%datetime1%\
C:\Analysis\Analysis.exe '%%~nf.dat'
popd
)
timeout /t 5 /nobreak
goto top
exit /b 0
I honestly dont need the "for" loop but i'm not sure how to do otherwise (tried to replace "for" with "if", no success. Also, the "datetime1" variable wont update, so if I try to run multiple .dat files of the same name, it writes over the previous one in the same subfolder, which I can live with but prefer not to have.
So to clarify my questions:
1. How do i do this without the "for" loop? I understand this may be causing my issue of the datetime variable not updating.
2. I used the "goto" command to continuously loop this script, should I use something different?
Answer to my question, with help from #compo. I wasn't aware that I needed the for \f... statement in the loop as well as the set datetime2. I needed to change %datetime...% to !datetime...! throughout the for loop. See below for fixed code.
In response to #Gerhard Barnard, %time% and %date% is not in a format that I can name directories with unfortunately.
#Echo off
echo Script 2 Initiated
#echo on
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set datetime2=%datetime:~0,8%_%datetime:~8,6%
SETLOCAL EnableDelayedExpansion
SET LOGFILE=Script2_LOG_%datetime2%.log
call :top >> %LOGFILE%
:top
for %%f in (*.dat) do (
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set datetime2=!datetime:~0,8!_!datetime:~8,6!
set foldername=%%~nf_!datetime2!
md "!foldername!"
move "M:\AnalysisDrop\%%~nf*.dat*" "M:\AnalysisDrop\!foldername!\"
pushd M:\AnalysisDrop\!foldername!\
C:\Analysis\Analysis.exe '%%~nf.dat'
popd
)
timeout /t 5 /nobreak
goto top
exit /b 0
I hope this, based upon your original code, helps you with structuring your script:
#Echo Off
Rem Enabling delayed expansion
SetLocal EnableDelayedExpansion
Rem Setting current directory
If /I Not "%CD%"=="M:\AnalysisDrop" (CD /D "M:\AnalysisDrop" 2>Nul||Exit/B)
Rem Setting date and time variable for log file name
Call :GetLDT
Rem Call Main section outputting to log file
Call :Main>>"Script1_LOG_%LDT%.log"
Rem Exit script
Exit/B
Rem Main section
:Main
Rem Loop over each matching file in the current directory
For %%A In (*.dat) Do (
Rem Setting date and time variable for destination directory name
Call :GetLDT
Rem Create directory if it does not exist
If Not Exist "%%~nA_!LDT!\" MD "%%~nA_!LDT!"
Rem Move file to directory
Move "%%~A" "%%~nA_!LDT!"
Rem Run analysis against file in destination
Start "" /D "%%~nA_!LDT!" /W "C:\Analysis\Analysis.exe" '%%~A'
)
Rem Create a delay
Timeout 5 /NoBreak>Nul
Rem Re-run Main section in loop
GoTo Main
Rem GetLDT section
:GetLDT
Rem Setting date and time string
For /F "EOL=L" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "LDT=%%B"
Rem Formatting string as required
Set "LDT=%LDT:~,8%_%LDT:~-6%"
Rem Return to point after Call
GoTo :EOF
Your command using '%%~A' looks wrong with single quotes, but I'll leave that to you to decide.
Essentially what I want is to get the the user that's currently using a computer. I got the below code from this community which works well...,
::Batch 1
#echo off
for /f %%a in (output1.txt) do WMIC /NODE:%%a computersystem GET name, username
do echo %%r >> output2.txt
..., except that my computer names have hyphens in them and cannot be used.
To circumvent that, I run the below code (also got this here, thanks again!) to retrieve the IPs first:
::Batch 2
#echo off
for /f %%a in (hosts.txt) do call :process %%a
goto :eof
:process
set hostname=%1
for /f "tokens=4 delims=: " %%r in ('ping -n 1 %hostname%^|find /i "Statistics"') do echo %%r >> output2.txt
I then feed this information to the first batch file above to get the hostnames.
So essentially I place my hostnames on a txt file named Hosts.txt, run batch 2, then run batch 1.
I've tried many days to combine the both, but cant seem to figure it out.
As #aschipfl suggested. Use FOR /? to see information about using the FOR command.
for /f %%a in (output1.txt) do (
WMIC /NODE:%%a computersystem GET name, username
ECHO right here
)
I am trying create a batch file which pings a particular host and moves the results to a file with the current time stamp for every hour. I was able to do it continuously, but the I want to know whether there is any script with which I can set the interval for which it runs the ping command.
I am successfully able to print the time stamp, ping response and the able to change the name of the file with current time stamp, but it's happening continuously. I want to do it for a time interval like say for 4hrs continuous ping then move the results to a file.
please help
There is no sane way to do this with a command script only.
What I recommend is to write a script that performs the requested action once.
Use Windows Task Scheduler to call the script every hour.
What I've attempted is by NO means an ideal, much less precise method of doing what you desire (purely in batch), however with some tweaking and refining, or by serving as a potential route/template for similar scripts by more experienced batch scripters, I believe it may be useful:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2 delims=:" %%i in ('echo %time%') do (
set /a hour=%%i+4
set /a minute=%%j
set expected=!hour!:!minute!
echo !expected!
)
for /f "tokens=*" %%i in ('ping 192.168.1.1 -n 1') do (echo %%i>pinglog.txt && goto PING)
:PING
for /f "tokens=1-2 delims=:" %%i in ('echo %time%') do (
set current=%%i:%%j
echo !current!
)
if !current!==!expected! (
goto EOF
) else (
for /f "tokens=* skip=2" %%i in ('ping 192.168.1.1 -n 2 -w 30000') do (
echo %date% %time% %%i>>pinglog.txt && goto PING
)
)
Control the hour or minute interval by adding the relevant value to either, in the lines:
set /a hour=%%i+4 rem Here I've added 4 hours
set /a minute=%%j
Remove the echo !expected! and echo !current! lines if you don't wish to see the temporal progression in the cmd output, which can be minimised while the script is running.
I am relatively new to Batch and am having problems trying to find a string value within a txt file. I am using a neat program called CCExtactor to extract closed captioning from a file and need to grab the time of the closed captioning.
The output from CCExtractor looks like this:
###SUBTITLE#08:37#08:40#She ran away
My batch script looks like this:
for /F "delims=" %%a in (subtitle.txt) do ( echo %1|findstr /R /C:"^[^0-9][0-9]*$" )
I can't seem to get this to work! I am trying to skim the time values!
How would I go about doing this??
Here's an example.
#echo off
setlocal
set filename=subtitle.txt
for /f "delims=# tokens=2,3" %%I in ('findstr /r /c:"^###SUBTITLE#[0-9][0-9]:[0-9][0-9]#[0-9][0-9]:[0-9][0-9]#" %filename%') do (
echo start=%%I, end=%%J
)
In your for statement, you should make the delims value to be #. That way you can echo out %%d and %%e to get the start time and end time.