Help with simple code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 17 Apr 2019 13:00

Help with simple code

#1 Post by AlbRodriguez » 17 Apr 2019 13:02

Dear friends,

I am using a code to delete headers (line 1 to 82) from all .txt files inside one folder. The thing is this code works for relatively small files, but when I try to use on big files, the code does nothing. I think it is because it is using temporary files.

Can someone help me modify this code to not use temporary files and just delete on each one of .txt file from line 1 to 82? Thank you!

Code: Select all

@echo off
for %%f in (*.txt) do (
    more +82 "%%f" > "%TEMP%\%%f"
    move /y "%TEMP%\%%f" "%%f" > nul
echo Done.

Posts: 192
Joined: 04 Mar 2014 11:14
Location: germany

Re: Help with simple code

#2 Post by pieh-ejdsch » 22 Apr 2019 06:01

Hallo AlbRodriguez,

this is certainly due to the number of lines.
From 65534 lines you need a confirmation with the space bar for each of the next 65534 lines.
The creation of a temporary file can not be avoided.
But you can bypass the keypress.
As many small files are created as are needed until the end (ie each file 65534 lines).
After a short wait - all files must be ready - these files will be joined together.
Subsequently, the processes are stopped.

Code: Select all

@echo off
if .%1 == ./read goto :read
if .%1 == ./write goto :write

set "file=D:\output2"
 rem up to 2 g lines
set /a skip=1700000
set "out=D:\newfile"

set "tempout=D:\output_"

echo check file ...
for /f %%i in (' find /c /v "" ^<%file% ') do set "N=%%i"
2>nul del "%out%%N%Y"
2>nul del "%out%%N%X"
echo wait for creating seperate files ...
if .%1 == . start "read" /wait 2>nul cmd /c "2>nul "%~f0" /read | "%~f0" /write"
echo new file created.
echo done
exit /b

setlocal enabledelayedexpansion
promt $g$s.
>nul ping localhost -n 1
for /l %%n in (%skip% 65534 %N% ) do (
  >&3 echo create file at line %%n ...
  2>nul start "%%n" /b cmd /c " <%file% more +%%n | >"%tempout%%%n" find /v "" " 2>nul
  for /l %%i in (1 40000 %%n) do @ >nul ping localhost -2 nul
>&3 echo Line %N% arrived ...
>&3 echo wait for all files ...
>>"%out%%N%Y" type nul
if NOT exist "%out%%N%X" >nul ping localhost -n 2 & goto :waitR
del "%out%%N%X"
exit /b

prompt $g$s-
echo please wait ...
>nul ping localhost -n 30
if NOT exist "%out%%N%Y" >nul ping localhost -n 3 & goto :waitW
echo skip lines -- merge file(s)
>"%out%" type nul
for /l %%n in (%skip% 65534 %N% ) do (
  >>"%out%" type "%tempout%%%n"
  for /l %%j in (1 40000 %%n) do @ >nul ping localhost -2 nul
2>nul >nul taskkill /f /im /im find.exe
for /l %%n in (%skip% 65534 %N% ) do 2>nul del "%tempout%%%n"
>>"%out%%N%X" type nul
2>nul del "%out%%N%Y"
exit /b

Post Reply