Batch remove first character of all text files in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tiro_silex
Posts: 1
Joined: 11 May 2015 05:18

Batch remove first character of all text files in a folder

#1 Post by tiro_silex » 11 May 2015 06:32

Hey,

I have a series of files (*.mail, but they are essentially *.txt) in a single folder.
I would like a batch script to delete the first character (which should always be a "-") within each text file (so not editing the filename, but the file's content).
(Optionally it can delete all "-" characters in the text files, but I would prefer to just delete the first one).
I'd like this edited straight in the existing file, no output files, etc.
For now I have been using Notepad++'s "Find in Files"-function, which works well, but I would prefer a batch-based solution.

Can someone help me out?
Thanks! :-)

Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: Batch remove first character of all text files in a fold

#2 Post by Ben Mar » 11 May 2015 10:58

Just try this:

Code: Select all

@echo off
for %%a in (*.txt) do @(
   findrepl "^-" "" < %%a > %%~na.tmp
   del %%a
   ren %%~na.tmp %%a
)

This uses a native Windows batch script called findrepl.bat (by aacini)
It can also be found here: viewtopic.php?f=3&t=4697
Place it in the same folder as the batch file, or in a folder that is on the system path.
Last edited by Ben Mar on 12 May 2015 00:15, edited 2 times in total.

dbenham
Expert
Posts: 2270
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch remove first character of all text files in a fold

#3 Post by dbenham » 11 May 2015 14:11

You can use JREPL.BAT. The following works from the command line - no batch required:

Code: Select all

for %F in (*.txt) do @jrepl "^-" "" /jendln "skip=true" /f "%F" /o -

If used in a batch script, then

Code: Select all

@echo off
for %%F in (*.txt) do call jrepl "^-" "" /jendln "skip=true" /f "%%F" /o -


Dave Benham

Post Reply