tags:

views:

77

answers:

3

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%    
       )     
   )
)
A: 

Maybe use gawk.exe from UnxUtils :

gawk "NR<10" < file1 > file2
Benoit
Better GNU gawk : gnuwin32.sourceforge.net/packages/gawk.htm
ghostdog74
A: 

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

ghostdog74
Does it work in windows batch file?
Sarika
@Sarika: It works instead windows batch file.
abatishchev
yes, its just a command. So put that command inside your batch file.
ghostdog74
Do you know what the reason for disappearing "!" symbol in batch file while writing from one file to another?
Sarika
A: 

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 exlamation phase the last caret escapes the !

jeb
very very thanks for your valuable reply. I am so late..
Sarika
But i placed the DisableDelayedExpansion in next else block to work properly.Thanks.
Sarika
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 ) ) )
Sarika