Devenv command line switches error - batch-file

I have a simple solution which contains next to class projects also setup projects (extension installed in VS: vdproj).
Facing strange behaviour doing the following:
Open solution in VS2013 -> kick off rebuild -> no errors.
Create a small batch file:
CALL "%VS120COMNTOOLS%vsvars32.bat"
DEVENV "D:\Source\Solution.sln" /Rebuild "Debug|Any CPU" 1>NUL 2>&1
IF ERRORLEVEL 1 GOTO Error
IF ERRORLEVEL 0 GOTO Yeah
:Error
COLOR c
Echo.
Echo end %date% - %time%
Echo Failed!
PAUSE
:Yeah
Echo.
PAUSE
Run this batch file and the errorlevel is not zero
Changed now inside the batch file the devenv action to the following:
DEVENV "D:\Source\Solution.sln" /Clean "Debug|Any CPU" 1>NUL 2>&1
DEVENV "D:\Source\Solution.sln" /Build "Debug|Any CPU" 1>NUL 2>&1
Run this batch file and the errorlevel is zero
The strange thing is that without output redirection I do also not get any errors while running the batch with the rebuild action active.
Any suggestions why I do get a different behaviour?

In the first case you are doing a Rebuild. In the second case you are doing a Clean and Build. Temporarily remove the 1>NUL 2>&1 and you should be able to see what the error is all about.
You might simplify things a bit and save the results like this. Then you can view the log file before you 'Press any key to continue' if you want.
CALL "%VS120COMNTOOLS%vsvars32.bat"
SET "LogFile=%Temp%\BuildLog.txt"
IF EXIST "%LogFile%" DEL /F /Q "%LogFile%"
DEVENV "D:\Source\Solution.sln" /Rebuild "Debug|Any CPU" 1>"%LogFile%" 2>&1
IF ERRORLEVEL 1 (
COLOR c
Echo.
Echo end %date% - %time%
Echo Failed! View log file %LogFile% if you prefer. Then
)
PAUSE
IF EXIST "%LogFile%" DEL /F /Q "%LogFile%"

Related

Basic Windows Command don't work in .cmd file

Here is my dummyScript.cmd file contents-
#echo off
set logfile=LogFile.log
echo Deleting backed up databases and folder > %logfile%
RMDIR /S /Q dummyFolder
if %ERRORLEVEL% NEQ 0 (
echo Unable to delete the back-ups. Please check the log file '%logfile%'
call start notepad %logfile%
exit %ERRORLEVEL%
)
echo Copying another backed up folder from a remote server... >> %logfile%
xcopy /Y "\\dummyRemoteServer\c$\dummy\folder" "C:\Dummy\Folder" >> %logfile%
if %ERRORLEVEL% NEQ 0 (
echo Unable to the backed up folder from remote server. Please check the log file '%logfile%'.
call start notepad %logfile%
exit %ERRORLEVEL%
)
When I run that as Administrator, nothing happens. When I run the script from command prompt, it gives me this error message-
The syntax of the command is incorrect.
OS: Windows 10
Many people have posted this, but the solutions didn't work for me. Any suggestions?

RENAME function not renaming in batch script

EDIT: I have updated this post to include the entire script.
EDIT: While not ideal, this is meant to be an automation of a fix provided by the company that makes the software.
I have a batch file that I am running as Administrator.
I am running a batch file based on the file system input by the user.
One of the first commands renames a file.
If I execute this command on its own, from an elevated command prompt, it renames the file.
When I nest the command inside the IF statement, it doesn't rename the file.
I have commented out all of the other lines to simply rename the file if the user enters "1".
I have tried encapsulating the file path and file each individually within quotes, which I shouldn't need, and still do not get it to rename the file.
I am running the batch script as Admin.
#echo off
setlocal EnableDelayedExpansion
if "%$ecbId%" == "" (
echo Welcome to the ADaPT
echo Choose '1' for 32 bit
echo Choose '2' for 64 bit
echo Type anything else to abort.
echo.
set "UserChoice=abort"
set /P "UserChoice=Type your choice: "
if "!UserChoice!"=="1" (
echo Executing 32 bit sequence...
echo Regsvr32.exe /u C:\Windows\System32\MSCOMCTL.OCX
echo REN C:\Windows\System32\MSCOMCTL.OCX MSCOMCTL.bak
xcopy C:\install\MSCOMCTL.OCX C:\Windows\System32 folder
Regsvr32.exe C:\Windows\System32\MSCOMCTL.OCX
Regsvr32.exe /u C:\Windows\System32\MSCOMCTL.OCX
del "C:\Windows\System32\ MSCOMCTL.OCX"
echo REN "C:\Windows\System32\MSCOMCTL.bak" "MSCOMCTL.OCX"
Recho Regsvr32.exe C:\Windows\System32\MSCOMCTL.OCX
shutdown.exe /r /t 00
)
if "!UserChoice!"=="2" (
echo Regsvr32.exe /u C:\Windows\SYSWOW64\MSCOMCTL.OCX
echo REN "C:\Windows\SYSWOW64\MSCOMCTL.OCX" "MSCOMCTL.bak"
xcopy C:\install\MSCOMCTL.OCX C:\Windows\SYSWOW64 folder
Regsvr32.exe C:\Windows\SYSWOW64\MSCOMCTL.OCX
Regsvr32.exe /u C:\Windows\SYSWOW64\MSCOMCTL.OCX
del "C:\Windows\SYSWOW64\ MSCOMCTL.OCX"
echo REN "C:\Windows\SYSWOW64\MSCOMCTL.bak" "MSCOMCTL.OCX"
echo Regsvr32.exe C:\Windows\SYSWOW64\MSCOMCTL.OCX
REM... shutdown.exe /r /t 00
)
if not "!UserChoice!"=="1" (
echo toto3
if not "!UserChoice!"=="2" (
echo toto4
echo Unknown input ... Aborting script
endlocal
exit /B 400
)
)
)
endlocal
I have simplified your entire script structure so as not to require parenthesised blocks, or the need to delay expansion:
#Echo Off
If Defined $ecbId GoTo :EOF
Echo Welcome to the ADaPT
Choice /C CQ /M "Continue or Quit"
If ErrorLevel 2 GoTo :EOF
Set "Dest=TEM32"
Set PROCESSOR_ARCHITE|Find "64">Nul&&(Set Dest=WOW64)
Set "Dest=%SYSTEMROOT%\SYS%Dest%"
Echo Executing sequence . . .
RegSvr32 /U "%Dest%\MSCOMCTL.OCX"
Rem To backup and replace MSCOMCTL.OCX:
Rem Uncomment the next three unRemarked lines
Rem and comment the next three unRemarked lines below them.
::Del /A /F "MSCOMCTL.bak" 2>Nul
::Ren "%Dest%\MSCOMCTL.OCX" "MSCOMCTL.bak"
::XCopy "C:\install\MSCOMCTL.OCX" "%Dest%"
Rem To recover MSCOMCTL.OCX from the backup:
Rem Comment the next three unRemarked lines
Rem and uncomment the previous three unRemarked lines above them.
If Not Exist "MSCOMCTL.bak" RegSvr32 "%Dest%\MSCOMCTL.OCX" & GoTo :EOF
Del /A /F "%Dest%\MSCOMCTL.OCX"
Ren "%Dest%\MSCOMCTL.bak" "MSCOMCTL.OCX"
RegSvr32 "%Dest%\MSCOMCTL.OCX"
ShutDown /R /T 0 /D P:2:4
Notes:It is probably safe to delete lines 3 and empty line 4, I left them in only because no explanation was provided as to the purpose of the %$ecbId% variable value comparison.I have added Remarks for toggling the script for the backup or recovery methods, but not knowingly altered any of your command sequence, (I have however added the /A and /F options with Del, just in case, and included the /D option with ShutDown to ensure that the logs show it as a planned shutdown event).

Unrar a file in custom folder

Am trying to extract a RAR file to a directory (C:\autoexe\source). Here the "autoexe" folder name changes everyday. The fullname does not change, the constant thing is "auto" in the string "autoexe", the exe part changes. I tried the below
for /f "delims=" %%a in ('dir /b "%cd%\samples\package.rar"') do start "" "%ProgramFiles%\WinRAR\WinRAR.exe" x -ibck "%cd%\samples\package.rar" *.* "%cd%\auto * \source"
This commented batch code with error handling should do the job:
#echo off
rem Get name of first non hidden subfolder starting with auto in current folder.
for /D %%I in (auto*) do set "FolderName=%%I" & goto CheckArchive
echo Error: There is no auto* folder in: "%CD%"
echo/
pause
goto :EOF
:CheckArchive
if exist "samples\package.rar" goto ExtractArchive
echo Error: There is no file "package.rar" in subfolder "samples" of
echo folder: "%CD%"
echo/
pause
goto :EOF
:ExtractArchive
"%ProgramFiles%\WinRAR\UnRAR.exe" x -c- -idq -y -- "samples\package.rar" "%FolderName%\"
if not errorlevel 1 goto :EOF
echo/
echo/
pause
Read text file Rar.txt in program files folder of WinRAR for details on the used switches on UnRAR command line or run UnRAR.exe from within a command prompt window without any option to get displayed a brief help on how to use this freeware console application.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
pause /?
set /?
Read answer on Single line with multiple commands using Windows batch file for an explanation of & operator. And read the Microsoft support article Testing for a Specific Error Level in Batch Files.

Log a successful uninstall with a BAT file

I have created a BAT file to uninstall any version of skype. It works fine. I am wanting to set it up to also check the event viewer to see that it uninstalled successfully. I was wondering if there was anyone that has done something to this nature. So far, this is what I have:
#echo off
echo Closing skype...
taskkill /F /IM Skype.exe
echo Removing previous versions...
wmic product where "Name like 'Skype%%'" call uninstall
New Code:
#ECHO off
IF EXSIST "C:\Program Files (x86)\Skype" GOTO INSTALLED
ELSE IF EXSIST "C:\Program Files\Skype" GOTO INSTALLED
ELSE GOTO NOTINSTALLED
:Installed
taskkill /F /IM skype.exe
wmic product where "name like 'Skype%%'" call uninstall
shutdown /r /f
:NOTINSTALLED
EXIT
You can check the %errorlevel% variable to get the last error code.
If it's 0, then it worked!
if %errorlevel%==0 echo Uninstall successful
EDIT:
An update for your new code, (you can't do an else if in batch, only use them individually)
#ECHO off
IF EXIST "C:\Program Files (x86)\Skype" GOTO INSTALLED
IF EXIST "C:\Program Files\Skype" GOTO INSTALLED
EXIT
:INSTALLED
taskkill /F /IM skype.exe
wmic product where "name like 'Skype%%'" call uninstall
shutdown /r /f
You can try:
(
echo.Exit code was %errorlevel%
echo.
echo.Uninstall application terminated on %date% - %time%
) > ".\uninstall_log.txt"
If you want to output to console then use this:
echo.
echo.
echo.Exit code was %errorlevel%
echo.
echo.Uninstall application terminated on %date% - %time%
echo.
echo.
echo.
echo.Press any key to exit Skype uninstaller.
pause>nul
exit

Can i make batch unclosable

Ive been looking for answer to this question but so far unsucessfully. Can i somehow make it that after batch file is started it wouldnt close if you would press X button on top right (the exit button)? If yes, how to do that?
code for "fake virus"
#echo off
color 47
net stop themes >nul
title DEEP VIRAL INFECTION!
echo VIRAL INFECTION!!!
echo VIRAL INFECTION!!!
echo VIRAL INFECTION!!!
echo ERROR!!!
echo -
echo virus - TROJAN_DEMOLISHER code #45643676
echo -
echo FIREWALL - FAILED
echo -
echo ANTI-VIRUS - FAILED
echo -
echo IP ADDRESS BREACHED!
echo -
echo VIRUS ATTAINING: ****-****-****-8894
echo -
pause
cls
echo -
echo SCANNING INFECTED AREAS...
echo -
pause
title SCANNING INFECTED AREAS...
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
:end
cls
title DEEP VIRAL INFECTION!
echo -
echo 86.5 PERCENT OF MEMORY INFECTED
echo -
echo INFECTION FATAL!
echo -
echo DELETION OF ENTIRE CONTENTS OF LOCAL DISK C: REQUIRED
echo -
pause
cls
echo -
title DELETING HARD-DRIVE C:
echo -
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
cls
title DEEP VIRAL INFECTION!
echo -
echo CONTENTS OF HARD-DRIVE C: ERASED
echo -
pause
cls
echo -
echo SCANNING...
echo -
title SCANNING...
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
:end1
cls
title DEEP VIRAL INFECTION!
echo -
echo 0.00 PERCENT OF HARD-DRIVE INFECTED
echo -
pause
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
pause
cls
title SYSTEM FAILURE
color 17
echo ERROR!
echo -
echo VISUAL MEMORY LOST!
echo -
echo RAM LOST!
echo -
echo CORE PROCESSOR FAILING...
echo -
echo TOTAL SYSTEM CRASH IMMINENT!
echo -
echo -
pause
cls
echo -
echo -
echo -
echo SHUTDOWN COMPUTER NOW TO AVOID RISK OF FIRE!
echo -
echo -
echo -
pause
cls
echo -
echo -
echo -
echo SEEK PROFESSIONAL HELP IMMEDIATLY TO PREVENT FURTHER DAMAGE!
echo -
echo -
echo -
pause
start shutdown /s /t 60 /c "SYSTEM FAILURE<SHUTTING DOWN TO AVOID FURTHER DAMAGE!"
multiple dir lines are to give the effect of smth actually happening
There is no way to avoid that the cmd.exe session be closed when the user press the X button, but your Batch file can start a second cmd.exe session via start command and restart it again as soon as it terminates. If the window size of both the initial and second cmd.exe sessions are adjusted properly, you will get the desired effect.
#echo off
if "%1" equ "Restarted" goto %1
:again
echo N|start "" /WAIT cmd.exe /C "%~F0" Restarted > NUL
goto :again
:Restarted
echo I am a virus!
:loop
echo You can't close me!
timeout /T 1 > NUL
goto loop
Also, you were worried about losing the virus effect. Heres a way to make that not happen
Make another Notepad document and put,
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
pause
Replace The_Name_Of_Virus with the file name of the batch file.
Doing that tells the computer to open the file 5 times. Then if someone closes one of them and it restarts the code, there are still 4 left that are still going. Making it run 5 batch files at the same time will slow your system down a bit. The %username% command uses the username so you do not need to replace %username% with your PC username.
Someone please correct me if I got anything wrong (I don't think I did).
Also, I think you should only put 1 dir command in there because on computers with a lot of filled memory (like mine), it will take a long time to go through every file. I have about 400GB filled on my computer and it took 15mins just to finish the first block of dir commands.
Well instead of deleting task mgr you could just kill taskmgr and then rename it to say nottaskmgr.exe and after the prank thing is done just kill taskmgr and rename it back to taskmgr.exe
Instead of putting "pause" at the end of each speech part you can put
ping -n 2 localhost >nul
That gives it a small delay before moving on the next part.
It basically just tells the system to ping itself and depending on your computer that will take a different amount of time. The 2 is telling the system to ping itself 2 times.
You could copy your .Bat file source and re-create it in a command prompt applications, this will require Visual Studio Community or better.
You can remove this windows Top-Bar utilities through the application frames Properties.
(Use the property tab for this)
You could also make a simple prank virus using a regular windows form application by using the following code with a looped (copy 'n paste) prompt.
MessageBox.Show("message", title);
I hope this helped you.
Yours sincerely,
OTA

Resources