How can I read the last 53 characters from a text file using Windows commands?


I don't have any scripting experience but I've been asked to create a script to automatically download log files from a storage array controller for daily archiving. Due to the limitations of the storage vendor's CLI, it looks like the most straightforward way to do this is to run the vendor's command to list the log files and redirect the output to a text file.

Once the filenames are stored in the text file, I'd like to read the most recent filename from the file and use that to pull the file from the array. The standard filename format for the log files is 53 characters but changes every day because the create date and time are part of the filename. The filenames are somewhat random because the create time isn't consistent every day.

It was pretty easy to generate the list of file names, but I haven't figured out how to read the last 53 characters from the file.

Due to the limitations of the storage vendor's syntax, I have to use the exact filename (which is why I'm trying to use the list approach).

I don't know if it matters, but there's a blank line at the end of my text file with my filenames. I don't know offhand if I'm doing something wrong or if this is a standard result for a list created by a batch file. I don't know if that will affect trying to read the filename or not.

Another potential complication is that I have to use only standard Windows commands. I can't install any third-party utilities on my management server.

Here's a sample from the output file created when I run the command to list the files:

469 1815 06/11/2014 13:30:44 SERIALNUMBER##_SC#_YYYY-MM-DD_HH-MM-SS-GMT_M05-00.log

Any suggestions would be appreciated. I've spent a fair amount of time researching but the examples I've found so far appear to be for Unix or use third party tools I can't use.

Thanks. Bill

The closest I can come is to get you a line out of your file.
This bat file will count the lines in your file, then echo out the indicated line:

@setlocal enabledelayedexpansion
@set /a %count=0
@for /f "usebackq delims==" %%i in (`type %1`) do @(
    @set /a "count += 1"

REM countb = count from end of file back to the line you want
REM 0 == last line, 1 = line before last line, etc

@set /a %countb=1
@for /f "usebackq delims==" %%i in (`type %1`) do @(
    @set /a "countb +=1"
    if !countb! == %count% @echo %%i

I found a similar question that was about counting individual characters in a bat file, so if you really need to get a specific number of characters from the file, maybe take a look here: iterate over a string in a foor loop