• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Removing substring in a file with batch

Hi there;

I have a file having contents as:
http://sharepoint.company/foo/baz.html
http://sharepoint.company/bar.htm

I am trying to remove http://sharepoint.company/ parts in a batch, yet I am not successfuly.

Could you fix my code please?

Best regards.
for /f "tokens=* delims=" %%a in (myfile.txt) do (
set str=%%a
set str=%str:http=%
ECHO %str% >> filecopy.txt)
PAUSE

Open in new window

0
jazzIIIlove
Asked:
jazzIIIlove
  • 2
3 Solutions
 
knightEknightCommented:
Download this command-line search/replace tool:  http://www.paulslore.com/utils/chgstr.zip

Then you can remove the string like this:

   chgstr.exe  "http://sharepoint.company/"  ""  myfile.txt
0
 
knightEknightCommented:
It will also work on multiple files and it will traverse sub-folders:

  chgstr.exe  "http://sharepoint.company/"  ""   *.txt  /s
0
 
Shift-3Commented:
Generally speaking, you shouldn't use batch scripting for text editing.  It strips blank lines and chokes on special characters.  However, if you really must, here's how you could do it.


@echo off
setlocal enabledelayedexpansion
 
set input=myfile.txt
set output=filecopy.txt
 
for /f "tokens=*" %%G in ('type "%input%"') do (
 set line=%%G
 set line=!line:http://sharepoint.company/=!
 echo !line! >> "%output%"
)
 
pause

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now