剪切参数中给出的文件

时间:2016-08-30 13:24:50

标签: powershell batch-file

我对Batch或Powershell有点新意,所以我来这里是为了得到你的帮助。

我有一个由某些文件组成的文件,我想在不同的文件中剪切它们。

大文件看起来像这样

X1
Y1
LINE1 1
LINE1 2
...
LINE1 8
FILELINE1 1
FILELINE1 2
...
FILELINE1 X1*Y1
X2
Y2
LINE2 1
LINE2 2
...
LINE2 8
FILELINE2 1
FILELINE2 2
...
FILELINE2 X2*Y2

等等。 我正在尝试为每个人创建一个新文件。 (所以,对于我的例子,我将有1个文件包含第一个X1 * Y1 + 10行,第二个文件将包含X1 * Y1 + 10 + 1到X1 * Y1 + 10 + 1 + X2 * Y2 +第10行)。

我的第一个想法是取X1,删除文件的第一行,取Y1,然后再删除第一行,然后复制文件中的X1 * Y1 + 8第一行,删除第一行原始文件,依此类推,直到我找到EOF。

问题在于我不了解批处理的工作原理:/

(实际上我有代码:

   setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (%1) do (
    if !first!==1 echo %%i
    set X = %%i
    set first=0
)
more /E +1 %1 > %1.tmp
setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (%1.tmp) do (
    if !first!==1 echo %%i
    set Y = %%i
    set first=0
)

给我第一个X和第一个Y,但我认为它无处可去......

非常感谢您的阅读!

编辑: 我想创建包含以下内容的文件:

file1:

X1
Y1
LINE1 1
LINE1 2
...
LINE1 8
FILELINE1 1
FILELINE1 2
...
FILELINE1 X1*Y1

file2:

X2
Y2
LINE2 1
LINE2 2
...
LINE2 8
FILELINE2 1
FILELINE2 2
...
FILELINE2 X2*Y2

...

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你的文件由以下几个部分组成:

X
Y
<Text block>
<Second text block>

XY是整数,<Text block>由8行组成,<Second text block>XY组成线。

如果是这种情况,那么下面的PS代码就可以解决这个问题:

# Open the file
$reader = [System.IO.File]::OpenText("D:\test.log")
try{
    $x = -1
    $y = -1
    $linesToCopy = New-Object System.Collections.ArrayList
    $currentLine = 0
    $id = 0
    # Loop on the contents of the file
    for() {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
        if($x -eq -1)
        {
            # Store X as an integer, as well as in the "buffer"
            $x = [int]$line
            $linesToCopy.add($line)
        }
        elseif($y -eq -1)
        {
            # Store Y as an integer, as well as in the "buffer"
            $y = [int]$line
            $linesToCopy.add($line)
            $TotalLinesTocopy = $x*$y+8
            # We're going to copy the X*Y+8 following lines 
            # (we've already stored the first two ones)
            Write-Host "TotalLinesTocopy = $TotalLinesTocopy"
        }
        else
        {

            if($currentLine -lt $TotalLinesTocopy)
            {
                $linesToCopy.add($line)
                $currentLine++
            }
            else
            {
                # Dump the "buffer" to file
                $linesToCopy | Out-File -filepath ("d:\test{0}.log" -f $id) -Force
                # Reinitialize everything
                $id++
                $linesToCopy = New-Object System.Collections.ArrayList
                $currentLine = 0
                $x = [int]$line
                $y = -1
                $linesToCopy.add($line) 
            }
        }
    }
}finally{
    # During the final loop, we'll reach the finally block 
    # before dumping the final buffer
    # So, dump it now
    if($linesToCopy -ne $null)
    {
        $linesToCopy | Out-File -filepath ("d:\test{0}.log" -f $id) -Force
    }
    $reader.close()
}

旁注:我从不尝试批量做同样的事情,所以请不要问;)

答案 1 :(得分:0)

所以我做了,如果有人对这个方法感兴趣的话。我是使用批处理命令完成的:

@echo off
set "num=0"

REM copy the file, since we are modifying it, we'll need this one to recreate the file once it's finished
xcopy %1 %1.tmp

:again
REM if file is empty, we've done it so we can stop.
if "%~z1" == "" ( 
    echo File doesnt exist. 
) else if "%~z1" == "0" ( 
    goto stop
) else ( 
    echo File is not empty
)

REM take the first line
set /p firstline=<%1
echo %firstline%

REM create a tmp file where we have remove the first line
more +1 %1 > tmp.ptx 

REM take the second line 
set /p line=<tmp.ptx
echo %line%

REM del the tmp file
del aaaa.ptx

REM XY is the number of line we will copy
set /a "XY= %line% * %firstline%" + 10
echo %XY%
set /a num+=1

REM head is a script that give you the first X lines of a file.
call head %1 %XY% > "%num%.ptx"

REM create a file having only the little file remainging
more +%XY% %1 > tmp.ptx
del %1
REN tmp.ptx %1
goto again

REM once everything is finished, juste make it as if nothing happened...
:stop
del %1
REN %1.tmp %1
pause

和head.bat:

@powershell -command "& {get-content %1|select-object -first %2}"

我希望将来可以帮助某人。