Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I have a directory that contains a list of files all with the date they were created. for example: data^070208.dat. I would like to write a batch file that goes through this directory, finds todays files, and then copies them locally to my machine. I am having problems storing the filename substring in a variable to compare it with the date substring variable. Any help would be appreciated! thanks.
@echo off
setLocal EnableDelayedExpansioncd c:\temp
::*******Create String of date***********
set year=%date:~12,2%
set day=%date:~7,2%
set month=%date:~4,2%
set dateString=%year%%month%%day%
set y=FOR %%f IN (*.dat) DO (
set y=%%f
set fileDate=%y:~10,6%
IF fileDate==dateString xcopy c:\temp\y c:\tData
echo file=%%f
echo dateString=!dateString!
echo fileDate=!fileDate!
echo error-%errorlevel%
)

Nevermind. Problem solved. I looked through some of the other questions and found this one:
Subject: extract filename in batch + more
Instead of comparing the two string variables I did the following:
::*******Create String of date***********
set year=%date:~12,2%
set day=%date:~7,2%
set month=%date:~4,2%
set dateString=%year%%month%%day%dir /b c:\temp\*.dat | find "%dateString%" > Found.txt
for /f %%a in (Found.txt) do xcopy %%a c:\temp\TestData

If you want to get the benefits of delayed expansion you have to mark the affected variables by ! and more there are misinterpreted substrings.
@echo off
SetLocal EnableDelayedExpansioncd c:\temp
::*******Create String of date***********
set year=%date:~12,2%
set day=%date:~7,2%
set month=%date:~4,2%
set dateString=%year%%month%%day%
set y=FOR %%f IN (*.dat) DO (
set y=%%f
set fileDate=!y:~5,6!
IF fileDate==dateString xcopy C:\temp\!y! C:\tData
echo file=!y!
echo dateString=!dateString!
echo ErrorLevel=!errorlevel!
)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |