我需要在固定位置的文本文件中插入4个字符串:
我总是希望将字符串写在完全相同的位置,无论之前的其他字符串有多长。所以它应该如下所示:
the product ergerzgtrg 65ggrth784rjhnjgbkljn doesn't exist
the product reggbrtbhtrergzthrjhlyoiul rtjntjrez doesn't exist
the product zef rt doesn't exist
这是我的代码:
FOR /F "delims=; tokens=1-7*" %%a in (mytextfile.txt) do (
if "%%e"=="Unkown" (
echo the product %%b %%c doesn't exist>>Unkown_product.txt
)
)
mytextfile.txt如下所示:
K5134908-Blabla_4;K5134908;Blabla_4;01-69423;Unkown;K5134908-Blabla_4-516245;K5134908-Blabla_4-516245;
K2602207-Blabla_2;K2602207;Blabla_2;01-81111;Unkown;K2602207-Blabla_2-516245;K2602207-Blabla_2-516245;
K2602006-Blabla_3;K2602006;Blabla_3;01-82789;Unkown;K2602006-Blabla_3-516245;K2602006-Blabla_3-516245;
K2601507-Blabla_4;K2601507;Blabla_4;01-75135;Unkown;K2601507-Blabla_4-516245;K2601507-Blabla_4-516245;
有没有办法在批处理文件(.bat)中执行此操作?
答案 0 :(得分:1)
为值添加足够的空格(在我的示例中为20),然后剪切第一个[无论你需要的]字符(在我的示例中为15):
@echo off
setlocal
FOR /F "delims=; tokens=1-7*" %%a in (mytextfile.txt) do (
if "%%e"=="Unkown" call :format "%%b" "%%c"
)
goto :eof
:format
set "b=%~1 "
set "c=%~2 "
set "b=%b:~0,15%"
set "c=%c:~0,15%"
echo the product %b% %c% doesn't exist>>Unkown_product.txt
注意:根据您的文件示例,搜索字符串为"Unkown"
(可能是拼写错误,我想它应该是"Unknown"
)