4

I tried to copy one file to another from starting line upto a limit. ie., line 1 to 10(file1.txt)->file2.txt but while writing "!" is skipped. what can i do for to solve it. Any help will be thankful.

The loop for that function is given below.

%NF%-> new file.

%EF%-> existing file

%1% -> line number(passed from another part)

:_doit

findstr /N /R "." %EF%|findstr /B /C:"%1:">nul

if errorlevel 1 (    
    echo. >>%NF%        
) else (    
    for /f "tokens=1 delims=" %%a in ('findstr /N /R "." %EF%^|findstr /B /C:"%1:"') do (    
        if [%%a] EQU [] (    
            echo. >>%NF%    
        ) else (    
            echo %%a >>%NF%    
       )     
   )
)
0

4 Answers 4

3

If you can download tools, you can use GNU win32 gawk

gawk.exe "NR>10{exit}1"  file1 > file2

And you can take a look at this thread here that is similar

Sign up to request clarification or add additional context in comments.

2 Comments

yes, its just a command. So put that command inside your batch file.
Do you know what the reason for disappearing "!" symbol in batch file while writing from one file to another?
3

The reason is the delayed expansion, if you disable it, also the ! work as expected. You can disable it only for the loop.

if errorlevel 1 (    
    echo. >>%NF%        
) else (
   setlocal DisableDelayedExpansion
    for /f "tokens=1 delims=" %%a in ('findstr /N /R "." %EF%^|findstr /B /C:"%1:"') do (    
        if [%%a] EQU [] (    
            echo. >>%NF%    
        ) else (    
            echo %%a >>%NF%    
       )     
   )
   endlocal
)

The parser for batch lines has multiple phases: The first phase is the %var% expansion, then comes the special character phase "^<>&|() and after the %%a phase, the delayed (exclamation phase).

That's the reason why the ! dissappear in your case. Because you got something like this from your file %%a=Hello! Then the exclamation phase try to expand the !, but this fails and it is removed.

If in your file is the line Hello^! you got in your "copy" Hello!

But in a batch file you need two carets to display one !

echo hello^^!

Because in phase2, the ^^! is inflated to ^!, and in the exclamation phase the last caret escapes the !

4 Comments

But i placed the DisableDelayedExpansion in next else block to work properly.Thanks.
if errorlevel 1 ( echo. >>%NF% ) else ( for /f "tokens=1 delims=" %%a in ('findstr /N /R "." %EF%^|findstr /B /C:"%1:"') do ( if [%%a] EQU [] ( echo. >>%NF% ) else( setlocal DisableDelayedExpansion echo %%a >>%NF% endlocal ) ) )
Note that for /f will skip empty lines.
Yes, but there can't be empty lines, because findstr /N always prepend the line number
2
if errorlevel 1 ( 
    echo. >>%NF% 
) else ( 
    for /f "tokens=1 delims=" %%a in ('findstr /N /R "." %EF%^|findstr /B /C:"%1:"') do ( 
       if [%%a] EQU [] ( 
          echo. >>%NF% 
       ) else( 
          setlocal DisableDelayedExpansion 
          echo %%a >>%NF%
          endlocal
       ) 
    )
)

1 Comment

There are two problems, "echo." is two times slower as "echo(" and it fails if a file named "echo" exist. The second problem is with long files your code doesn't work, because you didn't use endlocal and therefore you get an overflow.
1

Maybe use gawk.exe from UnxUtils :

gawk "NR<10" < file1 > file2

1 Comment

Better GNU gawk : gnuwin32.sourceforge.net/packages/gawk.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.