Variables 文本文件中的复杂变量设置

Variables 文本文件中的复杂变量设置,variables,batch-file,set,trim,spaces,Variables,Batch File,Set,Trim,Spaces,我有一个windows批处理文件,可以读取文本文件的不同行进行处理。在执行过程中,我必须打开另一个只有一行的文本文件。该行包含一个序列号,在结束后大约有3或4个空格 我需要读入这个名为(value.txt)的文件,获取第一行中的数据,并将该值赋给一个变量,该变量末尾没有空格。 之后,我需要将.pdf扩展名添加到此变量并设置为另一个变量 然后我需要检查带有pdf扩展名的变量是否作为文件名存在于当前目录中。如果是,那么我可以将变量值更改为在.pdf之前添加-1。如果此文件仍然存在,请尝试-2等。 因

我有一个windows批处理文件,可以读取文本文件的不同行进行处理。在执行过程中,我必须打开另一个只有一行的文本文件。该行包含一个序列号,在结束后大约有3或4个空格

我需要读入这个名为(value.txt)的文件,获取第一行中的数据,并将该值赋给一个变量,该变量末尾没有空格。 之后,我需要将.pdf扩展名添加到此变量并设置为另一个变量

然后我需要检查带有pdf扩展名的变量是否作为文件名存在于当前目录中。如果是,那么我可以将变量值更改为在.pdf之前添加-1。如果此文件仍然存在,请尝试-2等。 因此 读取名为value.txt的文件名

55400TERM1(变量VAR1不带空格)

55400TERM1.pdf(变量VAR2设置为.pdf扩展名)

55400TERM1-1.pdf(如果存在55400TERM1.pdf,则更新变量VAR2)

55400TERM1-2.pdf(如果存在55400TERM1.pdf,则更新变量VAR2)


Etc Etc-循环,直到无法使用变量值归档现有文件。

以下是此任务的注释批处理脚本:

@echo off
rem If text file with file name does not exist in current
rem directory, exit this batch file as nothing can be done.
if not exist value.txt (
    echo %~f0: Missing file "value.txt" in "%CD%".
    goto :EOF
)

rem Read first line from text file. Command FOR automatically splits up the
rem line into strings using space and horizontal tab character as string
rem delimiter. As the line contains just one string with trailing spaces
rem most likely caused by wrong redirecting an output into the text file
rem the single string is assigned to the variable with no trailing spaces.
for /F %%I in (value.txt) do (
    set "FileName=%%I"
    goto CheckFile
)

echo %~f0: No string in file "value.txt" in "%CD%".
goto :EOF

:CheckFile
if exist "%FileName%.pdf" goto FindLatestFile
echo No file "%FileName%.pdf" found in "%CD%".
set "FileName=%FileName%.pdf"
goto ProcessFile

rem The command DIR as used here lists all files matching the wildcard
rem specification in reverse order according to last modification date.
rem This batch file assumes that the file with highest number is the
rem newest modified file. This speeds up determining the file with the
rem highest number in file name. For an alphabetical reverse order it
rem would be necessary that all files have the same number of digits,
rem i.e. the files are *-01.pdf, *-02.pdf, ..., *-10.pdf, *-11.pdf as
rem otherwise the alphabetical order would be *-1.pdf, *-10.pdf, *-11.pdf,
rem *-2.pdf, ..., *-9.pdf and then last file would be determined wrong.

:FindLatestFile
for /F %%I in ('dir /O-D /TW /B "%FileName%-*.pdf" 2^>nul') do (
    set "LastFileName=%%~nI"
    goto FoundLatestFile
)

echo No file "%FileName%-*.pdf" found in "%CD%".
set "FileName=%FileName%-1.pdf"
goto ProcessFile

:FoundLatestFile
for /F "tokens=2 delims=-" %%I in ("%LastFileName%") do set "LastFileNumber=%%I"
set /A LastFileNumber+=1
set "FileName=%FileName%-%LastFileNumber%.pdf"

:ProcessFile
echo Next file is: %FileName%
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • dir/?
  • 获取/?
  • goto/?
  • 如果/?
  • rem/?
  • 设置/?

您自己试过吗?为什么你希望我们为你做所有的事情?我没有写很多代码,所以我在看谁能写最精简的代码来完成这项工作。这里的一些人似乎写了50行代码,而其他人写了10行代码来做同样的事情。如果你能接受挑战,那就试试吧。