Issue with script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
message1
Posts: 4
Joined: 21 Aug 2012 13:42

Issue with script

#1 Post by message1 » 21 Aug 2012 13:56

I am trying to replace '12345' with '56789' in a text file.
For this I am calling the following command in run_rename.bat file:

Code: Select all

for /f "usebackq delims==" %%a IN (`dir /b *4869*.txt`) do call renameFile.bat %%a


And the renamefile.bat has the following code:

Code: Select all

'net use P:\

CD C:\temp

REM
REM

setLocal DisableDelayedExpansion

REM Rename File to X.xml to enable change of text
if exist X.xml del X.xml
REM ren email_4869_12345.txt X.xml
ren %1 X.xml

REM @echo off > XXX
setLocal DisableDelayedExpansion

for /f "tokens=* delims= " %%G in (X.xml) do ( set str=%%G


setLocal EnableDelayedExpansion

REM set the string "12345 " to be "56789 "
set str=!str:12345 =56789 !

REM generate a new file with the rename in place
REM >> email_4869_12345.txt echo(!str!)
>> %1 echo(!str!)

del X.xml


This code is chopping off '!' from the text file and replacing nothing and taking the character off.
For example:
if the file has 'when!', the script is deleting '!' and taking the position off in the generated file.
Can anybody suggest where is it going or suggest me how to suppress this?


Thanks.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Issue with script

#2 Post by abc0502 » 21 Aug 2012 14:07

so all what you are trying to do is to change a part of a file name with another part, is that right?

message1
Posts: 4
Joined: 21 Aug 2012 13:42

Re: Issue with script

#3 Post by message1 » 21 Aug 2012 14:11

Yes.
12345 with 56789.


Thanks.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Issue with script

#4 Post by abc0502 » 21 Aug 2012 14:12

I guess this code will do that for the renaming part,

Code: Select all

For /F "tokens=1,2,3* delims=_" %%a in ("email_4869_12345.txt") Do Ren "email_4869_12345.txt" "%%a_56789_%%c"

message1
Posts: 4
Joined: 21 Aug 2012 13:42

Re: Issue with script

#5 Post by message1 » 21 Aug 2012 14:17

Sorry, I made you understand the issue wrongly.


I am trying to process the data in the .txt file and i am processing it and not trying to rename the file name just.


Thanks

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Issue with script

#6 Post by abc0502 » 21 Aug 2012 14:19

Then what you are trying to do is "replace" text in a file with another text
Last edited by abc0502 on 21 Aug 2012 14:24, edited 3 times in total.

message1
Posts: 4
Joined: 21 Aug 2012 13:42

Re: Issue with script

#7 Post by message1 » 21 Aug 2012 14:23

Yes

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Issue with script

#8 Post by abc0502 » 21 Aug 2012 14:24

IF so, have alook at this
http://www.dostips.com/?t=Batch.FindAndReplace
and also this is somthing simmelar to what you was trying to do
http://www.computing.net/answers/programming/batch-file-to-replace-a-word/15770.html

and this
http://www.computing.net/answers/programming/batch-file-to-replace-a-word/15770.html

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Issue with script

#9 Post by foxidrive » 21 Aug 2012 23:12

Here's a WSH method.

Write your batch file to call this one.

@echo off
call SAR.BAT "input.txt" "output.txt" "12345" "56789"



Code: Select all

::Search and replace
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs

Post Reply