') was unexpected at this time.
This above error is seen when i run the below batch file :-
how do i fix this error
hostname > tmpFile
set /p primary= < tmpFile
pdls -c ser -a primary-host ids_ccm | for /f "tokens=3" %a in ('findstr :') do #echo %a > tmpFile1
set /p previous_primary= < tmpFile1
if "%previous_primary%"=="" pdset -c ser -x"-primary-host %primary% ids_ccm
else pdset -c ser -x"-primary-host %primary% -previous-primary-host %previous_primary%" ids_ccm
Within a batch file, metavariables such as the %a in your for statement must be %%a. %a is only correct when executing the command directly from the prompt. The syntax for if/else is if value op value (commandstrue) else (commandsfalse) where op is a comparison-operator, the first ( must be on the same physical line as the if, and ) else ( must be on the same physical line. There may be any number of physical lines between ( and ).
Related
I need help in to provide output that contain a blank space after each of 2 lines from .txt file containing number of lines without any blank in between.
Example :
Suppose a .txt file name as media.txt contains below input
A
B
C
D
.
.
.
Z
I want to represent above input into output like :
A
B
-----
C
D
----
E
F
----
.
.
.
.
I tried by using odd even concept but failing them to represent in above mentioned output.
Here, is my code in batch script:
set flag=1
for /f "tokens=2 delims==" %%a in ('type media.txt^|findstr "output"') do (
if %flag%%%2==0 (
echo %%a
) else (
echo.
echo ------------------
echo %%a
set flag+=1
)
)
Please let me know, where i am doing wrong..
This can easily be done in a batch-file or cmd with PowerShell. If you are on a supported Windows system, PowerShell will be available.
powershell -NoLogo -NoProfile -Command ^
"Get-Content .\media.txt -ReadCount 2 | ForEach-Object { $_; Write-Output '----' }"
You can't use if with a modulus and no % around a variable, and you can't check the current value of a variable in a loop without delayed expansion or some other hoops.
This should do the needful.
#(SETLOCAL ENABLEDELAYEDEXPANSION
ECHO OFF
SET /A "Flag=0"
SET /A "Mod=3"
)
CALL :Main
( ENDLOCAL
EXIT /B
)
:Main
For /f "tokens=2 delims==" %%_ in ('
Type media.txt^|findstr "output"
') do (
SET /A "flag= (flag + 1) % mod"
IF !flag! == 0 (
echo.
echo.------------------
)
echo.%%_
)
I wrote the following command line.
C:\Users\Administrator> echo TOMATO:50EA| (set /p z=& call set v1=APPLE:30EA %z%& for /F "tokens=1,2 delims= " %K in ('echo %v1%') do (echo Item 1: %K , Item 2: %L))
Result:
C:\Users\Administrator>(echo Item 1: APPLE:30EA , Item 2: TOMATO:50EA )
Item 1: APPLE:30EA , Item 2: TOMATO:50EA
Why has it also printed the echo command line?
The default behaviour in command line is to echo to console the command that will be executed. You can turn echo off or precede the commands you want to hide with #
echo TOMATO:50EA|(set /p z=& for /F "tokens=1,2 delims= " %K in ('echo APPLE:30EA %z%') do #echo Item 1: %K , Item 2: %L)
But this code (and your code) has a point of failure. It will only work while z has no value before starting to execute the command.
You can try to use delayed expansion
echo TOMATO:50EA|cmd /q /v /c"(set /p z=& for /F "tokens=1,2" %K in ("APPLE:30EA !z!") do echo Item 1: %K , Item 2: %L)"
and while the default configuration (with delayed expansion disabled) is used, then this code will solve the problem. But if you have delayed expansion enabled at command line and z has an initial value it will also fail.
Note:
The code in the question is written using %K. This means command line. Remember that in batch files the for replaceable parameters need to have the percent sign escaped (%%K).
I have this block of code:
#ECHO OFF
SET "SRCFOLDER=C:\Users\MyUserName\Desktop\PhotoTests"
SET "TEMPCODE=Hi"
ECHO %TEMPCODE%
ECHO.
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
)
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
PAUSE
I'm confused by the output as I noticed that the variable in the FOR loop is not overwritten by what I think should be the content of "test.txt", which will vary each time the FOR loop runs.
For the purpose of this example, the file tooltipInfo.bat will echo a text string like "1 Star" or "3 Stars" based on the rating recorded in the file's properties. The FIND statement should result in a "0" or "1" being saved into the test.txt file.
The output is:
Hi
Canon_Locked.JPG
Hi
Nikon_Locked.JPG
Hi
0
Press any key to continue . . .
May I know why the TEMPCODE variable isn't being overwritten in the loop and retains the original value of "Hi". However in the final block of code, it was able to read and echo out the actual content of the file.
This is a common mistake when using FOR and parentheses. The problem is that by default every variable between ( and ) is evaluated when the line is first read, and then re-used for each iteration of the FOR.
If the variable changes during the loop (via SET), then you will need to change % to ! for variables inside the parentheses, and also turn on delayed expansion using setlocal
SETLOCAL enabledelayedexpansion
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO !TEMPCODE!
ECHO.
)
I am really confused with something. I am making a batch file that can can display a specific line that I want. For example there is file hi.txt
Content of file -
ro.build.id=KOT49H
ro.build.display.id=XXX_ROM_v1
ro.build.version.incremental=eng.mudingyu.1408084583
ro.custom.build.version=XOLO_8x-1000_0815.v009
ro.internal.version=J608_XOLO_N1A_V0.8.7_S08015
ro.internal.version.rgk=J608_XOLO_N1A_V0.8.7_S08015
ro.build.version.sdk=19
ro.build.version.codename=REL
ro.build.version.release=4.4.4
ro.build.date=Fri Aug 15 14:39:45 CST 2014
ro.build.date.utc=1408084785
As here you can see the file content. Now say I want to print ONLY this piece of information on my screen when batch is executed
"4.4.4" from this line "ro.build.version.release=4.4.4"
I managed to pull of code for the 2nd line
ro.build.display.id=XXX_ROM_v1
When I execute batch I see this
XXX_ROM_V1
Code I used
set "build.prop="
for /F "skip=3 delims=" %%i in (WORKING_FOLDER\system\build.prop) do if not
defined build.prop set "build.prop=%%i"
echo.
echo "%build.prop%"
echo.
echo Enter the name you want to choose
set /p name=
echo Backing up old build.prop...
md C:\Kitchen\temp
copy C:\Kitchen\WORKING_FOLDER\system\build.prop C:\Kitchen\temp
cd C:\Kitchen\temp
cd C:\Kitchen\WORKING_FOLDER\system
build.prop.new (
for /f "tokens=1* delims==" %%A in (build.prop) do (
if "%%A" == "ro.build.display.id" (
echo ro.build.display.id=%name%
) else (
echo %%A=%%B
)
)
)
move /y build.prop.new build.prop >nul
build.prop.new (
for /f "tokens=1* delims==" %%A in (build.prop) do (
if "%%A" == "ro.build.id" (
echo ro.build.id=%name%
) else (
echo %%A=%%B
)
)
)
move /y C:\Kitchen\temp\build.prop.new
C:\Kitchen\WORKING_FOLDER\system\build.prop >nul
echo Changed value of ro.build.display.id to " %name% "
echo.
set "build.prop="
for /F "skip=3 delims=" %%i in (WORKING_FOLDER\system\build.prop) do if not
defined build.prop set "build.prop=%%i"
echo In build.prop now the current name of ROM is
echo.
echo Current ROM Name = %name%
Here as you see in first line I print the name using the line command function and also took input from user to change the name so it became easy to print the file name but in some other files the line "ro.build.display.id" is not always on 4th.
So my question is what logic or whatever can be done in order to print the particular term on screen that I wish to print without prompting user to enter new file name just print that specific piece of information
eg The OUTPUT I want
The Android version is "4.4.4"
EG Say I want to print "4.4.4" only from the file above
how can I do it?
Using findstr or anything please help really appreciated.
whew - that was a lot to read for a quite simple question.
for /f "tokens=2 delims==" %%i in ('find "ro.build.version.release" hi.txt') do set release=%%i
echo The Android version is "%release%"
Find the correct line and tokenize it with = as delimiter.
I am totally new on writing batch script,busy with the tutorials with the example below I can learn a thing or two.I really need help to write a batch script to insert a line of text at the middle of existing text file.
For example given the file myfile.txt with the contents:
a
bcd
efg
hjiklmnop
q
rs
t
uvwxyz
The the command ./put-in-middle.sh "=== === ===" myfile.txt
should modify the file to:
a
bcd
efg
hjiklmnop
=== === ===
q
rs
t
uvwxyz
#echo off
rem Count the number of lines in the file with FIND
for /F %%a in ('find /C /V "" ^< %2') do set numLines=%%a
rem Get the number of middle line
set /A middle=numLines/2
rem Process all lines, use FINDSTR /N to insert line numbers
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %2') do (
rem Echo the original line
echo/%%b
rem If the line is the middle one...
if %%a equ %middle% (
rem Insert the new line
echo %~1
)
)
Create previous Batch file as put-in-middle.bat and execute it this way:
put-in-middle "=== === ===" myfile.txt
Notes:
Previous program does not check for errors, like missing parameters. This checking may be added, if you wish.
The slash in the command echo/%%b is inserted to avoid the message "ECHO is on" if the line is empty. If the line may contain the string "/?", then the command should be changed to echo(%%b to avoid that the echo help be displayed in this case (the left parentheses is the only character that do that).
If the file contains Batch special characters, like < > | & ), the echo/%%b command fail. In this case, a special processing of files lines must be added. The same point apply to the new inserted line.
Previous program just display in the screen the new file. If you want to replace the original file, the output must be redirected to an auxiliary file and replace the original one at end:
.
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %2') do (
. . .
)) > auxiliar.txt
move /Y auxiliar.txt %2
Using sed and assuming even number of lines:
sed $(( $(wc input -l | cut -d' ' -f1) / 2))'a=== === ===' input
And this is the script version put-in-middle.sh:
line=$1
file=$2
sed $(( $(wc $file -l | cut -d' ' -f1) / 2))"a$line" $file