CMD读取另一个文本文件中指定的文本

时间:2021-04-25 02:08:12

Hi how to read files in another text files. I have text file with path-names like

您好如何读取其他文本文件中的文件。我有像路径名这样的文本文件

d:\cifs\fslit.txt with content:

d:\ cifs \ fslit.txt,内容如下:

d:\cifs\katowice\cifs_list.txt 
d:\cifs\krakow\cifs_list.txt 
d:\cifs\gdansk\cifs_list.txt 
d:\cifs\berlin\cifs_list.txt 

etc could be hundreds lines of these.

等等可能是数百行。

in everyone of these files we have paths like "d:\cifs\katowice\cifs_list.txt":

在这些文件的每个人中,我们都有“d:\ cifs \ katowice \ cifs_list.txt”之类的路径:

\\katowice\cifs\project\current\box\proj1 
\\katowice\cifs\project\current\box\ide1 
\\katowice\cifs\project\current\box\area_b 
\\katowice\cifs\project\current\box\ide2 

etc I would like to put all paths (content) of these files together to one file and remove doublets.

等我想把这些文件的所有路径(内容)放在一个文件中,然后删除doublets。

happy with any suggestions ....

对任何建议感到高兴....

1 个解决方案

#1


0  

Here is one option for you. It does come with some caveats. If your file lists do not end with a line ending, the last line from the previous file and first line from the next file, will be on the same line in the output file.

这是一个选项。它确实带有一些警告。如果文件列表不以行结尾结束,则前一个文件的最后一行和下一个文件的第一行将在输出文件的同一行中。

@echo on

REM Read list of files
(FOR /F "usebackq delims=" %%G IN ("d:\cifs\fslit.txt") DO (
    REM output all files to a single combined file
    TYPE "%%~G"
))>combined.txt
REM Call out to function to remove duplicates from file.
CALL :DEDUPE "combined.txt"
GOTO :EOF

:DEDUPE
:: Remove duplicates from file
setlocal disableDelayedExpansion
set "file=%~1"
set "sorted=%file%.sorted"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^


::The 2 blank lines above are critical, do not remove
sort "%file%" >"%sorted%"
>"%deduped%" (
  set "prev="
  for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%sorted%") do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    if /i "!ln!" neq "!prev!" (
      endlocal
      (echo %%A)
      set "prev=%%A"
    ) else endlocal
  )
)
>nul move /y "%deduped%" "%file%"
del "%sorted%"
GOTO :EOF

#1


0  

Here is one option for you. It does come with some caveats. If your file lists do not end with a line ending, the last line from the previous file and first line from the next file, will be on the same line in the output file.

这是一个选项。它确实带有一些警告。如果文件列表不以行结尾结束,则前一个文件的最后一行和下一个文件的第一行将在输出文件的同一行中。

@echo on

REM Read list of files
(FOR /F "usebackq delims=" %%G IN ("d:\cifs\fslit.txt") DO (
    REM output all files to a single combined file
    TYPE "%%~G"
))>combined.txt
REM Call out to function to remove duplicates from file.
CALL :DEDUPE "combined.txt"
GOTO :EOF

:DEDUPE
:: Remove duplicates from file
setlocal disableDelayedExpansion
set "file=%~1"
set "sorted=%file%.sorted"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^


::The 2 blank lines above are critical, do not remove
sort "%file%" >"%sorted%"
>"%deduped%" (
  set "prev="
  for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%sorted%") do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    if /i "!ln!" neq "!prev!" (
      endlocal
      (echo %%A)
      set "prev=%%A"
    ) else endlocal
  )
)
>nul move /y "%deduped%" "%file%"
del "%sorted%"
GOTO :EOF