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
Remove 1st line and last line from text file and rename
Moderator: DosItHelp
Re: Remove 1st line and last line from text file and rename
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.
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
)
Re: Remove 1st line and last line from text file and rename
Thanks foxidrive, perfect 100%.
Thanks
Thanks
Re: Remove 1st line and last line from text file and rename
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.
How can we fix that? Please excuse me.
Re: Remove 1st line and last line from text file and rename
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
Re: Remove 1st line and last line from text file and rename
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
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
Re: Remove 1st line and last line from text file and rename
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
Re: Remove 1st line and last line from text file and rename
Thanks foxidrive.