Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to replace the string in a file with file name. I am having 3000 files with different file names. Is there any way i can do with batch script I am completely new to scripting. please help me on this

eg: I am having a file name as 12345678.txt inside the file I am having some records along with a string 99999999. now i want to replace 99999999 with the file name 12345678.

I am having files in C:\DATA\

What I have tried:

I am completely new to scripting. please help me
Posted
Updated 30-Aug-18 6:22am
v2
Comments
CHill60 30-Aug-18 10:37am    
If it was a one-off I would probably use Notepad++ to do that for me rather than writing a script
suniti dinesh 30-Aug-18 10:57am    
Hi Chill. Thank you. I am having 3000 files and every file has different file names(1000001.txt, 1000002.txt....1003000.txt). how can i replace the data(99999999) in every file with the file number in notepad++. if we can do it please guide me how to do it.
RedDk 30-Aug-18 14:47pm    
Show your batch file code ... so far.

1 solution

Using PowerShell:
PowerShell
$files = Get-ChildItem -path C:\Data\ -filter *.txt
foreach ($file in $files)
{
    $content = Get-Content $file
    if ($content -match "99999999")
    {
        $content = $content -replace "99999999", $file.BaseName
        Set-Content $file $content
    }
}
 
Share this answer
 
Comments
suniti dinesh 30-Aug-18 15:11pm    
Hi richard thank you so much for your response. when i try to run powershell script. I am getting the below error
Get-Content : Cannot find path 'C:\Windows\system32\70002000.LDR' because it does not exist.
At C:\Script\Powershell.ps1:6 char:16
+ $content = Get-Content $file
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Windows\system32\70002000.LDR:String) [Get-Content], ItemN
otFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
suniti dinesh 30-Aug-18 15:13pm    
we are not mapping to the path where files are existing. we are mapping to system folder. please help me
Richard Deeming 30-Aug-18 15:18pm    
I thought you were looking at files in C:\Data\?

If you're looking at files in C:\Windows\System32\, you'll need to make sure you're running PowerShell elevated.

Also, if you've changed the Get-ChildItem call to return hidden files by using the -Force parameter, you'll probably need to pass the -Force parameter to the Get-Content call as well.
suniti dinesh 30-Aug-18 15:33pm    
Sorry for the confusion Richard. I have din't went to the path in powershell prompt. now i checked in the way you suggested. It worked like a champ. Thank you so much for you help
suniti dinesh 30-Aug-18 15:40pm    
one more small doubt like after fetching the value 9999999 from the file and if i want to increment the value with 1 (eg: 10000000,10000001,10000001...till files in the folders end) and replace it with incremented value instead of copying file name and replacing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900