9

i have a batch file that needs to apply the attrib +h command to a file, then output to a txt file and display contents on screen. This should also be done if file has not been provided or can not be found. I have this so far but can not get it to work:


:TOP
IF EXIST "%1" GOTO COMMAND
) ELSE
(
GOTO ERROR1

:COMMAND
attrib +h %1
SHIFT
GOTO TOP
GOTO END

:ERROR1
IF "%1"=="" GOTO ERROR2
) ELSE
(
GOTO ERROR3

:ERROR2
ECHO.
ECHO No file(s) provided. Please re run the batch file.
GOTO END

:ERROR3
ECHO.
ECHO The file was not found. Please re run the batch file.
GOTO END

:END

This is my first computer course and any help will be greatly appreciated. Thank you.

SmkErrl
  • 91
  • 1
  • 1
  • 3
  • 2
    **To get you started:** [How the Command Interpreter parses Batch scripts](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/7970912) **Great Resources:** [ss64](http://ss64.com/), [Rob van der Woude](http://www.robvanderwoude.com/batchfiles.php), [DosTips](http://www.dostips.com/) – David Ruhmann Mar 11 '14 at 19:27
  • 2
    To make your code more readable, consider renaming the error labels. For instance, instead of ERROR2, say NOFILE. For ERROR3, say NOTFOUND. – Tony Mar 12 '14 at 00:08

3 Answers3

13

There are a few issues with this code. Firstly, batch files require specific syntax with their IF / ELSE statements.

Something like this

IF EXIST "%1" (
    echo "it's here!"
) ELSE (
    echo "it isn't here!"
)

works correctly, while something like this

IF EXIST "%1" 
(
    echo "it's here!"
) 

ELSE 
(
    echo "it isn't here!"
)

does not. The parenthesis delimit the block, so your IF command will execute everything in between ( and ) if it evaluates to true.

Secondly, you don't actually need any ELSE statements. Because you are using GOTO commands just before your ELSE commands, you will never reach the second GOTO command if the first IF evaluates to true.

Finally, with the code that you currently show, the :TOP tag that you have is unnecessary.

After all of that, you should be left with something that looks like this:

@ECHO off
IF EXIST "%1" (
    GOTO COMMAND
)
GOTO ERROR1

:COMMAND
    echo "You entered a file correctly, and it exists!"
    GOTO END

:ERROR1
    IF "%1"=="" (
        GOTO ERROR2
    )
    GOTO ERROR3

:ERROR2
    ECHO.
    ECHO No file(s) provided. Please re run the batch file.
    GOTO END

:ERROR3
    ECHO.
    ECHO The file was not found. Please re run the batch file.
    GOTO END

:END
Lily Mara
  • 3,859
  • 4
  • 29
  • 48
  • This makes it a easier to understand, my professor had it formatted differently making it harder to read. – SmkErrl Mar 11 '14 at 19:49
  • 2
    A tip for the future: don't tell people that this is your homework. You'll probably get more help. – Lily Mara Mar 11 '14 at 19:52
2

I am not to familiar with Batch, but it looks like your If statement is formatted wrong.

IF EXIST "%1" (
    GOTO COMMAND
) ELSE
(
    GOTO ERROR1
)
Justin
  • 31
  • 1
2

Just some problems with parenthesis and flow logic

@ECHO OFF

    IF "%~1"=="" GOTO ERROR1

:TOP
    IF NOT EXIST "%~1" GOTO ERROR2
    attrib +h "%~1"
    IF "%~2"=="" GOTO END
    SHIFT
    GOTO TOP

:ERROR1
    ECHO.
    ECHO No file(s) provided. Please re run the batch file.
    GOTO END

:ERROR2
    ECHO.
    ECHO The file "%~1" was not found. Please re run the batch file.
    GOTO END

:END
MC ND
  • 69,615
  • 8
  • 84
  • 126