I have been having the hardest time with this FOR loop. I thought it was working the other day, but when I went to use it today, it didn't. This is part of a much larger script, but this is the only portion that is hanging me up. Also, to note, I did not write the majority of the script. It was given to me, and I have had to modify it to fit my needs. Can someone take a look and let me what I am missing.
IF NOT EXIST C:\Paytronix\config\deploytool\terminals.xml (
copy terminals.xml_template terminals.xml > temp.txt
set MAKETERMXML=1
) ELSE (
set MAKETERMXML=0
)
echo Create terminals.xml = %MAKETERMXML%
PING localhost -n 5 >NUL
for /l %%T in (1,1,%TERMINALS%) do (
echo Trying terminal %TERMSTRING%%%T
PING localhost -n 5 >NUL
REM net use /user:USERNAME /PERSISTENT:NO P: \\%TERMSTRING%%%T\C$ PASSWORD
net use P: \\%TERMSTRING%%%T\C$ /USER:%USR% %PWRD% /PERSISTENT:NO > temp.txt
REM /USER:username password (Insert after C$
REM net use P: \\%TERMSTRING%%%T\C$ /PERSISTENT:NO > temp.txt
setlocal EnableDelayedExpansion
echo Copying Installer Shortcut to Startup Folder
IF EXIST "\\%TERMSTRING%%%T\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" (
copy aaaPxAlohaUiInstaller.lnk "\\%TERMSTRING%%%T\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" > result.txt
) ELSE (
copy aaaPxAlohaUiInstaller.lnk "\\%TERMSTRING%%%T\C$\Documents and Settings\All Users\Start Menu\Programs\Startup" > result.txt
)
PING localhost -n 5 >NUL
find /c "1" result.txt > copyresult.txt
FOR /F "tokens=1,2,3" %%a in (copyresult.txt) do set COPYTAG=%%c
echo Copy Result = !COPYTAG!
PING localhost -n 5 >NUL
IF !COPYTAG!==1 (
echo SUCCESS !!! Valid Terminal %TERMSTRING%%%T > C:\Paytronix\Paytronix.bd
echo SUCCESS !!! Valid Terminal %TERMSTRING%%%T
echo Updating PxInst folder
IF NOT EXIST "\\%TERMSTRING%%%T\C$\PxInst\" mkdir \\%TERMSTRING%%%T\C$\PxInst > temp.txt
REM Delete PxInst contents
del /F /S /Q "\\%TERMSTRING%%%T\C$\PxInst\*" > temp.txt
REM Create PxAlohaUiInstaller folder
IF NOT EXIST "\\%TERMSTRING%%%T\C$\PxInst\PxAlohaUiInstaller" mkdir \\%TERMSTRING%%%T\C$\PxInst\PxAlohaUiInstaller > temp.txt
REM Update user program installer
copy /Y C:\Paytronix\PxAlohaUiInstaller\PxAlohaUiInstaller.exe \\%TERMSTRING%%%T\C$\PxInst\PxAlohaUiInstaller > temp.txt
REM Copy PxAlohaUiInstaller done
PING localhost -n 5 >NUL
REM Create user program dir if missing
IF NOT EXIST "\\%TERMSTRING%%%T\C$\Paytronix" (
echo Creating User Program folder
mkdir \\%TERMSTRING%%%T\C$\Paytronix\config\userprog > temp.txt
copy C:\Paytronix\config\userprog\pxalohaui.cfg \\%TERMSTRING%%%T\C$\Paytronix\config\userprog\ > temp.txt
)
::echo
IF %MAKETERMXML%==1 (
echo ^<Terminal^> >> terminals.xml
echo ^<Name^>term%%T^</Name^> >> terminals.xml
echo ^<Path^>\\%TERMSTRING%%%T\C$^</Path^> >> terminals.xml
echo ^<Address^>%TERMSTRING%%%T^</Address^> >> terminals.xml
echo ^</Terminal^> >> terminals.xml
)
)
endlocal
net use P: /DELETE /YES > temp.txt
del result.txt
del copyresult.txt
del temp.txt
)
echo %MAKETERMXML%
IF %MAKETERMXML%==1 (
echo ^</TerminalList^> >> terminals.xml
echo ^</PaytronixConfig^> >> terminals.xml
copy terminals.xml C:\Paytronix\config\deploytool\terminals.xml
)
Related
#echo off
set source_file=C:\TEMP\CopyFile\TestFileToCopy.txt
set ip_list=C:\TEMP\PCList\PCList.txt
set log_file=C:\TEMP\Logs\copy_log.txt
for /F "usebackq tokens=*" %%i in (%ip_list%)
do (set destination=\%%i\c$\TEMP echo Checking availability of %%i...ping -n 1 %%i | find "TTL=" > nul
if errorlevel 1 (echo %%i is unavailable. >> %log_file%)
else (robocopy %source_file% %destination% /E
if errorlevel 0 ( echo File copy to %%i completed successfully. >> %log_file% )
else (echo File copy to %%i failed. >> %log_file%)
)
)
echo File copy complete. Check %log_file% for results.
pause
I try to make the above code to work, but a cmd window opens and the notepad window with PCList.txt open and nothing happens. I want to copy a file to many remote computers and check with ping if the pc is live.
I was making a .bat code to send .txt files to a specific folder on my ftp server. But I also wanted a way to check if these files were really uploaded. I searched a lot on the internet and unfortunately, I realized that it can't be done using .bat command.
So I tried using another way: The .bat will send the files and then will take it back to the folder, if the file already exists in the folder it will show a message that the file was successfully uploaded.
I did this script below, but the part of the "checking if was uploaded" is not working right.
Someone can help me?
#echo off
#setlocal enableextensions
#cd /d "%~dp0"
mode 34,12
color 0a
Ping www.google.nl -n 1 -w 1000 >nul 2>nul
if errorlevel 1 (set internet=Nao foi possivel se conectar ao servidor) else (set internet=Conectado) >nul 2>nul
echo %internet%
if "%internet%"=="No connection" goto 1
if "%internet%"=="Conected" goto 2
:1
echo No connection
echo.
echo Try later...
echo.
pause
exit
:2
( echo open ftp.xxxxxxxxxxx.com
echo xxxxxxx
echo xxxxxxx
echo ascii
echo lcd "c:\Vendas Pay&Go\files"
echo cd "Vendas Cartões Pay&Go"
echo cd "ECO"
echo mput *.txt
echo bye
)> %temp%\ftpsend.dat
ftp -i -s:%temp%/ftpsend.dat >nul 2>nul
del /f /s /q %temp%\ftpsend.dat >nul 2>nul
( echo open ftp.xxxxxxx.com
echo xxxxxxx
echo xxxxxxx
echo ascii
echo lcd "c:\Vendas Pay&Go\files"
echo cd "Vendas Cartões Pay&Go"
echo cd "ECO"
echo mget *.txt
echo bye
)> %temp%\ftpsend.dat
ftp -i -s:%temp%/ftpsend2.dat >nul 2>nul
del /f /s /q %temp%\ftpsend2.dat >nul 2>nul
if %*.txt% exist goto3
:4
echo File was not uploaded
pause
:3
echo File Uploaded.
del /s /f /q "c:\Vendas Pay&Go\files\*.txt"
If you want to conditional execution to display a message you can do something like this. The && means the previous command was successful. The || means the previous command was not successful.
(ftp -i -s:%temp%/ftpsend.dat | find /I "file successfully transferred" >nul) && (echo File Successfully Transferred) || ( echo File not transferred. Tray again later.)
Updated version based on comment below
(ftp -i -s:%temp%/ftpsend.dat | find /I "file successfully transferred" >nul) && (
echo File successfully sent.
del /f /s /q "c:\Vendas Pay&Go\files\*.txt" >nul
) || (
cls
echo File not uploaded. Try later.
)
I am new to bat script. I want below script to check if web is up and if not restart the coldfusion and save all info into log file.
#REM Remove any existing files.
del C:\Users\Administrator\check.txt
#REM to download file
wget -O check.txt --no-check-certificate https://10.2.3.30/ebanking/ch/en-en/index.cfm
#REM Search for the term in the previously downloaded file.
FINDSTR /C:"index.cfm" check.txt
if errorlevel equ 1 notfound
found
cls
:notfound
echo notfound > log.txt
echo stopping ebanking >> log.txt
call C:\ColdFusion10\cfusion\bin\cfstop.bat
echo starting ebanking >> log.txt
call C:\ColdFusion10\cfusion\bin\cfstart.bat
cls
exit
:found
echo found >> log.txt
cls
exit
You missed goto command for the label notfound. For logic, I swapped label between notfound/found
#echo off
pushd "C:\Users\Administrator"
#REM Remove any existing files.
del check.txt
#REM to download file
wget -O check.txt --no-check-certificate https://10.2.3.30/ebanking/ch/en-en/index.cfm
#REM Search for the term in the previously downloaded file.
FINDSTR /C:"index.cfm" check.txt
if errorlevel equ 1 (goto notfound)
:found
echo found >> log.txt
exit /b 0
:notfound
echo notfound > log.txt
echo stopping ebanking >> log.txt
call C:\ColdFusion10\cfusion\bin\cfstop.bat
echo starting ebanking >> log.txt
call C:\ColdFusion10\cfusion\bin\cfstart.bat
exit
you could simplify the code to the following in a loop
#echo off
set "someurl=https://10.2.3.30/ebanking/ch/en-en/index.cfm"
:loop
wget --server-response -nv --spider %someurl% 2>&1 | find "HTTP/1.1 200 OK">nul
if errorlevel 1 (
echo %DATE% %TIME%: stopping ebanking>> log.txt
call C:\ColdFusion10\cfusion\bin\cfstop.bat
timeout 5>nul
echo %DATE% %TIME%: starting ebanking>> log.txt
call C:\ColdFusion10\cfusion\bin\cfstart.bat
) else (
echo %DATE% %TIME%: site is up>> log.txt
)
rem Wait 1 hour before loop
timeout 3600
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
It is very late, and I'm tired at looking at this. Could someone explain where I might have bad syntax in this script?
The script looks to see if an old installation exists on a live machine within computers.txt file. If so, it should uninstall it, copy over the new installation, and then install it. If anything fails, log to its respective log file.
#echo off
:CheckifLogsExist
if NOT exist Uninstall.log (
copy /y nul Uninstall.log
) else (
del Uninstall.log && copy /y nul Uninstall.log
)
if NOT exist WMIC.log (
copy /y nul WMIC.log
) else (
del WMIC.log && copy /y nul WMIC.log
)
if NOT exist Copying.log (
copy /y nul Copying.log
) else (
del Copying.log && copy /y nul Copying.log
)
if NOT exist Install.log (
copy /y nul Install.log
) else (
del Install.log && copy /y nul Install.log
)
:checkifalive
for /F %%I IN (computers.txt) DO
(
ping -n 1 %%I
if NOT %errorlevel%==0 echo Machine offline && goto:EOF
:Uninstall
echo "Uninstalling previous version of Symantec Endpoint Protection"
psexec \\%%I -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Uninstall.log
:copy
echo "Finding out which processor is in the machine"
wmic cpu list brief > temp.out
findstr /I "86" temp.out && goto copy86 || goto copy64
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> WMIC.log
:copy86
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%%I
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Copying.log
goto Install86
:copy64
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%%I
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Copying.log
goto Install64
:Install86
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%%I -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Install.log
goto Finish
:Install64
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%%I -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Install.log
goto Finish
:Finish
)
Your syntax error is that these need to be on the same line, with a space after do
Note Stephan's comment that you also need to enable delayed expansion or as Vicky says use a subroutine that the forindo command will call
for /F %%I IN (computers.txt) DO
(
#echo off
:clear_logfiles :: Label not needed
:: copy command doesn't care, if the file exists or not, it just (re)creates it with size 0:
copy /y nul Uninstall.log
copy /y nul WMIC.log
copy /y nul Copying.log
copy /y nul Install.log
:checkifalive :: Label not needed
for /F %%I IN (computers.txt) DO ( call DoIt %%i )
echo all done.
goto :eof
REM this is the subroutine
:DoIt
ping -n 1 %1
if NOT %errorlevel%==0 (
echo Machine offline
goto :EOF
)
REM Uninstall
echo "Uninstalling previous version of Symantec Endpoint Protection"
psexec \\%1 -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
if NOT %errorlevel%==0 echo %1 - %errorlevel% >> Uninstall.log
REM copy
echo "Finding out which processor is in the machine"
wmic cpu list brief | findstr /I "86"
:: why checking for 86/64 if the code is exactly the same for both? Anyway - here we go:
if %errorlevel%==0 ( call copy86 ) else ( call copy64 )
goto :eof
:copy86
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%1
if NOT %errorlevel%==0 (
echo %1 - %errorlevel% >> Copying.log
) else (
REM Install86
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%1 -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %1 - %errorlevel% >> Install.log
)
goto :eof
:copy64
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%1
if NOT %errorlevel%==0(
echo %1 - %errorlevel% >> Copying.log
) else (
REM Install64
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%1 -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %1 - %errorlevel% >> Install.log
)
goto :eof