Remove 1st line and last line from text file and rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Remove 1st line and last line from text file and rename

#1 Post by zagix » 16 Oct 2013 23:36

Hi,
I have about 50 text files in a folder and which is on daily basis i receive. The format of file is below i want the first line and last line to be deleted and rename the file on first line last three digits.

Text file format

1815102012 100099000
1154720520034900200434500900000000353000100099010
1155720520035000200434600900000000764600100099010
12000001 0000000353000100099000

The file to be renamed as 010.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove 1st line and last line from text file and rename

#2 Post by foxidrive » 17 Oct 2013 01:06

This uses a helper batch file called findrepl.bat from - viewtopic.php?f=3&t=4697

Put findrepl.bat in the same folder with this batch file and your .txt files.

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /b /a-d *.txt ') do (
   call findrepl  /o:+2:-2 <"%%a" >"%%a.tmp"
   set /p "var=" <"%%a.tmp"
      setlocal enabledelayedexpansion
        ren "%%a.tmp" "!var:~-3!.txt"
      endlocal
)

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Remove 1st line and last line from text file and rename

#3 Post by zagix » 18 Oct 2013 02:00

Thanks foxidrive, perfect 100%.
Thanks

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Remove 1st line and last line from text file and rename

#4 Post by zagix » 18 Oct 2013 13:30

Sorry foxidrive, I made a haste in reply, Infact my text files have trailing spaces at the end of lines which gives the result of renaming empty.

How can we fix that? Please excuse me.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove 1st line and last line from text file and rename

#5 Post by foxidrive » 18 Oct 2013 14:21

This relies on the fact that the line is all numbers, no spaces or commas etc.

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /b /a-d *.txt ') do (
   call findrepl  /o:+2:-2 <"%%a" >"%%a.tmp"
   set /p "var=" <"%%a.tmp"
      setlocal enabledelayedexpansion
        for %%b in (!var!) do set "var=%%b"
        ren "%%a.tmp" "!var:~-3!.txt"
      endlocal
)
pause

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Remove 1st line and last line from text file and rename

#6 Post by zagix » 21 Oct 2013 01:25

Hi foxidrive,

I have checked and tested it's perfect. Thanks a lot.
Well if you support i have one more query in this, I want to remove the first two digits leaving next 6 fields and delete all after that, and move to D:\cleandata
Sample file
1154720520034900200434500900000000353000100099010
1155720520035000200434600900000000764600100099010
After removng the fields
547205
557205

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove 1st line and last line from text file and rename

#7 Post by foxidrive » 21 Oct 2013 04:24

This will process the files already done, and keep columns 3 to 8 in every line, and create/append the files in d:\cleandata

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%z in (*.txt) do for /f "usebackq delims=" %%a in ("%%z") do (
    set "line=%%a"
    >>"d:\cleandata\%%z" echo !line:~2,6!
)
echo.
echo done
echo.
endlocal
pause

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Remove 1st line and last line from text file and rename

#8 Post by zagix » 24 Oct 2013 00:31

Thanks foxidrive.

Post Reply