1.

Solve : For Dias de verano: Can I extract a portion of a file name??

Answer»

Yesterday you came to my rescue and showed me how to extract the DATE and convert it into a different format.  Today I have a similar file re-naming issue that I'm hoping you can help me with.

Same scenario as yesterday:  a dozen small files from multiple directories get copied into a single directory and then compressed into a zip file.  Each of these files has the creation date in the file name, and I WOULD like to extract that string of characters from the first file and INSERT it in the zipfile name.

I found a batch file in another thread that will sort the files so the same file name will always be on top from one day to the next, and the file name format will always be the same, a four (4) character file name preceding the creation date (which will always be the previous business day) followed by the file extension, with the three parts being separated by two periods, as in name.yyyymmdd.extension.

So my question today is, can that yyyymmdd creation date be extracted and saved as a variable for use in renaming the zip file?

Again, thanks in advance for any assistance you may provide.How flattering to be asked by name! But what a heavy responsibility.

Quote

the file name format will always be the same, a four (4) character file name preceding the creation date (which will always be the previous business day) followed by the file extension, with the three parts being separated by two periods, as in name.yyyymmdd.extension.

Have you worked out how you are going to isolate the filename that is "on top"? Is the extension always the same?

Never mind, the extension doesn't matter. All you need to do is extract the 8 characters starting at the 6th position from the start.

To slice a string variable...

You need a percent sign, the variable name, a colon ( : ) and a tilde ( ~ ). a number, a comma, another number, and a final percent sign.

if %string% is the full string

%string:~a,b% is the substring which is b characters long starting at position a.
(Start from zero)

so if %string% is abcd.yyyymmdd

%string:~0,4% will produce abcd

%string:~5,4% will produce yyyy

%string:~5,8% will produce yyyymmdd


Considering everything I tried yesterday before asking for help, I'm feeling kind of foolish at how I missed the solution entirely. 

Here is the syntax that isolates the filename, which I found on this forum a few weeks ago...

 FOR /F  %%i IN ('dir /b /a-d /o-d') DO (
        set firstfile=%%i
        )

...and ADDING %firstfile:~5,8% after, per your directions, yields the characters I need to properly name my file.

Thanks again!!


Discussion

No Comment Found