-2

I have a windows batch file that reads in various lines of a text file for processing. During the execution I have to open another text file that has only one line in it. The line contains a serial number with about 3 or 4 spaces after the end.

I need to read in this file called (value.txt), grab the data in the first line and assign the value to a variable without any spaces at the end. After that I need to add the .pdf extension to this variable and set to another variable.

I then need to then check if the variable with the pdf extension exists in the current directory as a file name. If it does then can I change the variable value to add -1 before the .pdf. If this file still exists then try -2 etc. Thus File name read in called value.txt

55400TERM1 (variable VAR1 is assigned with no spaces)

55400TERM1.pdf (variable VAR2 is set with the .pdf extension)

55400TERM1-1.pdf (variable VAR2 is updated if 55400TERM.pdf exists)

55400TERM1-2.pdf (variable VAR2 is updated if 55400TERM1.pdf exists)

Etc etc - loops until it cannot file a existing file with the variable value.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138

1 Answers1

0

Here is a comment batch script for this task:

@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%".
    exit /B
)

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%".
exit /B

: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%

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • dir /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143