Ask Question Forum:
Model Library:2025-02-08:A.I. model is online auto reply
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-03-10 09:05:48
Point:500 Replies:5 POST_ID:828893USER_ID:11059
Topic:
Windows 7;Windows Batch Scripting;Shell Scripting
Dear Expert,
I've one file called "junk.txt" in which I would like to replace Hostname and PortNumber
string with new value using window batch shell script , but I am spending more a few
hour in google, and still can not solve why I can not echo str variabie which
is always empty from following script code, Could you help on this, how to find
a string in string and replace with the new value. And the string addtion is "+" psosible ?
set newip="203.1.123.342"
set newport="00001779"
for /F "tokens=*" %%i in (junk.txt) do set str=%%i & echo %str% & if (%str:~1,9)=="HostName") str='"HostName"="'+'"+newip+'""
Please advise
Duncan
junk.txt file
=============
I've one file called "junk.txt" in which I would like to replace Hostname and PortNumber
string with new value using window batch shell script , but I am spending more a few
hour in google, and still can not solve why I can not echo str variabie which
is always empty from following script code, Could you help on this, how to find
a string in string and replace with the new value. And the string addtion is "+" psosible ?
set newip="203.1.123.342"
set newport="00001779"
for /F "tokens=*" %%i in (junk.txt) do set str=%%i & echo %str% & if (%str:~1,9)=="HostName") str='"HostName"="'+'"+newip+'""
Please advise
Duncan
junk.txt file
=============
"Present"=dword:00000001"HostName"="217.76.12.127""Protocol"="ssh""PortNumber"=dword:00001771"CloseOnExit"=dword:00000001 1:2:3:4:5:
Expert: dragon-it replied at 2024-03-10 10:43:29
Ok, no problem. Worked for me here but then copy/pasted wrong version, line 19 should have been
echo %%i=!value!
Steve
echo %%i=!value!
Steve
Author: duncanb7 replied at 2024-03-10 10:01:14
Thanks for all of your rely.
I tried obda 's code in one time, it works as I expect
and dragon-it, thanks for your code, I tried it that seems not working
even review it many times and check all correct file typing and path.
The output junk.txt is same as before
Thanks for your attached link for the subject
Have a nice
Duncan
I tried obda 's code in one time, it works as I expect
and dragon-it, thanks for your code, I tried it that seems not working
even review it many times and check all correct file typing and path.
The output junk.txt is same as before
Thanks for your attached link for the subject
Have a nice
Duncan
Expert: dragon-it replied at 2024-03-10 09:29:28
I read there that you wanted "+" in the results, if not just remove the "+" characters.
Steve
Steve
Assisted Solution
Expert: dragon-it replied at 2024-03-10 09:28:25
100 points EXCELLENT
Try this. Few problems in there including within a single command like the for command you can't access variables set like your str entry unless you have delayedexpansion turned on (see line 2). Then you use !variable! to access.
Steve
Steve
@echo offsetlocal enabledelayedexpansionset newip=203.1.123.342set newport=00001779cd /d c:somedirwithitinREM Delete previous go if needed and rename file to junk.old to processdel junk.old 2>NULrename junk.txt junk.oldREM Make backup copy of filecopy /y junk.old junk.bak(for /F "tokens=1,2 delims==" %%i in (junk.old) do ( set value=%%~j if /i "%%~i"=="HostName" set value="+%newip%+" if /i "%%~i"=="PortNumber" set value="%newport%" echo %%i=%%j)) > junk.txtREM Remove this if wantedtype junk.txtpause 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:
Other alternatives are to use a util. to do it for you, e.g.
http://www.paulslore.com/utils/chgstr.zip
http://tools.tortoisesvn.net/grepWin
http://www.programmersheaven.com/download/41236/download.aspx
http://software.reinhardt.nu/ssr/index.htm
http://fart-it.sourceforge.net/
http://www.gnu.org/software/sed/
http://www.inforapid.de/html/searchreplace.htm
http://www.nodesoft.com/SearchAndReplace/Default.aspx
http://www.no-nonsense-software.com/freeware/
http://www.bestcode.com/html/findreplace.html
Accepted Solution
Expert: oBdA replied at 2024-03-10 09:28:01
400 points EXCELLENT
Try it with this script; it will make a backup of the original file before changing the original:
@echo offsetlocal enabledelayedexpansionset InputFile=C:Tempjunk.txtset NewHostName=203.1.123.342set NewPort=00001779set BackupFile=%InputFile%.bakif not exist "%BackupFile%" copy "%InputFile%" "%BackupFile%" >NULif exist "%InputFile%" del "%InputFile%"for /f "tokens=1-3 delims==:" %%a in ('type "%BackupFile%"') do ( if "%%~c"=="" (set Line=%%a=%%b) else (set Line=%%a=%%b:%%c) if "%%~a"=="HostName" (set Line=%%a="%NewHostName%") if "%%~a"=="PortNumber" (set Line=%%a=%%b:%NewPort%) echo !Line! >>"%InputFile%" echo !Line!) 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15: