I have a batch file that create another batch file.
I need to add inside the echo a random function to have numbers from 1 to 6.
My batch code (works perfectly)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::: Batch Code :::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#echo off
(for /f "usebackq delims=" %%a in ("D:\Program files\Openvpn\openvpn_configuration_list_for_clicks.csv") do (
echo :::: Start Of The Command Block ::::
echo/
echo MOVE /Y "D:\Program files\Openvpn\config_to_check\%%~NXa" "D:\Program files\Openvpn\config\"
echo START "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect %%~NXa
echo PING -n 30 localhost ^>NUL 2^>^&1
echo PING -n 5 www.wikipedia.org^|FIND /I "TTL"^>NUL
echo IF NOT "%%ERRORLEVEL%%"=="1" ^(
echo rundll32 user32.dll,MessageBeep 0x00000010L
echo start "" "D:\Program files\Firefox Esr\FirefoxPortable.exe"
echo TIMEOUT /T 60 /NOBREAK ^>NUL
echo goto search_%%a
echo ^)
echo :search_%%a
echo tasklist /FI "IMAGENAME eq firefox.exe" 2^>NUL ^| find /I /N "firefox.exe"^>NUL
echo if "%%ERRORLEVEL%%"=="0" ^(
echo TIMEOUT /T 60 /NOBREAK ^>NUL
echo goto search_%%a
echo ^)
echo if "%%ERRORLEVEL%%"=="1" ^(goto continue_%%a^)
echo :continue_%%a
echo taskkill.exe /F /IM openvpn.exe
echo taskkill.exe /F /IM openvpn-gui.exe
echo MOVE /Y "D:\Program files\Openvpn\config\%%~NXa" "D:\Program files\Openvpn\config_to_check\"
echo/
echo :::: End Of The Command Block ::::
echo/
echo/
echo/
)) > "D:\Program files\Openvpn\final.bat"
(echo exit) >> "D:\Program files\Openvpn\final.bat"
openvpn_configuration_list_for_clicks.csv (list of vpn)
vpn1.ovpn
vpn2.ovpn
vpn4.ovpn
vpn8.ovpn
output final.bat without random function (works perfectly)
:::: Start Of The Command Block ::::
MOVE /Y "D:\Program Files\Openvpn\config_to_check\vpn1.ovpn" "D:\Program Files\Openvpn\config\"
START "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect vpn1.ovpn
PING -n 30 localhost >NUL 2>&1
PING -n 5 www.wikipedia.org|FIND /I "TTL">NUL
IF NOT "%ERRORLEVEL%"=="1" (
rundll32 user32.dll,MessageBeep 0x00000010L
start "" "D:\Program Files\Firefox Esr\FirefoxPortable.exe"
TIMEOUT /T 60 /NOBREAK >NUL
goto search_vpn1.ovpn
)
:search_vpn1.ovpn
tasklist /FI "IMAGENAME eq firefox.exe" 2>NUL | find /I /N "firefox.exe">NUL
if "%ERRORLEVEL%"=="0" (
TIMEOUT /T 60 /NOBREAK >NUL
goto search_vpn1.ovpn
)
if "%ERRORLEVEL%"=="1" (goto continue_vpn1.ovpn)
:continue_vpn1.ovpn
taskkill.exe /F /IM openvpn.exe
taskkill.exe /F /IM openvpn-gui.exe
MOVE /Y "D:\Program Files\Openvpn\config\vpn1.ovpn" "D:\Program Files\Openvpn\config_to_check\"
:::: End Of The Command Block ::::
I need a solution to have a random number from 1 to 6 in path inside echo
"D:\Program Files\Firefox Esr 2\FirefoxPortable.exe"
or
"D:\Program Files\Firefox Esr 5\FirefoxPortable.exe"
or
"D:\Program Files\Firefox Esr 6\FirefoxPortable.exe"
The code without a random function works very well and generates output correctly, I need to insert a random function inside the code that does not send the output generation into error.
The random function need to calculate for every loop to have a new random number for every loop
Let me step through my comment with some examples.
Your initial code was escaping opening parentheses unnecessarily and should have looked more like this:
Echo :::: Start Of The Command Block ::::
Echo/
Echo Ping -n 30 localhost ^>Nul 2^>^&1
Echo Ping -n 5 www.facebook.com^|Find /I "TTL"^>Nul
Echo If Not "%%ErrorLevel%%"=="1" (
Echo Rundll32 User32.dll,MessageBeep 0x00000010L
Echo Set /A num=(%%RANDOM%% %%%% 6^^^) + 1
Echo Start "" "D:\Program Files\Firefox Portable %%num%%\FirefoxPortable.exe"
Echo Timeout 60 /NoBreak ^>Nul
Echo ^)
Echo/
Echo :search
However, you are setting a variable and trying to use it within the same If block, this can be fixed in a number of ways:
Use a pseudo Call statement:
Echo :::: Start Of The Command Block ::::
Echo/
Echo Ping -n 30 localhost ^>Nul 2^>^&1
Echo Ping -n 5 www.facebook.com^|Find /I "TTL"^>Nul
Echo If Not "%%ErrorLevel%%"=="1" (
Echo Rundll32 User32.dll,MessageBeep 0x00000010L
Echo Set /A num=(%%RANDOM%% %%%% 6^^^) + 1
Echo Call Start "" "D:\Program Files\Firefox Portable %%%%num%%%%\FirefoxPortable.exe"
Echo Timeout 60 /NoBreak ^>Nul
Echo ^)
Echo/
Echo :search
Enable delayed expansion: (this example assumes that delayed expansion isn't already enabled in the script doing the Echoing)
Echo :::: Start Of The Command Block ::::
Echo/
Echo Ping -n 30 localhost ^>Nul 2^>^&1
Echo Ping -n 5 www.facebook.com^|Find /I "TTL"^>Nul
Echo If Not "%%ErrorLevel%%"=="1" (
Echo Rundll32 User32.dll,MessageBeep 0x00000010L
Echo Set /A num=(%%RANDOM%% %%%% 6^^^) + 1
Echo SetLocal EnableDelayedExpansion
Echo Start "" "D:\Program Files\Firefox Portable !num!\FirefoxPortable.exe"
Echo EndLocal
Echo Timeout 60 /NoBreak ^>Nul
Echo ^)
Echo/
Echo :search
Enable delayed expansion: (this example assumes that delayed expansion is already enabled in the script doing the Echoing)
Echo :::: Start Of The Command Block ::::
Echo/
Echo Ping -n 30 localhost ^>Nul 2^>^&1
Echo Ping -n 5 www.facebook.com^|Find /I "TTL"^>Nul
Echo If Not "%%ErrorLevel%%"=="1" (
Echo Rundll32 User32.dll,MessageBeep 0x00000010L
Echo Set /A num=(%%RANDOM%% %%%% 6^^^) + 1
Echo SetLocal EnableDelayedExpansion
Echo Start "" "D:\Program Files\Firefox Portable ^!num^!\FirefoxPortable.exe"
Echo EndLocal
Echo Timeout 60 /NoBreak ^>Nul
Echo ^)
Echo/
Echo :search
Restructure the code in order not to have an unnecessary If block (preferred):
Echo :::: Start Of The Command Block ::::
Echo/
Echo Ping -n 30 localhost ^>Nul 2^>^&1
Echo Ping -n 5 www.facebook.com^|Find /I "TTL"^>Nul
Echo If "%%ErrorLevel%%"=="1" GoTo search
Echo Rundll32 User32.dll,MessageBeep 0x00000010L
Echo Set /A num=(%%RANDOM%% %%%% 6^) + 1
Echo Start "" "D:\Program Files\Firefox Portable %%num%%\FirefoxPortable.exe"
Echo Timeout 60 /NoBreak ^>Nul
Echo/
Echo :search
All of the above examples assume that the code shown is inside a parenthesised block, similar to (code above)>"another.bat" or >"another.cmd" (code above)
[Edit /]
Here's some code to incorporate the additional stuff you've now posted. I have utilised methd 4. from above to remove the unnecessary If blocks. It should also incorporate the randomisation function you require, which is the same as previously posted and compatible with your batch file.
#Echo Off
(For /F "UseBackQ Delims=" %%A In (
"D:\Program Files\OpenVPN\openvpn_configuration_list_for_clicks.csv") Do (
Echo #Move /Y "D:\Program Files\OpenVPN\config_to_check\%%~nxA" "D:\Program Files\OpenVPN\config"
Echo #Start "" "C:\Program Files\OpenVPN\bin\OpenVPN-GUI.exe" --connect %%~nxA
Echo #Timeout 30 /NoBreak^>Nul 2^>^&1
Echo #Ping -n 5 www.wikipedia.org^|Find /I "TTL"^>Nul^|^|GoTo search_%%A
Echo #Rundll32 User32.dll,MessageBeep 0x00000010L
Echo #Set "num="
Echo #Set /A num=(%%RANDOM%% %%%% 6^) + 1
Echo #Start "" "D:\Program Files\Firefox ESR %%num%%\FirefoxPortable.exe"
Echo #Timeout 60 /NoBreak^>Nul
Echo/
Echo :search_%%A
Echo #TaskList^|Find /I "Firefox.exe"^>Nul^|^|GoTo continue_%%A
Echo #Timeout 60 /NoBreak^>Nul
Echo #GoTo search_%%A
Echo/
Echo :continue_%%A
Echo #TaskKill /F /IM OpenVPN.exe 2^>Nul
Echo #TaskKill /F /IM OpenVPN-GUI.exe 2^>Nul
Echo #Move /Y "D:\Program Files\OpenVPN\config\%%~nxA" "D:\Program Files\OpenVPN\config_to_check"
Echo/))>"final.bat"
Echo #Exit /B>>"D:\Program Files\OpenVPN\final.bat"
Related
I'm pretty new to this so please bear with me, and if you require anymore information from me please say. Thanks in advance for your help.
I have this code that pings different PCs then returns back if they are online/offline. I wanted to know if you could add another column once the bat file has ran its ping test so it has the computer name next to it.
#echo off
if exist C:\tools\computers.txt goto Label1
echo.
echo Cannot find C:\tools\computers.txt
echo.
Pause
goto :eof
:Label1
echo PingTest executed on %date% at %time% > C:\tools\z.txt
echo ================================================= >> C:\tools\z.txt
for /f %%i in (C:\tools\computers.txt) do call :Sub %%i notepad C:\tools\z.txt
goto :eof
:Sub
echo Testing %1 set state=alive ping -n 1 %1 | find /i "bytes=" || set state=dead echo %1 is %state% >> C:\tools\z.txt
The bat file creates a document that shows the following;
PingTest executed on 28/07/2016 at 13:10:28
99.1.82.28 is alive
99.1.82.100 is alive
ect.
If possible I would like the bat file to run so it displays this;
The bat file creates a document that shows the following;
PingTest executed on 28/07/2016 at 13:10:28
Computer 1 : 99.1.82.28 is alive
Computer 2 : 99.1.82.100 is alive
ect.
--
Would appreciate any help & guidance on this.
Thanks.
You can try this solution :
#echo off
Title Ping Test
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
ping -n 1 %%a>nul && set "msg=%%a : !ip! ALive ok" || set "msg=%%a : !ip! Dead failed to respond"
echo !msg!
echo !msg! >> %LogFile%
)
)
EndLocal
Start "" %LogFile%
pause>nul & exit
EDIT : on 29/07/2016 # 12:48
Another version with multi-colors : Special thanks goes to ICARUS for the color function (-_°)
#echo off
Rem Special thanks goes to Iracus for the color function (-_°)
mode con cols=60 lines=20
Title Multi-Ping hosts Tester with Multi-colors by Hackoo
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
call :init
echo(
call :color 0E "------- Ping Status of Computers hosts -------" 1
echo(
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
ping -n 1 %%a>nul && set "msg=%%a - !ip! ALive ok" && Call :Color 0A "!msg!" 1 || set "msg=%%a - !ip! Dead failed to respond" && Call :Color 0C "!msg!" 1
echo !msg! >> %LogFile%
)
)
EndLocal
Start "" %LogFile%
pause>nul & exit
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
<nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
EDIT : Update On 23/08/2016
http://pastebin.com/zjYwSqUM
My batch-file program always crashes at the same point. What always happens before it crashes is this:
ping -n 6 127.0.0.1 1>nul: 2>nul:
-n cant be processed syntactically at this point.
if ping -n 1 127.0.0.1|find "(0" >nul && goto Loop
And then the window closes.
Could maybe someone help me fix the problem?
This is the whole script:
#setlocal enableextensions enabledelayedexpansion
:Loop
set ipaddr=127.0.0.1
ping -n 6 %ipaddr% >nul: 2>nul:
if ping -n 1 %ipaddr%|find "(0%" >nul && goto Loop
echo Connection lost
start "" http://www.google.com
else if ping -n 1 %ipaddr%|find "(100%" >nul && goto Loop
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*" goto Loop
endlocal
Here is an advanced Version with two blocks of code that are executed dependent of whether Connection is ok or lost.
#setlocal enableextensions enabledelayedexpansion
set ipaddr=127.0.0.1
:Loop
ping -n 6 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && (
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*"
) || (
echo Connection lost
tasklist /v /fi "Windowtitle eq google*" || start "" http://www.google.com
)
goto :Loop
This is the "conventional way" with %errorlevel% and if- else (same logic, the above is just a shorter way of doing it):
#setlocal enableextensions enabledelayedexpansion
set ipaddr=127.0.0.1
:Loop
ping -n 6 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul
if %errorlevel%==0 (
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*"
) else (
echo Connection lost
tasklist /v /fi "Windowtitle eq google*" || start "" http://www.google.com
)
goto :Loop
I took set ipaddr... out of the Loop. No Need to do it again and again.
(just wondering, if Google would be reachable, if the Connection got lost...)
EDIT to reflect the last comment. A bit enhanced, so any action only happens, if the connection status changes. Delete the "log" function if you don't need it, or redirect them to a file, if you like.
#echo off
setlocal enabledelayedexpansion
set ipaddr=127.0.0.1
set "status=undefined"
:Loop
ping -n 2 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && (
set oldstatus=!status!
set status=online
if !status! neq !oldstatus! (
echo %date% %time% Connection switched from !oldstatus! to !status!
taskkill /FI "WINDOWTITLE eq google*" >nul 2>&1
)
) || (
set oldstatus=!status!
set status=offline
if !status! neq !oldstatus! (
echo %date% %time% Connection switched from !oldstatus! to !status!
start "" http://www.google.com
)
)
goto :Loop
I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.
In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.
All I want is like this:
Directory A:
1. A.txt
2. B.txt
3. list.txt (By line sequential with filename want to move)
4. process.bat
Directory B:
None of files (Then move a file by order of line set in list.txt) OR
A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
Process.bat
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing %name%.txt now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer %name%.txt completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)
:eof
Please guide me to finish the script by using a while-loop method. Thanks
I change some script sequence and it works now.
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
set name=%*
if exist C:\Test2\*.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo Previous job did not finished yet. >> C:\Test2\Log.txt
Timeout /t 5
echo.
echo. >> C:\Test2\Log.txt
goto :processline
)
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing %name%.txt now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer %name%.txt completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)
:eof
This will copy as well as count the number of lines from your text file..
# echo off
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :
:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit
:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath
I am currently trying to ping a list of hostnames, preferably one at a time, and then use nslookup on that hostname. If the hostname in nslookup matches up with the hostname first used, then I would like to use the IP to check against another file (called home.txt) that would contain the location.
What I have so far:
#Echo Off
If '%1'=='' GOTO Syntax
Echo Running Script and Saving Results to Results.CSV
Echo Script Run %date% %time% >> Results.csv
For /F %%i in (%1) do Call :StartPing %%i
Goto :eof
:StartPing
PING %1 -n 1 | FIND /i "TTL" > nul && goto Success
PING %1 -n 1 | FIND /i "timed" > nul && goto Timedout
PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg
:Success
for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a
set IPAddress=%Address::=%
echo %1, %IPAddress%,%Hostname%
echo %1, %IPAddress%,%Hostname% >> Results.csv
NSLOOKUP %IPAddress% | FIND /i "Name" = "%Hostname%" goto home
:home
echo %IPAddress% home.txt
Goto :EOF
:Timedout
Echo %1, Request timed out.
Echo %1, Request timed out. >> Results.csv
:ErrorMsg
Echo %1, Ping request could not find host.
Echo %1, Ping request could not find host. >> Results.csv
goto :eof
:Syntax
echo . . .
goto :eof
An example of what home.txt might contain:
10.102.6.43 = 2J
IE, just a bunch of IP ranges mapped to office locations.
Ideally, the script should then make a popup box showing the location of the IP address, or just echo it on-screen.
Any ideas?
Made some changes and tested a few things, here is what i have so far.
#echo Off
#cls
if '%1'=='' GOTO Syntax
echo Running Script and Saving Results to Results.CSV
echo Script Run %date% %time% >> Results.csv
for /F %%i in (%1) do Call :StartPing %%i
goto :EOF
:StartPing
PING %1 -n 1| FIND /i "TTL" > nul && goto Success
PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg
:Success
for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a
set IPAddress=%Address::=%
for /f "tokens=2" %%b in ('nslookup %IPAddress%^|find /i "Name"') do set fqdn=%%b
echo %1, %IPAddress%,%Hostname%
echo %1, %IPAddress%,%Hostname% >> Results.csv
goto :EOF
:Timedout
Echo %1, Request timed out.
Echo %1, Request timed out. >> Results.csv
:ErrorMsg
Echo %1, Ping request could not find host.
Echo %1, Ping request could not find host. >> Results.csv
goto :EOF
:Syntax
echo . . .
goto :EOF
:EOF
echo this is the END OF FILE
pause
Every time I run hh.bat host.txt, everything runs fine until it hits the nslookup part, then it creates between 1500 and 3000 processes in windows task manager. without the NSLOOKUP part hh.bat works fine.
I had the nslookup part in a separate script called nslookup.bat which worked fine for a little while, and now it does not want to work. create that amount of processes everytime...
testing in a Hyper-V environment with 3 windows 7 pcs and a DC.
I keep getting a Missing operand error. I cant seem to find it and I figure a new set of eyes can.
#echo off
title Log Split And Backup
rem dt = date
rem tm = time
rem wd = week day
rem mh = month
rem dy = day
rem yr = year
rem hh = hour
rem mm = minute
rem ss = second
rem ms = milisecond
rem gtr = greater than
rem lss = less than
rem equ = equal to
echo Starting Log Split And Backup...
ping 1.1.1.1 -n 1 -w 1500 >nul
cls
echo Log Split And Backup Has Started...
:time
set hh=%tm:~0,2%
set mm=%tm:~3,2%
set ss=%tm:~5,2%
set ms=%tm:~7,2%
goto date
:date
set dt=%date%
set tm=%time%
set wd=%dt:~0,3%
set mh=%dt:~4,2%
set dy=%dt:~6,2%
set yr=%dt:~8,4%
goto scheduletimes
:scheduletimes
:hour1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 6 goto minutes1
goto hour2
:minutes1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 0 goto seconds1
goto hour2
:seconds1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% lss 10 goto day1
goto hour2
:hour2
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 18 goto minutes2
goto time
:minutes2
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 0 goto seconds2
goto time
:seconds2
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% lss 10 goto day2
goto time
:days
:day1
for %%i in (Mon) do (
if "%wd%"=="%%i" goto mon1
)
for %%i in (Tue) do (
if "%wd%"=="%%i" goto tue1
)
for %%i in (Wed) do (
if "%wd%"=="%%i" goto wed1
)
for %%i in (Thu) do (
if "%wd%"=="%%i" goto thu1
)
for %%i in (Fri) do (
if "%wd%"=="%%i" goto fri1
)
for %%i in (Sat) do (
if "%wd%"=="%%i" goto sat1
)
for %%i in (Sun) do (
if "%wd%"=="%%i" goto sun1
)
:day2
for %%i in (Mon) do (
if "%wd%"=="%%i" goto mon2
)
for %%i in (Tue) do (
if "%wd%"=="%%i" goto tue2
)
for %%i in (Wed) do (
if "%wd%"=="%%i" goto wed2
)
for %%i in (Thu) do (
if "%wd%"=="%%i" goto thu2
)
for %%i in (Fri) do (
if "%wd%"=="%%i" goto fri2
)
for %%i in (Sat) do (
if "%wd%"=="%%i" goto sat2
)
for %%i in (Sun) do (
if "%wd%"=="%%i" goto sun2
)
:logsplitting
:mon1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\0-mon\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:tue1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\1-tue\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:wed1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\2-wed\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:thu1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\3-thu\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:fri1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\4-fri\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:sat1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\5-sat\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:sun1
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\6-sun\server1.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:mon2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\0-mon\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:tue2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\1-tue\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:wed2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\2-wed\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:thu2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\3-thu\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:fri2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\4-fri\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:sat2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\5-sat\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
:sun2
#echo %date%
#echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping 1.1.1.1 -n 1 -w 3000 >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\6-sun\server2.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
Thank you to anyone that looked through this entire code and especially thank you to the person that finds the error for me.
If the above code is your complete program, then tm variable has not been assigned before it is used. I think that this line:
set tm=%time%
needs to be moved below :time label...
In essence this code should replace your repetitive code, and also provide a set of robust time and date variables (zero padded) using Wmic (XP Pro and later).
This uses the variables %var% and %server% in the xcopy routine.
Your :scheduletimes routine looks wacky because you seem to be setting and resetting a variable called %hh% and are testing the same variable again and again.
If you provide information about the schedule times then that may be simplified too.
#echo off
title Log Split And Backup
rem wd = week day
rem mh = month
rem dy = day
rem yr = year
rem hh = hour
rem mm = minute
rem ss = second
rem ms = milisecond
rem gtr = greater than
rem lss = less than
rem equ = equal to
echo Starting Log Split And Backup...
ping -n 1 localhost >nul
cls
echo Log Split And Backup Has Started...
:time
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set Yr=%dt:~0,4%
set Mh=%dt:~4,2%
set Dy=%dt:~6,2%
set HH=%dt:~8,2%
set MM=%dt:~10,2%
set SS=%dt:~12,2%
set MS=%dt:~15,2%
:date
set dt=%date%
set wd=%dt:~0,3%
:scheduletimes
:hour1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 6 goto minutes1
goto hour2
:minutes1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 0 goto seconds1
goto hour2
:seconds1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% lss 10 set server=server1
goto hour2
:hour2
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 18 goto minutes2
goto time
:minutes2
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% equ 0 goto seconds2
goto time
:seconds2
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a HH=%%b
if %HH% lss 10 set server=server2
if "%wd%"=="MON" set var=0-%wd%
if "%wd%"=="TUE" set var=1-%wd%
if "%wd%"=="WED" set var=2-%wd%
if "%wd%"=="THU" set var=3-%wd%
if "%wd%"=="FRI" set var=4-%wd%
if "%wd%"=="SAT" set var=5-%wd%
if "%wd%"=="SUN" set var=6-%wd%
:logsplitting
echo %date%
echo %time%
echo Starting the 12hr Log Split and Log Backup...
ping -n 1 localhost >nul
xcopy "C:\Users\Kratos\Desktop\MC Server\server.txt" "C:\Users\Kratos\Dropbox\Minecraft Backups\Logs\%var%\%server%.txt" /D /E /R /I /K /Y
del "C:\Users\Kratos\Desktop\MC Server\server.txt"
echo Finished Log Split and Log Backup...
goto time
Well, you can see your error by using debug.
Please do as following:
> bash -x script1.sh
Please look at the following url for more detail.
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html