in Education by
I have a batch file as follows; CD C:\MyFolder findstr /c:"stringToCheck" fileToCheck.bat IF NOT XCOPY "C:\OtherFolder\fileToCheck.bat" "C:\MyFolder" /s /y I am getting an error ("C:\OtherFolder\fileToCheck.bat" was unexpected at this time.) when trying to execute this. Please let me know what I am doing wrong. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I presume you want to copy C:\OtherFolder\fileToCheck.bat to C:\MyFolder if the existing file in C:\MyFolder is either missing entirely, or if it is missing "stringToCheck". FINDSTR sets ERRORLEVEL to 0 if the string is found, to 1 if it is not. It also sets errorlevel to 1 if the file is missing. It also prints out each line that matches. Since you are trying to use it as a condition, I presume you don't need or want to see any of the output. The 1st thing I would suggest is to redirect both the normal and error output to nul using >nul 2>&1. Solution 1 (mostly the same as previous answers) You can use IF ERRORRLEVEL N to check if the errorlevel is >= N. Or you can use IF NOT ERRORLEVEL N to check if errorlevel is < N. In your case you want the former. findstr /c:"stringToCheck" "c:\MyFolder\fileToCheck.bat" >nul 2>&1 if errorlevel 1 xcopy "C:\OtherFolder\fileToCheck.bat" "c:\MyFolder" Solution 2 You can test for a specific value of errorlevel by using %ERRORLEVEL%. You can probably check if the value is equal to 1, but it might be safer to check if the value is not equal to 0, since it is only set to 0 if the file exists and it contains the string. findstr /c:"stringToCheck" "c:\MyFolder\fileToCheck.bat" >nul 2>&1 if not %errorlevel% == 0 xcopy "C:\OtherFolder\fileToCheck.bat" "c:\MyFolder" or findstr /c:"stringToCheck" "c:\MyFolder\fileToCheck.bat" >nul 2>&1 if %errorlevel% neq 0 xcopy "C:\OtherFolder\fileToCheck.bat" "c:\MyFolder" Solution 3 There is a very compact syntax to conditionally execute a command based on the success or failure of the previous command: cmd1 && cmd2 || cmd3 which means execute cmd2 if cmd1 was successful (errorlevel=0), else execute cmd3 if cmd1 failed (errorlevel<>0). You can use && alone, or || alone. All the commands need to be on the same line. If you need to conditionally execute multiple commands you can use multiple lines by adding parentheses cmd1 && ( cmd2 cmd3 ) || ( cmd4 cmd5 ) So for your case, all you need is findstr /c:"stringToCheck" "c:\MyFolder\fileToCheck.bat" >nul 2>&1 || xcopy "C:\OtherFolder\fileToCheck.bat" "c:\MyFolder" But beware - the || will respond to the return code of the last command executed. In my earlier pseudo code the || will obviously fire if cmd1 fails, but it will also fire if cmd1 succeeds but then cmd3 fails. So if your success block ends with a command that may fail, then you should append a harmless command that is guaranteed to succeed. I like to use (CALL ), which is harmless, and always succeeds. It also is handy that it sets the ERRORLEVEL to 0. There is a corollary (CALL) that always fails and sets ERRORLEVEL to 1.

Related questions

0 votes
    I would like to manage a process inside a cmd file. My cmd will launch an other cmd file and i ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Locked. This question and its answers are locked because the question is off-topic but has historical ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I have a swiftUi view depending on a class data. Before displaying the data, I have to compute it ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I would like to specify an additional default shortcut class to a set of classes, similarly to that @each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    So, I am creating an application which displays song lyrics for entered songs, however, longer songs do not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Write a program to take a string parameter (st) of a function reversing(). Reverse the sentence in sucha way so ... None Hate All Love Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    I am retrieving messages from an Outlook account. I am trying to get inline files and attachments from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    If the probabilities for A to fail in an examination is 0.2 and that for B is 0.3, then the probability that either A ... C. ≤ .5 D. 0 Select the correct answer from above options...
asked Nov 20, 2021 in Education by JackTerrance
0 votes
    If the probabilities for A to fail in an examination is 0.2 and that for B is 0.3, then the probability that either A ... C. ≤0.5 D. 0 Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
0 votes
    The operates if all of its three components function. The probability that the first component fails during the year ... the year? Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
0 votes
    ___________ refers to the ability of the system to recover committed transaction updates if either the system ... in chapter Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    What do you do if any concrete cube fails in 28 days compressive strength test?...
asked Jan 20, 2021 in Technology by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Dec 7, 2021 in Education by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
...