如何使用 Windows 批处理文件创建脚本来修改文本文件?

问题描述 投票:0回答:1

我不太懂Windows写代码,希望有好心人帮帮我

我需要一些可以修改文本文件的东西。它需要在 .bat 批处理脚本中运行或从中运行。修改是查找、复制、替换文本。

在txt文件中,会有大约30个以^FOxx,xxx^开头的字符串,其中x为整数。我需要将 162 添加到逗号后的所有 3 位数字。因此,例如,^FO354,231^ 将变为 ^FO354,393^

其中一些字符串在 FO 后只有 2 位数字。例如 ^FO23,756^。 逗号后的一些字符串是 1、2 或 4 位数字。主要是 3 位数字,如我的示例,但可以是 1、2、3 或 4。

总而言之,对于 ^FO 的每个实例,下一个逗号后的数字需要将其值增加 162。

这是模组 1。还需要一个模组。这是模组 2.

上述程序运行后,会出现一行以^FO95, 908^(以前是^FO95,746)和^FS结尾的行。我需要将此行复制并粘贴(复制)为原始行正下方的新行。行可以被识别为开始 ^FO 和结束 ^FS。

最后但同样重要的是,在新的/重复的行中,我们需要将逗号后面的数字更改为 8。我们需要 ^FO95,908^ 变成 ^FO95,8^。

我很乐意雇人来写这篇文章。如果有人可以推荐我,那对我来说很好。或者这可能违反了这个委员会的规则,我在阅读的规则中没有看到任何关于商业主义的内容。

哦顺便说一句,这不是家庭作业。这是 Zebra 热敏打印机的标签。我们新的会计软件不支持我们需要的标签样式。幸运的是,发送到打印机的文件是一个 .zpl 文件,是纯文本,因此我们希望在通过批处理文件发送到打印机时转换每个标签。

谢谢!

windows batch-file text
1个回答
0
投票
@ECHO OFF
SETLOCAL

rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
rem This code assumes that the parameter provided to the batch file is the filename only of a 
rem text file, so for instance "q75755707" to read "q75755707.txt" and produce new file 
rem "q75755707_processed.txt"

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\%1.txt"
SET "outfile=%destdir%\%1_processed.txt"

(

FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
  SET "line=%%e"
  CALL :process
)
)>"%outfile%"

TYPE "%outfile%"

GOTO :EOF


:process
:: does LINE start ^FO?
IF "%line:~0,3%" neq "^FO" ECHO %line:^=^^%&GOTO :eof
FOR /f "tokens=1,2*delims=,^^" %%u IN ("%line:~3%") DO SET "num1=%%u"&SET "num2=%%v"&SET "rest=%%w"
:: are num1 and num2 both numeric? If not,error
SET "notnumber=9%num1%%num2%"
FOR /l %%z IN (0,1,9) DO CALL SET "notnumber=%%notnumber:%%z=%%"
IF DEFINED notnumber ECHO %num1% and %num2% found IN Field Offset&pause&GOTO :eof
:: batch treats numbers starting 0 as OCTAL, so make them decimal
SET /a num1=100000%num1%
SET /a num2=100000%num2%
SET /a num1=num1 %% 10000
SET /a num2=num2 %% 10000

:: bump num2 by 162

SET /a num2+=162

:: Generate replacement line
ECHO ^^FO%num1%,%num2%^^%rest:^=^^%

:: And additional line
IF "%num1% %num2%"=="95 908" ECHO ^^FO%num1%,8^^%rest:^=^^%

GOTO :eof

批处理文本文件,毛毛的

某些字符是特殊的。在这件事上值得关注的是

^
%
。其他符号也有他们在阳光下的时间。

Batch 将以

0
开头的数字字符串视为 OCTAL。上面的代码绕过了这个陷阱。

期望从尝试的解决方案中获得一些代码,以及一些样本源数据是正常的。我不再有任何 ZPL 样本。

我相信货币化 SO 响应是非常不鼓励的,但我建议您检查这个以寻求解决方案


修订:

@ECHO OFF
SETLOCAL

rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "destdir=u:\your results"
SET "sourcedir=u:\your files"

:: Clicking on a file?
SET "filename1=%~1"
IF NOT DEFINED filename1 (
 rem just running batch
 FOR /f "delims=" %%y IN ('dir /b /a-d "%sourcedir%\*.zql"') DO CALL "%~0" "%sourcedir%\%%y"
 GOTO :eof
)

SET "sourcedir=%~dp1"
SET "outfile=%destdir%\%~n1_processed.txt"

(
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
  SET "line=%%e"
  CALL :process
)
)>"%outfile%"

:: This line intended to show the transformed file
TYPE "%outfile%"

:: This line to retain display of transformed file
pause

:: This line to print transformed file
print /d:\\PC-NAME\"Zebra ZP 500 (ZPL)" "%outfile%"

:: This line to delete transformed file
del "%outfile%"

:: This line to delete source file
del "%filename1%"

GOTO :EOF


:process
:: does LINE start ^FO?
IF "%line:~0,3%" neq "^FO" ECHO %line:^=^^%&GOTO :eof
FOR /f "tokens=1,2*delims=,^^" %%u IN ("%line:~3%") DO SET "num1=%%u"&SET "num2=%%v"&SET "rest=%%w"
:: are num1 and num2 both numeric? If not,error
SET "notnumber=9%num1%%num2%"
FOR /l %%z IN (0,1,9) DO CALL SET "notnumber=%%notnumber:%%z=%%"
IF DEFINED notnumber ECHO %num1% and %num2% found IN Field Offset&pause&GOTO :eof
:: batch treats numbers starting 0 as OCTAL, so make them decimal
SET /a num1=100000%num1%
SET /a num2=100000%num2%
SET /a num1=num1 %% 10000
SET /a num2=num2 %% 10000

:: bump num2 by 162

SET /a num2+=162

:: Generate replacement line
ECHO ^^FO%num1%,%num2%^^%rest:^=^^%

:: And additional line
IF "%num1% %num2%"=="95 908" ECHO ^^FO%num1%,8^^%rest:^=^^%

GOTO :eof

在应用于真实数据之前,始终对照测试目录进行验证。

OK - 稍作修改。

如果通过单击文件名或文件组运行,将转换并打印该文件或文件组。

如果通过简单地执行批处理来运行,将扫描

sourcedir
源文件名,转换每个文件并打印每个文件。

注意,我已经将扩展名更改为

.zql
以进行测试。您需要将其更改为
.zpl

生成转换后的文件后,有 5 个可选操作。只需将

REM 
放在动作的开头即可不执行它。

© www.soinside.com 2019 - 2024. All rights reserved.