JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Replace text in a file in batch script

Author:JIYIK Last Updated:2025/03/21 Views:

In this article, we will look at some ways to replace text in a file. We will look at two different ways.

Our first approach consists only of a batch script to perform the task, while the second approach provides a solution through Windows PowerShell.

Assume we have a text file with the following content.

Test.txt:

This is text that is saved in a text file. This is an update. ,,,,,,,,,,,,

We will replace these commands with null characters.


Replace text in a file using batch script

Batch script:

@echo off
FOR /f "tokens=*" %%s IN (Test.txt) DO (
  SET Texts=%%s
)
set Texts=%Texts:,=%

FOR /F "tokens=* delims=" %%x IN (Test.txt) DO SET text=%%x
ECHO %Texts% > "G:\BATCH\Test.txt" :: the path location of the txt file

We first FOR /f "tokens=*" %%s IN (Test.txt) DO (read the file using lines, then with SET Texts=%%slines, we initialize a string variable with the text of the file. We set Texts=%Texts:,=%go through lines replacing each comma with a null character.

Finally, we put the text into the file again. When we run the above code, we will see the following file content change.

This is text that is saved in a text file. This is an update.

Replace text in a file using Windows PowerShell

This method will also provide the same result as in our previous method. In this method, we have used PowerShell in a batch script. The sample code for this method is shown below,

powershell -Command "(gc Test.txt) -replace ',', '' | Out-File -encoding ASCII Test.txt"

When we run the code, we will see the changes to the file contents below.

This is text that is saved in a text file. This is an update.

Keep in mind that the commands we discuss here only work in the Windows Command Prompt, or CMD environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Running batch scripts in C#

Publish Date:2025/03/21 Views:139 Category:OPERATING SYSTEM

In this article, we will see how to write a C# program that can run a batch file from a directory. Running batch scripts in C# In C#, when we want to execute a batch file, it acts as a process. You can follow the sample code below to run a

Extract or unzip files in batch script

Publish Date:2025/03/21 Views:122 Category:OPERATING SYSTEM

In this article, we will see how to create a batch script to extract files from a zip file. Create a batch script to unzip the files We can extract or decompress files using batch scripts which need to contain the destination directory wher

Transferring files to Raspberry Pi using batch script

Publish Date:2025/03/21 Views:56 Category:OPERATING SYSTEM

This article will show a batch command that transfers a file from Windows to the Raspberry Pi environment and executes the file. PuTTY is a third-party free tool that implements Telnet and SSH for Windows and Unix platforms for free. It als

Renaming part of a file name in a batch script

Publish Date:2025/03/21 Views:122 Category:OPERATING SYSTEM

Sometimes we need to rename a series of files in a specific order. Most project files contain this sequence, and we can easily do this in a batch script using a simple one-line command. This short article will show us how to change a specif

Run batch scripts using Task Scheduler

Publish Date:2025/03/21 Views:152 Category:OPERATING SYSTEM

This article will show you how to use Task Scheduler to run a batch file. Run batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't nee

Mapping a network drive in a batch script

Publish Date:2025/03/21 Views:69 Category:OPERATING SYSTEM

This article will discuss how to map a network drive in a batch script. Mapping a network drive in a batch script For this purpose, we will see three formats of the same command. However, the general format of the command is: net use P: "\\

Copy files using batch script

Publish Date:2025/03/21 Views:91 Category:OPERATING SYSTEM

Batch scripts are scripts built specifically for the Windows command line to perform a variety of tasks and execute user-directed sequences of commands. Batch scripts make it easy to use Windows PowerShell. This article will explain how to

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial