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.
One of the common uses for the 'if' statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the 'if' statement can be done for both strings and numbers.
[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.
%% in batch acted like \\ in bash. Where one would need to cancel the meaning of the previous percent-sign in a batch file; because variables in batch look like %var% . So because percent had a special meaning you needed to use %%var%% so a variable was still usable in a batch file.
The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.
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
)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With