1
rem @echo off
cls
color 07
goto start

:initialize_chgcolor
call :make_download
call :make_unzip
call :run_download
call :run_unzip
del chgcolor.c chgcolor.zip download.vbs unzip.vbs 2> nul
goto :EOF
:initialize_choice
call :make_choice
call :run_choice
del ftp.commands 2> nul
goto :EOF
:fresh_initialize
mkdir compatability_commands
cd compatability_commands
echo Creating Scripts:
echo.
call :initialize_chgcolor
call :initialize_choice
cd ..
goto :EOF
:make_download
echo Creating Download Script...
echo 'Script from http://stackoverflow.com/a/2973344/1337520 > download.vbs
echo dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP") >> download.vbs
echo dim bStrm: Set bStrm = createobject("Adodb.Stream") >> download.vbs
echo xHttp.Open "GET", WScript.Arguments(0), False >> download.vbs
echo xHttp.Send >> download.vbs
echo. >> download.vbs
echo with bStrm >> download.vbs
echo     .type = 1 '//binary >> download.vbs
echo     .open >> download.vbs
echo     .write xHttp.responseBody >> download.vbs
echo     .savetofile WScript.Arguments(1), 2 '//overwrite >> download.vbs
echo end with >> download.vbs
echo Script Created!
echo.
goto :EOF
:make_unzip
echo Creating UnZip Script...
echo 'Extract Script In VBS > unzip.vbs
echo 'Script From http://stackoverflow.com/a/911796/1337520 >> unzip.vbs
echo 'The location of the zip file. >> unzip.vbs
echo ZipFile=WScript.Arguments(0) >> unzip.vbs
echo 'The folder the contents should be extracted to. >> unzip.vbs
echo ExtractTo=WScript.Arguments(1) >> unzip.vbs
echo. >> unzip.vbs
echo 'If the extraction location does not exist create it. >> unzip.vbs
echo Set fso = CreateObject("Scripting.FileSystemObject") >> unzip.vbs
echo If NOT fso.FolderExists(ExtractTo) Then >> unzip.vbs
echo    fso.CreateFolder(ExtractTo) >> unzip.vbs
echo End If >> unzip.vbs
echo. >> unzip.vbs
echo 'Extract the contants of the zip file. >> unzip.vbs
echo set objShell = CreateObject("Shell.Application") >> unzip.vbs
echo set FilesInZip=objShell.NameSpace(fso.GetAbsolutePathName((ZipFile))).items >> unzip.vbs
echo objShell.NameSpace(ExtractTo).CopyHere(FilesInZip) >> unzip.vbs
echo Set fso = Nothing >> unzip.vbs
echo Set objShell = Nothing >> unzip.vbs
echo Script created!
echo.
goto :EOF
:make_choice
echo Creating "choice.exe" script
echo binary > ftp.commands
echo get Services/TechNet/samples/PS/Win98/Reskit/SCRPTING/CHOICE.EXE >> ftp.commands
echo disconnect >> ftp.commands
echo bye >> ftp.commands
echo "choice.exe" completed!
echo.
goto :EOF
:run_download
echo Executing Download Script...
echo If a window opens up asking you "Do you want to open this file?", press OPEN
echo This will allow the script to download files via HTTP.
start /wait download.vbs http://www.mailsend-online.com/wp/chgcolor.zip chgcolor.zip
echo Download Script Completed!
echo.
goto :EOF
:run_unzip
echo Extracting Downloaded File...
echo If a window opens up asking you "Do you want to open this file?", press OPEN
echo This will allow the script to extract the downloaded file.
start /wait unzip.vbs chgcolor.zip "%CD%"
echo Extracting Complete!
echo.
goto :EOF
:run_choice
echo Executing FTP Download...
ftp -n -A -s:ftp.commands ftp.microsoft.com > nul
echo FTP Download Completed!
goto :EOF
:intialize_KeyList
call :write_last_modified
echo.
echo Creating KeyList.txt
echo Last Modified: %time_date% > KeyList.txt
echo ############################################### >> KeyList.txt
echo #                                             # >> KeyList.txt
echo #^>^>^>^>^>^>^>^>^>^>^>^>^>^> Serial Key List ^<^<^<^<^<^<^<^<^<^<^<^<^<^<# >> KeyList.txt
echo #                                             # >> KeyList.txt
echo ############################################### >> KeyList.txt
echo #########THIS_SHOULD_BE_LINE_7_(SEVEN)######### >> KeyList.txt
echo ############################################### >> KeyList.txt
echo.>> KeyList.txt
echo Done!
goto :EOF
:file_check
if exist compatability_commands (
  if not exist compatability_commands\chgcolor.exe (
    echo CHGCOLOR.EXE is missing from the tools, downloading.
    cd compatability_commands
    call :initialize_chgcolor
    cd ..
  )
  if not exist compatability_commands\CHOICE.EXE (
    echo CHOICE.EXE is missing from the tools, downloading.
    cd compatability_commands
    call :initialize_choice
    cd ..
    )
) else (
  echo The required tools were not found. These files will start downloading now.
  call :fresh_initialize
)

if not exist KeyList.txt (
  echo KeyList.txt was not found, generating...
  call :intialize_KeyList
  ) else (
    more +6 KeyList.txt > .tmp
    set /P string=< .tmp
    if not [%string%]==[#########THIS_SHOULD_BE_LINE_7_(SEVEN)#########] (
      cls
      color CF
      echo ERROR! YOU HAVE A MALFORMED KeyList.txt FILE! PLEASE BACKUP YOUR SERIALS, DELETE THE KeyList.txt FILE AND LAUNCH THIS PROGRAM AGAIN!
      echo Or fix it, if you know how....
      echo.
      echo Press a key to exit
      pause > nul
      set res=1
      goto :EOF
      )
    goto :EOF
    )
:start
echo Initializing...
set res=0
call :file_check
if %res%==1 exit
echo Else

:get_last_modified
set /p time_date= < KeyList.txt
echo %time_date%
pause
:write_last_modified
time /T>.tmp
set /p time=< .tmp
date /T>.tmp
set /p date=< .tmp
del /A H .tmp
set time_date=%time% %date%
goto :EOF

OK, Aim Of Code (This Stage):

  • Be able to download the files chgcolor.exe (In a ZIP), and CHOICE.exe (via FTP) in a folder called compatability_commands
  • Create a file (KeyList.txt) in the base directory.
  • When the script starts, check that line 7 of KeyList.txt is exactly: #########THIS_SHOULD_BE_LINE_7_(SEVEN)#########
  • Output the current time and date into the first line of KeyList.txt
  • Be able to check that each file exists, and if not, goes through the process of downloading them.

But as the title suggests, I am getting ) was unexpected at this time.. Putting @echo off as a remark/comment. It appears after

 if not exist compatability_commands\CHOICE.EXE (
echo CHOICE.EXE is missing from the tools, downloading.
 cd compatability_commands
 call :initialize_choice
 cd ..
)
)  else (

I do not see what is wrong, there are enough parentheses for them, unless I counted wrong. Help please :?

PS: Log File If It Helps >>HERE<<

Andrew Wong
  • 115
  • 10

1 Answers1

4

Replace the line:

    if not [%string%]==[#########THIS_SHOULD_BE_LINE_7_(SEVEN^)#########] (

Inside a code block the parser counts parentheses (also in echo commands). Closing parentheses ends the code block. If this not should be, escape it with a caret ^.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Ahh I see what happened. Thanks! – Andrew Wong Jul 09 '13 at 09:09
  • @AndrewWong btw: you can also escape with double quotes `if not "%string%"=="#########THIS_SHOULD_BE_LINE_7_(SEVEN)#########" (` – Endoro Jul 09 '13 at 09:12
  • Ok now I have this edit: echo #########THIS_SHOULD_BE_LINE_7_(SEVEN)######### >> KeyList.txt To echo #########THIS_SHOULD_BE_LINE_7_(SEVEN^)#########>> KeyList.txt But when I do: if "%string%"=="#########THIS_SHOULD_BE_LINE_7_(SEVEN^)#########" echo Yes, it doesn't say yes. Sorry about the formatting of this post. – Andrew Wong Jul 09 '13 at 09:28
  • EDIT: I think I'm going to just dropbox them. – Andrew Wong Jul 09 '13 at 09:39
  • https://www.dropbox.com/s/37oqzyw6lko3jw3/example2_file_string_different.bat Works – Andrew Wong Jul 09 '13 at 09:44
  • https://www.dropbox.com/s/0p3h635xzjits3i/example3_check_string_different.bat DOES NOT WORK – Andrew Wong Jul 09 '13 at 09:45
  • @AndrewWong outside a code block `()` you don't need escaping parentheses, [click](http://pastebin.com/JCarSsuh). – Endoro Jul 09 '13 at 09:58
  • Ok then referring it back to the original code, why isn't it working? – Andrew Wong Jul 09 '13 at 12:11
  • In the original code you are _inside_ the code block so you need escaping with caret or double quotes. The code block starts usually with an `if` or `for` statement with an open parenthesis `(` and goes until the next _matching_ closing parenthesis `)`. – Endoro Jul 09 '13 at 13:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33138/discussion-between-andrew-wong-and-endoro) – Andrew Wong Jul 09 '13 at 13:51
  • Does that mean that by using the double quotes,I don't need the escape carets? – Andrew Wong Jul 09 '13 at 13:52
  • Yes, you need here only one escaping: caret or double quotes. – Endoro Jul 09 '13 at 13:55
  • Well it seems to be working, except for the fact that, double clicking the batch file seems to go to the error code block (Color goes CF, and message appears). But when I run it from a command prompt, no error appears.. – Andrew Wong Jul 10 '13 at 01:21
  • EDIT: Ok it seems to be working, I just changed the file checking sequence backwards. – Andrew Wong Jul 10 '13 at 01:43