# Solved: Extract Date from File Name - Using Windows Batch File



## first_time_user (Jun 24, 2012)

Hi,

I have a file in a folder on a network drive whose path is : *"\\sql\imp\ssis"*

I need a windows batch file that looks into the above folder for a file with a name like : "*abcd_03052012_84373.txt*" .

The above file name has a date embedded in it in "ddmmyyyy" format. I want that to be extracted and put into a variable or a file somewhere so that I can read that later and store into a sql table.

Please help.


----------



## Squashman (Apr 4, 2003)

Is the naming convention of the file always the same?
Four letters _ 8 numbers _ 6 numbers.txt


----------



## first_time_user (Jun 24, 2012)

Yes please.


----------



## Squashman (Apr 4, 2003)

```
FOR /F "TOKENS=2 delims=_" %%G IN ('dir /a-d /b ^|findstr /B /I /R "[a-z][a-z][a-z][a-z]_.*_.*\.txt"^|findstr /E /I /R ".*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9]\.txt"') do set fdate=%%G
```


----------



## first_time_user (Jun 24, 2012)

Good heavens! Thank you very much Sir. I could never have written that myself. Thank you very much.


----------

