Computing.Net > Forums > Programming > Batch - search filename to copy

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch - search filename to copy

Reply to Message Icon

Name: un.known
Date: February 8, 2007 at 07:28:10 Pacific
OS: WinXP
CPU/Ram: dual 3.2/2gb
Product: HP
Comment:

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 EnableDelayedExpansion

cd 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%
)



Sponsored Link
Ads by Google

Response Number 1
Name: un.known
Date: February 8, 2007 at 09:07:23 Pacific
Reply:

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


0

Response Number 2
Name: IVO
Date: February 8, 2007 at 09:17:23 Pacific
Reply:

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 EnableDelayedExpansion

cd 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!
)



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch - search filename to copy

Batch file to copy from network dri www.computing.net/answers/programming/batch-file-to-copy-from-network-dri/14488.html

BATCH to copy and rename files www.computing.net/answers/programming/batch-to-copy-and-rename-files/15221.html

batch to copy files www.computing.net/answers/programming/batch-to-copy-files/14316.html