所以我要做的是为多个人创建一个查找,它在文本文件中会显示名称和数字,比如
文本文件示例:
Beth
1234567891
Jay
2134456544
这是我能解释我想做什么的最好方法:
@echo off
set "file=Test1.txt"
setlocal EnableDelayedExpansion
<"!file!" (
for /f %%i in ('type "!file!" ^| find /c /v ""') do set /a n=%%i && for /l %%j in (1 1 %%i) do (
set /p "line_%%j="
)
)
set /a Name=1
set /a Number=2
Echo Line_%Name%> %Name%.txt (Im trying to get this to say line_2 to say 1st line in the text file)
Echo Line_%Number%> %Name%.txt (Im trying to get this to say line_2 to say 2nd line in the text file)
:Start
set /a Name=%Name%+2 (These are meant to take off after 1 so lines 3,5,7,9 so on)
set /a Number=%Number%+2 (These are meant to take off after 2 so lines 4,6,8,10 so on)
Echo Line_%Name%
Echo Line_%Number%
GOTO :Start
所以结果是在Beth.txt中:
Beth
1234567891
因此,每个名称都将是一个文件名和文件中的第一行。我稍后会更改它,这样我就可以在每个文本文件中添加一个。
Name: Beth
Number: 1234567891
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:your files"
SET "destdir=u:your results"
SET "filename1=%sourcedir%q65417881.txt"
rem make sure arrays are empty
For %%b IN (name number) DO FOR /F "delims==" %%a In ('set %%b[ 2^>Nul') DO SET "%%a="
rem Initialise counter and entry array
SET /a count=0
SET "number[0]=dummy"
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
IF DEFINED number[!count!] (SET /a count+=1&SET "name[!count!]=%%a") ELSE (SET "number[!count!]=%%a")
)
rem clear out dummy entry
SET "number[0]=dummy"
FOR /L %%c IN (1,1,%count%) DO (
rem replace spaces with dashes
SET "name[%%c]=!name[%%c]: =-!"
rem report to console rem report to console
ECHO Name: !name[%%c]! Number: !number[%%c]!
rem generate name.txt file
(
ECHO !name[%%c]!
ECHO !number[%%c]!
)>"%destdir%!name[%%c]!.txt"
)
GOTO :EOF
您需要更改分配给sourcedir
和destdir
的值以适应您的情况。该列表使用了适合我的系统的设置。
我故意在名称中包含空格,以确保空格得到正确处理。
我在测试中使用了一个名为q65417881.txt
的文件,其中包含您的数据。
从文件读取的行数据被交替地分配给%%a
和number[!count!]
。数据保留在这些数组中,以供进一步处理使用。
[编辑后包括将名称中的空格转换为短划线]
如果我理解正确的话,您希望在每一行前面加上Number:
+SPACE,并在另一行前面放上Name:
+SPACE。为此,您不需要首先将每一行存储在一个变量中,您可以使用单个for /F
循环逐行读取文件,并单独处理每一行。有两种可能性:
使用
findstr /N
:在每行前面临时加上行号和:
@echo off rem // Loop through lines and precede each with line number plus `:`: for /F "tokens=1* delims=:" %%K in ('findstr /N "^" "Test1.txt"') do ( rem // Calculate remainder of division by two: set /A "MOD=%%K%%2" 2> nul rem // Toggle delayed expansion to avoid issues with `!`: setlocal EnableDelayedExpansion rem // Conditionally return line string with adequate prefix: if !MOD! neq 0 ( endlocal & echo Name: %%L ) else ( endlocal & echo Number: %%L ) )
当一行以
:
开头时,这将失败。检查当前字符串的数字表示是否大于
0
:@echo off rem // Loop through (non-empty) lines: for /F "usebackq delims=" %%L in ("Test1.txt") do ( rem // Determine numeric representation of current line string: set /A "NUM=%%L" 2> nul rem // Toggle delayed expansion to avoid issues with `!`: setlocal EnableDelayedExpansion rem // Conditionally return line string with adequate prefix: if !NUM! equ 0 ( endlocal & echo Name: %%L ) else ( endlocal & echo Number: %%L ) )
当名称以数字开头和/或数字行为
0
时,此操作将失败。
只是为了发布一些不同的东西:
@SetLocal EnableExtensions DisableDelayedExpansion & (Set LF=^
% 0x0A %
) & For /F %%G In ('Copy /Z "%~f0" NUL') Do @Set "CR=%%G"
@For /F "Tokens=1,2* Delims=:" %%G In ('%__AppDir__%cmd.exe /D/V/C ^
"%__AppDir__%findstr.exe /NR "^[a-Z]*!CR!!LF![0123456789]" "Test1?.txt" 2>NUL"
') Do @(SetLocal EnableDelayedExpansion
(Set /P "=Name: %%I!CR!!LF!Number: " 0<NUL & Set "_="
For /F Delims^=^ EOL^= %%J In ('%__AppDir__%more.com +%%H "%%G"') Do @(
If Not Defined _ Set "_=_" & Echo %%J)) 1>"%%I.txt" & EndLocal)
该文件应与当前工作目录中的Test1.txt
文件一起运行。重要的是,除了Test1.txt
之外,没有其他.txt
文件具有相同的基名称,后跟一个其他字符(例如Test1a.txt
或Test12.txt
(。如果您想更改文件名,请记住,在上面的代码中,您必须用?
字符(例如MyTextFile.log
⇒MyTextFile?.log
(。
我有一个难得的机会来验证这个脚本是否适用于以下示例Test1.txt
文件:
Beth 1234567891 Jay 2134456544 Bob 2137856514 Jimmy 4574459540 Mary 3734756547 Gemma 6938456114 Albert 0134056504