[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Batch File: String manipulation

Posted on 2010-08-19
21
Medium Priority
?
1,247 Views
Last Modified: 2012-05-10
Hi there,

I need to replace a the word "SAISON" from a text file to the word "PERIOD+".

SCRIPT TO FIX:

@echo off
setlocal enabledelayedexpansion

FOR "delims=" %%A in (%File_LOG_Mirror%) do (
      ECHO !%%A:SAISON=PERIOD^+!>>%File_LOG_Mirror_TEMP%
      DEL %File_LOG_Mirror%
      REN %File_LOG_Mirror_TEMP% %File_LOG_Mirror%
)

exit


Thanks,
Rene
0
Comment
Question by:ReneGe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +2
21 Comments
 
LVL 16

Assisted Solution

by:Bryan Butler
Bryan Butler earned 80 total points
ID: 33478721
Can you use WSH or powershell?  Or any other language?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33478741
u mean the file name should be manipulated or the file content?
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33478751
Content. Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:ReneGe
ID: 33478759
==> NO. just batch file.

Thanks
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33478765
==> developedtester

NO. just batch file.

Thanks
0
 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 120 total points
ID: 33478812
here's a vb script which does that.replace TEXT_FILE with the file path you wish to manipulate.
const TEXT_FILE = "c:\temp\1.txt"
const FIND_STRING = "SAISON"
const REPLACE_STRING = "PERIOD+"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(TEXT_FILE, 1)
fileContent = objFile.ReadAll
objFile.Close

set objFile = objFSO.OpenTextFile(TEXT_FILE, 2)
objFile.Write Replace(fileContent, FIND_STRING, REPLACE_STRING)
objFile.Close

Open in new window

0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33478867
for batch file solution check http://www.dostips.com/?t=Batch.FindAndReplace
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 33478875
Looks like you are getting some good help on a scripting approach.  But an alternative is to use a small general purpose search and replace type utility, like the one mentioned in this therad.  There are a few of these around.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_23185721.html

~bp
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 33478928
If you know there are no funny characters, or blank lines in the file, then this may get the job done:

@echo off
setlocal enabledelayedexpansion

FOR "delims=" %%A in (%File_LOG_Mirror%) do (
      set Line=%%A
      ECHO !Line:SAISON=PERIOD^+!>>"%File_LOG_Mirror%._temp"
      DEL "%File_LOG_Mirror%"
      REN "%File_LOG_Mirror%._temp" "%File_LOG_Mirror%"
)

exit

~bp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33480446
it's all very higgledy-piggledy...

firstly, as imaginative as it is, you can't do !%%a:abc=xyz!. I note we'd still have to rely on delayed expansion if it were possible. For that reason, %%a needs to be assigned to a variable first. Like this:

   set var=%%a
   echo !var:abc=xyz!

prew, if that works I'll take my hat off to you. Obviously, blank lines are not preserved.

As for the renaming thing... how about something like this:

BTW, AFAIK, you can't use DELIMS without 'FOR /F'.

Furthermore, because there are no double-quotes around %File_LOG_Mirror% in the FOR statement, I can only assume it contains no spaces. That being the case, the DEL and REN commands don't require them either.

However, to bo on the safe side, I would prefer to use USEBACKQ and  double-quotes (See next comment).



@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%a in (%File_LOG_Mirror%) do (
      set line=%%a
      echo !line:SAISON=PERIOD^+!>>%File_LOG_Mirror%.tmp
)

move /y %File_LOG_Mirror%.tmp %File_LOG_Mirror%
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 800 total points
ID: 33480457
Uses USEBACKQ


@echo off
setlocal enabledelayedexpansion

for /f "usebackq delims=" %%a in ("%File_LOG_Mirror%") do (
      set line=%%a
      echo !line:SAISON=PERIOD^+!>>"%File_LOG_Mirror%.tmp"
)

move /y "%File_LOG_Mirror%.tmp" "%File_LOG_Mirror%"
0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 1000 total points
ID: 33481151
Ah yes, that pesky bracket was in the wrong place!  Just to be clear it should look like this.

@echo off
setlocal enabledelayedexpansion

set File_LOG_Mirror=EE26415780.txt

FOR /F "usebackq delims=" %%A in ("%File_LOG_Mirror%") do (
    set Line=%%A
    ECHO !Line:SAISON=PERIOD^+!>>"%File_LOG_Mirror%._temp"
)
DEL "%File_LOG_Mirror%"
REN "%File_LOG_Mirror%._temp" "%File_LOG_Mirror%"

exit /b

~bp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33481266
Come clean with me bill.... you were inspired by my previous comment! I like that. Ha ha ha...
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33481272
Just pulling your leg prew...
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 33481343
It's all good.

~bp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33481410
:)
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33481851
*** TO ALL OF YOU GUYS ***
*I first wish to tell you that I am more than impressed.
*Not only you were extremly proactive, but you worked together with a touch of magic.
********************************

The following last script from billprew (that was inspired by the previous comment of the leg puller t0t0...;-)worked without a glitch.

Thanks to you all. You are my heros!!

I just wished I could give more than 500 pts...

Cheers,
Rene
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33482467
Pleasing feedback.

Pity I missed the definition of %File_LOG_Mirror% - I genuinely thought this was defined elsewhere.

Not to worry though. We got there in the end. It's great to be credited. Thank you.

The challenge far outweighs the points.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 33484159
Thanks Rene.

~bp
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 33484535
Very kind Rene.  I wish you could give more points too!  lol  Glad we could help, or should I say they ;
Cheers,
dt
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33491078
10x man
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question