批处理脚本:读取目录中的每个文件,替换一些字符串,将每个文件缩短为10行

时间:2021-11-26 02:29:30

I need help writing a batch script on Windows. My directory C:\OUTFiles contains 2355 .txt files with various lenghts which contain links to Wikipedia articles - for example a file called "Holzhausen.txt":

我需要帮助在Windows上编写批处理脚本。我的目录C:\ OUTFiles包含2355个.txt文件,其中包含各种长度,包含*文章的链接 - 例如名为“Holzhausen.txt”的文件:

http://de.wikipedia.org/wiki/[[Holzhausen (Langenpreising)]], Ortsteil der Gemeinde [[Langenpreising]] http://de.wikipedia.org/wiki/[[Holzhausen (Dähre)]], Ortsteil der Gemeinde [[Dähre]] ...

http://de.wikipedia.org/wiki/ [Holzhausen(Langenpreising)]],Ortsteil der Gemeinde [[Langenpreising]] http://de.wikipedia.org/wiki/ [[Holzhausen(Dähre)]], Ortsteil der Gemeinde [[Dähre]] ......

I would like to go through all files in C:\OUTFiles and cut the lenght of each file to 10 lines (or don't change the lenght if shorter then 10 lines).

我想浏览C:\ OUTFiles中的所有文件,并将每个文件的长度剪切为10行(如果短于10行,则不要更改长度)。

Additionally if a file contains [[some text]] like in the first line above I need to remove all brackets [[ ]].

另外,如果文件包含[[some text]],就像上面第一行一样,我需要删除所有括号[[]]。

How could I do it as batch script file on Windows? I am new to batch scripting and I searched * and tried to assemble a batch script but it is not quite done/working yet:

我怎么能在Windows上作为批处理脚本文件?我是批处理脚本的新手,我搜索了*并试图组装一个批处理脚本,但它还没有完成/工作:

@ECHO OFF
setlocal enabledelayedexpansion
set counter=1
for %%f in (*.txt) do call :p "%%f"
goto :eof

:p

SET /A maxlines=10
SET /A linecount=0

FOR /F %%A IN (*.txt) DO ( 
  IF !linecount! GEQ %maxlines% GOTO ExitLoop
  ECHO %%A 
  SET /A linecount+=1
)
SET /A counter+=1


:ExitLoop
:eof

PAUSE

Thank you a lot in advance!! Petra

非常感谢你提前!!佩特拉

1 个解决方案

#1


0  

This solution assumes that you buy my suggestion above to use powershell instead of batch.

此解决方案假设您购买上面的建议以使用powershell而不是批处理。

# this line assumes current directory - adjust to point to the actual location
$files = get-childitem *.txt
foreach ($file in $files) {
    $data = get-content $file
    $count = 1
    # this assumes that you want to put the modified 
    # output in a new file and keep the original file
    $newfile = $file.name + ".new.txt"
    foreach ($line in $data) {
        if($count -gt 10) {break}
        $line = $line -replace "[\[\]]",''
        out-file -filepath $newfile -inputobject $line -Append
        $count = $count + 1
    }
}

#1


0  

This solution assumes that you buy my suggestion above to use powershell instead of batch.

此解决方案假设您购买上面的建议以使用powershell而不是批处理。

# this line assumes current directory - adjust to point to the actual location
$files = get-childitem *.txt
foreach ($file in $files) {
    $data = get-content $file
    $count = 1
    # this assumes that you want to put the modified 
    # output in a new file and keep the original file
    $newfile = $file.name + ".new.txt"
    foreach ($line in $data) {
        if($count -gt 10) {break}
        $line = $line -replace "[\[\]]",''
        out-file -filepath $newfile -inputobject $line -Append
        $count = $count + 1
    }
}