1

Coding is just something I like to play with so I have limited knowledge and research hasn't helped. If I run the script I get both success and error messages instead of one or the other.

@ECHO OFF

SET /p HOSTFILE=Host File Name?&CLS

SET /p HIDEFILE=File To Hide?&CLS

SET /p OUTPUT=Output File Name?&CLS

SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS

COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% >null

if %ERRORLEVEL% EQU 0 GOTO continue

if %ERRORLEVEL% EQU 2 GOTO error

:continue
    ECHO %OUTPUT%%EXTENSION% Created.

:error
    ECHO File Not Created.

TIMEOUT /T 2 >null
Red
  • 61
  • 1
  • 8
  • 2
    Given that the previous command was `CLS`, what are you expecting the first `%ERRORLEVEL%` to return a code for/of? What about the second `%ERRORLEVEL%`? What are your `if` commands supposed to be determining? – Compo Nov 26 '20 at 20:51
  • I posted the wrong code version, I updated the to running one, If it'll matter. From my understanding, it should check for success "0", and the most likely error "2" and return the assigned responces. – Red Nov 26 '20 at 20:58
  • 2
    Success of what? First of all it should be `NUL` not `null`, and why would you expect the `copy` command to fail. The obvious reason would be that one or other of the files may not exist, and for that I'd check their existence first, not wait to see if the copy fails. IMO you should check for valid entries for each of your `SET /p` commands, regardless of whether you want to check for an errorlevel from the `copy` command. – Compo Nov 26 '20 at 22:14
  • 4
    a label doesn't stop the script. It will just continue with the next line. Use `goto :eof` to stop the script. – Stephan Nov 26 '20 at 22:29
  • The entire script is written to support the automation of this string "COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% >nul", it is the success of this that I am checking for. As for checking for the files first, I figured that would require me to step even further beyond my understanding and make this project even more difficult considering my current level of understanding. Possibly a future tweak but for now I'm crawling with these issues alone. – Red Nov 26 '20 at 22:31
  • More complicated??? Then why put the `&CLS` after each line. That just makes the script more unreadable. And the errorlevel will not be `2` when the copy command fails. It will be `1`. – Squashman Nov 26 '20 at 22:38
  • The reason I used &CLS was to clear the previous input question from the screen. I don't know of another way, or why this is wrong, this is what my googling found. – Red Nov 26 '20 at 22:47
  • 1
    `cls` is not bad per se, but it deletes any errormessages from the command before, before you can read it. So (generally for troubleshooting) you should remove them. Once you are sure your script works as intended, add them where you feel a need for it. – Stephan Nov 27 '20 at 16:25
  • 1
    Does this answer your question? [Where does GOTO :EOF return to?](https://stackoverflow.com/questions/37515901/where-does-goto-eof-return-to) – Nico Haase Nov 27 '20 at 17:39

1 Answers1

1

As Compo said in the comment you should probably check if your files exist. when both hostfile and hidefile exist, a success message is indicated otherwise error message follows.

@ECHO OFF

SET /p HOSTFILE=Host File Name?&CLS
SET /p HIDEFILE=File To Hide?&CLS
SET /p OUTPUT=Output File Name?&CLS
SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS


IF EXIST "%HOSTFILE%" IF EXIST "%HIDEFILE%" (
    echo exist hidefile
    COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% > nul
    GOTO continue
    )
) ELSE (
    echo does not exist
    GOTO error
)

:continue
    ECHO %OUTPUT%%EXTENSION% Created.
    GOTO timer
    exit /b 0

:error
    ECHO File Not Created.
    GOTO timer
    exit /b 0

:timer
    TIMEOUT /T 2 > nul
    exit /b 0

lww
  • 624
  • 1
  • 7
  • 14
  • This works perfectly, and you gave me working examples of mechanisms I've not used to play with and learn, thank you. – Red Nov 28 '20 at 18:02