10

I have a .txt file in which I want to replace the string aaa with bbb.

I have tried the following PowerShell code:

Get-Content c:\1.txt | ForEach-Object { $_ -replace "aaa", "bbb" } | Set-Content c:\1.txt

I get an error that 1.txt is being used by another process. What am I doing wrong?

16
(get-content c:\1.txt) | foreach-object {$_ -replace "prod", "qa1"} | set-content c:\1.txt

The parentheses around Get-Content ensure that the Get operation is completed before the Set operation begins, without this the two functions would both try to access the file at the same time.

0

I am wondering how it comes that when I make a usefull tool for someone that may be in desperate need of replacing a string in a file and uploading this on a free upload site, downloadable free of charge, a free tool indeed, again, how it comes that I become a spammer??? I am a very respectable, old programmer in my country, nobody called me a spammer by now. Now I must upload the source code to maintain my prestige...

Now I realized that COMMUNITY is a robot and that's ok with me...

Source is compilable with FPC.

Program ReplaceString;
var
  fs,fd:file of char;
  rdchar:char;
  cnt1:byte;
  ocur:longint;
  instr,outstr,tempstr:string;

function fileexists(filename:string):boolean;
var
  f:file;
begin
  {$I-} assign(f,FileName);reset(f);close(f); {$I+}
  fileexists:=(ioresult=0) and (filename<>'');
end;

procedure chkstr;
var
  tmp1:char;
  numread,cnt2:byte;
begin
  numread:=1;
  tempstr[1]:=rdchar;
  repeat
    read(fs,tmp1);
    inc(numread);
    tempstr[numread]:=tmp1;
  until (numread=length(instr)) or (instr[numread]<>tempstr[numread]);
  if (numread<length(instr)) or (instr[numread]<>tempstr[numread]) then
    for cnt2:=1 to numread do begin
      tmp1:=tempstr[cnt2];
      write(fd,tmp1);
    end else begin
      for cnt2:=1 to length(outstr) do begin
        tmp1:=outstr[cnt2];
        write(fd,tmp1);
      end;
      inc(ocur);
      write('Occurrences: ',ocur);
      for cnt2:=1 to 20 do write(chr(8));
    end;
end;

begin
  writeln('File string replacer v.1.0 for WIN32 by Rares Atodiresei (P)2018');
  if paramcount<>3 then begin
    writeln('Usage: Repstr32.exe <infile> <target string> <replacement string>');
    halt(1);
  end;
  if not fileexists(paramstr(1)) then begin
    writeln('File ',paramstr(1),' does not exist.');
    halt(1);
  end;
  writeln;
  ocur:=0;
  write('Searching...');
  for cnt1:=1 to 15 do write(chr(8));
  instr:=paramstr(2);
  outstr:=paramstr(3);
  assign(fs,paramstr(1));reset(fs);
  assign(fd,'_'+paramstr(1));rewrite(fd);
  while not eof(fs) do begin
    read(fs,rdchar);
    if rdchar=instr[1] then chkstr else write(fd,rdchar);
  end;
  close(fd);close(fs);
  if ocur=0 then begin
    write('Target string not found.');
    for cnt1:=1 to 25 do write(chr(8));
    erase(fd);
  end else begin
    erase(fs);rename(fd,paramstr(1));
  end;
  writeln;writeln;writeln('Done.');
end.
  • This seems to be some kind of Pascal, not Powershell. – RalfFriedl Nov 1 '18 at 10:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.