使用批处理脚本读取文本文件并以不同顺序将特定文本复制到新文本文件中
Read text file and copy specific text in different order into new text file using a batch script
希望就一个非常具体的问题寻求帮助。我是批处理编程的新手,想使用批处理文件归档以下内容。
读取文本文件 1 的所有行,并将值以不同的顺序放入文本文件 2 中 header 行。
示例:textfile1(输入文件)
我想从这个文件中逐行阅读并只选择某些内容。结构是一种 label="Value"spacelabel"Value"space 等每一行(试图在下面演示):
Supplier CountryName="GB" SupplierNumber="1112|DISCOUNT|497" Street="ANDERTON HOUSE" CountryCoded="GB" Name1="ANDERTON BOARD AND PACKAGING" CorporateGroupID="497"
Supplier CountryName="GB" SupplierNumber="113093|AMB HEAD OFFICE|846" Street="Langcliffe Paper Mills" CountryCoded="GB" Name1="JOHN ROBERTS HOLDINGS LTD" CorporateGroupID="846"
示例:textfile2(输出文件)
第一行应该是 header 行,每个标签由 TAB 分隔,在下面我想为每个字段写入在 textfile1 中找到的值。
如果某个字段没有值,那么我想在添加新找到的值之前添加一个 TAB。
如您所见,我只想选择引号之间的值,别无其他。
textfile1 的结构始终相同(顺序不变),标签也是如此。每行始终以 Supplier CountryName= 开头
只是想知道这是否可以使用批处理文件。
SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded
1112|DISCOUNT|497 497 ANDERTON BOARD AND PACKAGING GB
113093|AMB HEAD OFFICE|846 846 JOHN ROBERTS HOLDINGS LTD GB
对此的任何意见都将非常有帮助,谢谢。
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65822358.txt"
SET "outfile=%destdir%\outfile.txt"
:: Special characters
:: the variable TAB should be set to a tab character. This may not be copy/pastable from SO code to editors.
:: Make sure that this is a single TAB as intended, not a space-sequence
SET "tab= "
SET "columnsrequired=SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded"
SET "header="
FOR %%c IN (%columnsrequired%) DO SET "%%c="&SET "header=!header!%tab%%%c"
(
ECHO %header:~1%
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
FOR %%c IN (%columnsrequired%) DO SET "%%c="
SET "lineread=%%b"
FOR %%c IN (%columnsrequired%) DO (SET "%%c=!lineread:*%%c=!")
rem FOR %%c IN (%columnsrequired%) DO ECHO %%c is '!%%c!'
rem each variable now contains either the full original line or '="required value" waffle...`
FOR %%c IN (%columnsrequired%) DO (
SET "lineread=!%%c!"
IF "!lineread:~0,1!"=="=" (
rem change " to \ as " is a special character in batch
SET "lineread=!lineread:"=\!"
rem ECHO lineread "!lineread!"
FOR /f "tokens=2delims=\" %%x IN ("!lineread!") DO SET "%%c=%%x"
) ELSE (SET "%%c=")
)
SET "header="
FOR %%c IN (%columnsrequired%) DO SET "header=!header!%tab%!%%c!"
ECHO !header:~1!
)
)>"%outfile%"
GOTO :EOF
您的“问题”只是一个代码请求,通常会在本论坛上删除...但是,我有一些空闲时间,所以这里是您请求的解决方案:
@echo off
setlocal EnableDelayedExpansion
rem Define output fields
set "fields=SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded"
rem Get TAB character
for /F "delims=pR tokens=1,2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%b"
rem Assemble Header and Output "format" lines, and initialize fields
set "header="
set "output="
for %%a in (%fields%) do (
set "header=!header!%%a!TAB!"
set "output=!output!^!%%a:~1,-1^!!TAB!"
set "%%a="
)
rem Process the file
(
echo %header:~0,-1%
for /F "tokens=1*" %%a in (textfile1.txt) do (
call :GetFields %%b
echo %output:~0,-1%
)) > textfile2.txt
goto :EOF
:GetFields
set line=%*
set %line:" ="&set %
exit /B
textfile1.txt:
Supplier CountryName="GB" SupplierNumber="1112|DISCOUNT|497" Street="ANDERTON HOUSE" CountryCoded="GB" Name1="ANDERTON BOARD AND PACKAGING" CorporateGroupID="497"
Supplier CountryName="GB" SupplierNumber="113093|AMB HEAD OFFICE|846" Street="Langcliffe Paper Mills" CountryCoded="GB" Name1="JOHN ROBERTS HOLDINGS LTD" CorporateGroupID="846"
textfile2.txt:
SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded
1112|DISCOUNT|497 497 ANDERTON BOARD AND PACKAGING GB
113093|AMB HEAD OFFICE|846 846 JOHN ROBERTS HOLDINGS LTD GB
希望就一个非常具体的问题寻求帮助。我是批处理编程的新手,想使用批处理文件归档以下内容。
读取文本文件 1 的所有行,并将值以不同的顺序放入文本文件 2 中 header 行。
示例:textfile1(输入文件) 我想从这个文件中逐行阅读并只选择某些内容。结构是一种 label="Value"spacelabel"Value"space 等每一行(试图在下面演示):
Supplier CountryName="GB" SupplierNumber="1112|DISCOUNT|497" Street="ANDERTON HOUSE" CountryCoded="GB" Name1="ANDERTON BOARD AND PACKAGING" CorporateGroupID="497"
Supplier CountryName="GB" SupplierNumber="113093|AMB HEAD OFFICE|846" Street="Langcliffe Paper Mills" CountryCoded="GB" Name1="JOHN ROBERTS HOLDINGS LTD" CorporateGroupID="846"
示例:textfile2(输出文件) 第一行应该是 header 行,每个标签由 TAB 分隔,在下面我想为每个字段写入在 textfile1 中找到的值。 如果某个字段没有值,那么我想在添加新找到的值之前添加一个 TAB。 如您所见,我只想选择引号之间的值,别无其他。 textfile1 的结构始终相同(顺序不变),标签也是如此。每行始终以 Supplier CountryName= 开头 只是想知道这是否可以使用批处理文件。
SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded
1112|DISCOUNT|497 497 ANDERTON BOARD AND PACKAGING GB
113093|AMB HEAD OFFICE|846 846 JOHN ROBERTS HOLDINGS LTD GB
对此的任何意见都将非常有帮助,谢谢。
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65822358.txt"
SET "outfile=%destdir%\outfile.txt"
:: Special characters
:: the variable TAB should be set to a tab character. This may not be copy/pastable from SO code to editors.
:: Make sure that this is a single TAB as intended, not a space-sequence
SET "tab= "
SET "columnsrequired=SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded"
SET "header="
FOR %%c IN (%columnsrequired%) DO SET "%%c="&SET "header=!header!%tab%%%c"
(
ECHO %header:~1%
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
FOR %%c IN (%columnsrequired%) DO SET "%%c="
SET "lineread=%%b"
FOR %%c IN (%columnsrequired%) DO (SET "%%c=!lineread:*%%c=!")
rem FOR %%c IN (%columnsrequired%) DO ECHO %%c is '!%%c!'
rem each variable now contains either the full original line or '="required value" waffle...`
FOR %%c IN (%columnsrequired%) DO (
SET "lineread=!%%c!"
IF "!lineread:~0,1!"=="=" (
rem change " to \ as " is a special character in batch
SET "lineread=!lineread:"=\!"
rem ECHO lineread "!lineread!"
FOR /f "tokens=2delims=\" %%x IN ("!lineread!") DO SET "%%c=%%x"
) ELSE (SET "%%c=")
)
SET "header="
FOR %%c IN (%columnsrequired%) DO SET "header=!header!%tab%!%%c!"
ECHO !header:~1!
)
)>"%outfile%"
GOTO :EOF
您的“问题”只是一个代码请求,通常会在本论坛上删除...但是,我有一些空闲时间,所以这里是您请求的解决方案:
@echo off
setlocal EnableDelayedExpansion
rem Define output fields
set "fields=SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded"
rem Get TAB character
for /F "delims=pR tokens=1,2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%b"
rem Assemble Header and Output "format" lines, and initialize fields
set "header="
set "output="
for %%a in (%fields%) do (
set "header=!header!%%a!TAB!"
set "output=!output!^!%%a:~1,-1^!!TAB!"
set "%%a="
)
rem Process the file
(
echo %header:~0,-1%
for /F "tokens=1*" %%a in (textfile1.txt) do (
call :GetFields %%b
echo %output:~0,-1%
)) > textfile2.txt
goto :EOF
:GetFields
set line=%*
set %line:" ="&set %
exit /B
textfile1.txt:
Supplier CountryName="GB" SupplierNumber="1112|DISCOUNT|497" Street="ANDERTON HOUSE" CountryCoded="GB" Name1="ANDERTON BOARD AND PACKAGING" CorporateGroupID="497"
Supplier CountryName="GB" SupplierNumber="113093|AMB HEAD OFFICE|846" Street="Langcliffe Paper Mills" CountryCoded="GB" Name1="JOHN ROBERTS HOLDINGS LTD" CorporateGroupID="846"
textfile2.txt:
SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded
1112|DISCOUNT|497 497 ANDERTON BOARD AND PACKAGING GB
113093|AMB HEAD OFFICE|846 846 JOHN ROBERTS HOLDINGS LTD GB