check batch file whether success or not [closed] - windows

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 12 months ago.
Improve this question
On the windows, I write a batch file and the following is the content.
How to check it whether error or not on any step?
If there is error, then threw message and stop it.
git clone xxx
cd xxx
cd build
cmake ..
ninja install .

Use or conditional operators or errorlevel directly:
Also, do not do run multiple cd commands when you can run one.
#echo off
git clone xxx && echo Success || echo Git failed & exit /b 1
cd /d "xxx\build" && echo Success || echo unable to CD & exit /b 1
cmake .. && echo cmake Success || echo cmake failed & exit /b 1
ninja install . && echo Success || echo ninja install error & exit /b 1
to use errorlevel directly, using a single example and also demonstrating pushd and popd instead of cd:
#echo off
git clone xxx
if errorlevel 1 echo Error occurred & exit /b
pushd "xxx\build"
echo run other commands
popd

Related

Windows Batch Script, For Loop stops after three iterations [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
Windows Scripting (batch), for loop exiting before all iteration are finished, without an error.
When I run this script
#ECHO OFF && SETLOCAL enabledelayedexpansion
SET PROJECTS[0]=project1
SET PROJECTS[1]=project2
SET PROJECTS[2]=project3
SET PROJECTS[3]=project4
SET PROJECTS[4]=project5
FOR /l %%N in (
0, 1, 2, 3, 4
) DO (
ECHO !PROJECTS[%%N]!
ECHO ===========================================================
)
echo Success.
exit
I get
cmd.exe /c test.bat
project1
===========================================================
project2
===========================================================
project3
===========================================================
Success.
Process finished with exit code 0
But expecting all 5 projects to print.
I cannot find the error. What is happening??

how to write batch script on command prompt to extract nth line from multiple txt files? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
What's the batch script I should write on Windows command prompt to extract the values from 20th line of 200 text files and put them into a new text file named "master.txt"
Each text files contains hundreds of lines, and I only need the value from line 20.
For example
text file name:
test1.edit.txt
test2.edit.txt
test3.edit.txt
......
Sample File:
Line 1 - xxxxx
Line 2 - xxxxx ......
Line 20- PROPERTY 100
Result:
PROPERTY 100 (from test1.edit.txt)
PROPERTY 200 (from test2.edit.txt)
PROPERTY 300 (from test3.edit.txt) ......
Thank you in advance
If you have cigwin installed, write a,simple bash script
that looks like:
for f in `ls f?`; do
head -9 $f | tail -1
done
change the "f?" to find whatever your file names are (mine
are f1, f2, f3). Save the script as "line9" and run it as
bash line9
If you don't have cygwin, consider installing it. :-)
It really does have lots of useful unixish tools.
Copy this code to a batch file. Drag the folder where the TXT files are to the batch file. It should save all "line 20" text to a file called "Result.txt"
#echo off
if /i exist "%~1" (if /i not exist "%~1\" exit) else (exit)
set "Folder=%~1"
if /i exist "Result.txt" del /q "Result.txt"
pushd "%Folder%"
for /f "delims=" %%a in ('dir /b *.txt') do Call :GetLine "%%a"
popd
cmd.exe /c start "" notepad Result.txt
exit
:GetLine
for /f "skip=19 eol= delims=" %%a in ('type "%~1"') do (
echo %%a
>>%~dp0Result.txt echo %%a
goto :EOF
)
goto :EOF

Best practice for exiting batch file? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
For a batch file I want to check for different conditions and have a help message
Which one is the best practice for exiting the batch file after displaying the message?
if "%1"=="/?"(
echo "help message"
exit /b 0
)
[more code]
or
if "%1"=="/?"(
echo "help message"
goto :EOF
)
[more code]
:EOF
The first one seems better for my untrained eyes but a lot of the examples online use the GOTO tag method
What is the SO community's opinion on this?
Personally I use exit.
The normal exit command simply terminates the current script, and the parent (for example if you were running a script from command line, or calling it from another batch file)
exit /b is used to terminate the current script, but leaves the parent window/script/calling label open.
With exit, you can also add an error level of the exit. For example, exit /b 1 would produce an %errorlevel% of 1. Example:
#echo off
call :getError rem Calling the :getError label
echo Errorlevel: %errorlevel% rem Echoing the errorlevel returned by :getError
pause
:getError
exit /b 1 rem exiting the call and setting the %errorlevel% to 1
Would print:
Errorlevel: 1
press any key to continue...
Setting error levels with this method can be useful when creating batch scripts that may have things that fail. You could create separate :labels for different errors, and have each return a unique error level.
goto :eof ends the current script (call) but not the parent file, (similarly to exit /b)
Unlike exit, in which you can set an exiting errorlevel, goto :eof automatically sets the errorlevel to the currently set level, making it more difficult to identify problems.
The two can also be used in unison in the same batch file:
#echo off
call :getError
echo %errorlevel%
pause
goto :eof
:getError
exit /b 2
Another method of exiting a batch script would be to use cmd /k When used in a stand-alone batch file, cmd /k will return you to regular command prompt.
All in all i would recommend using exit just because you can set an errorlevel, but, it's really up to you.
There is no functional or performance difference between GOTO :EOF vs EXIT /B, except that EXIT /B allows you to specify the returned ERRORLEVEL, and GOTO :EOF does not.
Obviously if you want to specify the ERRORLEVEL when you return, then EXIT /B is preferred.
If you don't care about the return code, or you know that the ERRORLEVEL is already set to the correct value, then it makes no difference - it becomes strictly a matter of preference / coding style.

Creating year,month,date folder structure based on filename [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some files in a some unix server. When i copy to windows, based on filename , the file has to be copied into corresponding year,corresponding month and corresponding date folders. Sample filename : 20120201.117_visual_sciences_web_feed.out.gz.
Folder structure to be created based on first part of filename, in this case : 20120201(YYYY,MM,DD) . In above example ,filename should be copied into 2012 -> 02-> 01 folder.Folders should be created if not created Honestly i am not getting how this can be implemented, please suggest.
This should do the trick for you -
#echo off
set filename=20120201.117_visual_sciences_web_feed.out.gz
set filepath=PATH\TO\%filename%
REM get the date from the file name
for /f "delims=." %%A in ("%filename%") do set fdate=%%A
REM Y=YEAR; M=MONTH; D=DAY
set Y=%fdate:~0,4%
set M=%fdate:~4,2%
set D=%fdate:~6,2%
REM echo %Y% %M% %D%
REM check to see if directories exist
if not exist \%Y% mkdir %Y%
if not exist \%Y%\%M% mkdir %Y%\%M%
if not exist \%Y%\%M%\%D% mkdir %Y%\%M%\%D%
REM copy the file to the newly created destination folder
copy %filepath% %Y%\%M%\%D%\%filename%
pause
I hope you're able to implement this into your script easily enough. As pointed out; if you added some examples from your script - I would be able to help more.

How do I run a batch file as an administrator on all computers in a network at user login? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I don't have group policy installed to deploy firewall rules from there, so in the meantime I want to be able to run a batch file to update my firewall to allow Spiceworks to conduct scans on my machines without errors. I have the batch file to change the inbound rules but I cant run them on every computer separately it would not be time efficient. So how would I be able to have this batch file run as administrator via the network without the user seeing the admin password? I'm running XP on some machines and 7 on others.
I've tried my best to edit this question because I'm new to programming
There isn't a way that I know of to do what you want in a login script but you can do it all at once from your workstation using PsExec This is the command I'd use:
psexec -c -f -s -h /accepteula \\* "c:\path\to\your\batfile.bat"
That will run it against all computers on your domain with the system account and copy your batch file to each computer before running it. If, like me, you have a large domain and you only want to run it against a specific OU, here is a script I wrote to do that.
#echo off
setlocal enabledelayedexpansion
:: User Defined Variables
set FilterOU=TRUE
set OU=Environmental
if %FilterOU%==TRUE (
set qComp=wmic /NAMESPACE:\\root\directory\ldap PATH ds_computer Where "ds_distinguishedname like '%%%OU%%%'" get ds_name
) ELSE (
set qComp=wmic /NAMESPACE:\\root\directory\ldap PATH ds_computer get ds_name
)
Echo.
Echo Getting list of computers.
Echo.
for /f "skip=1" %%c in ('%qComp%') do (
For /f %%d in ("%%c") do (
ping -n 1 %%d | find "TTL=">nul && set r="up"||set r="down"
if /i !r!=="up" (
psexec -c -f -s -h /accepteula \\%%d "C:\scripts\yourbatchfile.bat"
)
)
)
exit /b

Resources