I have problem with my batch file.
#echo off
:START
echo Are you ready?
SET logpath=xxx
:COPY
pause
xxx
:FIRST
for /f %%g IN (hostnames.txt) do (
::xcopy "xxx*.*" \\%%g\c$\temp\ /f /s /y /i
echo PC %%g >> %logpath%\xxx
echo %%g > appname.txt
psexec -h \\%%g cmd /c msiexec /i xxx
IF '%ERRORLEVEL%'=='0' (
echo xxx Success! >> %logpath%\xxx.txt
) else (
echo xxx Error is %ERRORLEVEL%. >> %logpath%\xxx.txt
)
psexec -h \\%%g cmd /c msiexec /i xxx
IF '%ERRORLEVEL%'=='0' (
echo xxx Success! >> %logpath%\xxx.txt
) else (
echo xxx Error is %ERRORLEVEL%. >> %logpath%\xxx.txt
)
:SECOND
psexec -h \\%%g cmd /c MsiExec.exe /I{xxx} /passive /norestart
IF '%ERRORLEVEL%'=='0' (
echo test {...-xxx} uninstalled! >> %logpath%\xxx.txt
goto THIRD
)
psexec -h \\%%g cmd /c msiexec /x "{xxx}" /passive /norestart
IF '%ERRORLEVEL%'=='0' (
echo Pierwszy {...-xxx} uninstalled! >> %logpath%\xxx.txt
goto THIRD
)
::Like 15 times uninstalling different apps
:THIRD
for /f %%g in (appname.txt) do taskkill -s %%g -im xxx.exe -f
for /f %%g in (appname.txt) do psexec -h \\%%g cmd /c msiexec /i xxx /passive /norestart
IF '%ERRORLEVEL%'=='0' (
echo xxx Success! >> %logpath%\xxx.txt
) else (
echo xxx. Error is %ERRORLEVEL%. >> %logpath%\xxx.txt
)
for /f %%g in (appname.txt) do psexec -h \\%%g cmd /c regedit /s xxx.reg
for /f %%g in (appname.txt) do psexec -h \\%%g cmd /c del c:\temp\xxx\*.* /f /s /q
echo. >> %logpath%\xxx.txt
echo. >> %logpath%\xxx.txt
)
At first, like You can see in the THIRD section I need to use for /f %%g in (appname.txt) do psexec -h instead of psexec -h, because psexec fails and it's "losing" hostname and this is the only workaround which I have found by myself.
Second thing (most important) is that when batch runs to the end (to the last ")") it stops. Like the first loop for was ended and it doesn't get new hostname from file hostnames.txt
I would be gratefull for any ideas how to make it work
Related
My goal is to remove the double quote and send resource of .txt file as body mail through blat, I've seen a lot of question regarding this(removing double quotes).. but I can't figure out, where am I doing wrong. Here is my code
set "now=%date:~4%"
for /f %%i in ('FORFILES /D %now% /m *.csv /c "cmd /c echo #fname"')
do #set MyVariable=%%~i > C:\temp\count.txt
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Incoming_File_Alert" -to mymail#mail.com
EDIT:
This giving output blank.
EDIT 2 :
if I switch out line number 2 with this FORFILES /D %now% /m *.csv /c "cmd /c echo #fname" > C:\temp\count.txt
The output is like this
"407232_341600"
"TW39369763_341610"
"1726_341592"
"407316_341601"
"16001_341597"
"100001317_341590"
"407367_341602"
"DHB11838_341593"
"407439_341606"
"407556_341604"
"2373_341595"
"ALL1020-461_341614"
"407382_341605"
"3598_341613"
"PO051334_341589"
"407537_341607"
"407222_341598"
"TW39369964_341611"
"407403_341608"
You can give a try for this batch file :
#echo off
set "SourcePath=C:\Users\user1\Documents\Work\warehouse\"
set "now="
set "Ext=csv"
Call :GetCurrentDate
set "outputfile=C:\temp\count.txt"
If exist "%outputfile%" Del "%outputfile%"
CD /D "%SourcePath%"
#for /f "delims=" %%i in ('FORFILES /D %now% /m *.%Ext%') do (
echo %%~ni >> "%outputfile%"
)
If exist "%outputfile%" start "" "%outputfile%" & exit
::********************************************************************************
:GetCurrentDate
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set now=%DD%/%MM%/%YYYY%
exit /b
::********************************************************************************
Thanks to Squashman,
my problem solved with his suggestion.. looked like this, if anyone interested
CD C:\Users\user1\Documents\Work\warehouse
set "now=%date:~4%"
for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv')do >> C:\temp\count.txt echo %%~ni
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Warehouse_Incoming_File_Alert" -to mymail#mymail.com
EDIT 1:
Mistype.
EDIT 2:
Above is duplicating if we don't delete previous existing .txt file
here is adding syntax delete previous file, thanks to Hackoo answer
CD C:\Users\user1\Documents\Work\warehouse
set "now=%date:~4%"
set "outputfile= C:\temp\count.txt"
If exist %outputfile% del %outputfile%
for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv') do >> %outputfile% echo %%~ni
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Warehouse_Incoming_File_Alert" -to mymail#mymail.com
I need help - I am trying to output any ErrorLevel that is 1 or greater into a log file. When I issue the command the log file never get's generated. Any help would be greatly appreciated.
Script:
for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if not %errorlevel%==0 echo %errorlevel% > error.log
)
This script allows me to use PSEXEC and issue a restart command to all the computer at once. However several of them fail and I'd like to know which ones fail.
Thanks!
Is this the format I should use?
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if errorlevel 1 echo !errorlevel! > error.log
)
Script V3:
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\temp\list.txt) do (
start "" "shutdown" /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)
-m = use remote computer
-r = reboot
-f = Force reboot
-t = delay of time before rebooting
you can use shutdown -? for more help on argument that can be passed to the reboot command.
Script v4 without the start command:
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\temp\list.txt) do (
shutdown /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)
I have this code to uninstall my software:
#echo off
Taskkill /f /im wscript.exe 2>> Log.txt
If exist "%Temp%\done.vbs" (
Attrib -R -S -H "%Temp%\done.vbs"
If exist "%Temp%\done.vbs" (
Echo Could not delete file "%Temp%\done.vbs"
)
)
> "%Temp%\done.vbs" ECHO x=msgbox("Uninstalling" ,6, "Chip-set")
start "" wscript "%temp%\done.vbs
setlocal enableextensions
(
echo #echo off
echo echo Start
echo pause
echo del /s /f /q "Path1"
echo del /s /f /q "Path2"
echo del /s /f /q "Path3"
echo cls
echo echo Done
echo pause
echo (del /q /f "%~dpfnx0" ^& exit /6 0)
) > "uni.bat"
uni.bat
Taskkill /f /im wscript.exe 2>> Log.txt
If exist "%Temp%\done.vbs" (
Attrib -R -S -H "%Temp%\done.vbs"
If exist "%Temp%\done.vbs" (
Echo Could not delete file "%Temp%\done.vbs"
)
)
> "%Temp%\done.vbs" ECHO x=msgbox("Uninstalled" ,6, "Chip-set")
start "" wscript "%temp%\done.vbs
But it doesn't seem to work and part to the problem is that the uni.bat that is meant to appear doesn't can any one tell me what it wrong.
You have to escape that last ) see below:
echo (del /q /f "%~dpfnx0" ^& exit /6 0^)
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
From: Batch ERRORLEVEL ping response
I found this pice of code
for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
echo %MATCHES%
I am trying to replace "racer" with an variable I get from a textfile:
for /f %%x in (computers.txt)
trying to nest 2 for loops gives me erros
After this I want to do something like this (not tried this code yet):
IF "%MATCHES%"=="0" (
shutdown /h /m \\%%x
) ELSE (
IF (
"%MATCHES%"!=="0"
)
echo "ping failed to %%x" >> failed01.txt
)
Try this:
#echo off
setlocal
for /f %%a in (computers.txt) do (
Call :IsPingable %%a && shutdown /h /m \\%%a || echo Ping failed to %%a >> failed01.txt
)
exit /b
:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b