Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can I use pattern matching with the copy or move command?

What I wanted to do is copy the file a.txt from D:\1\2\datehere\a.txt to another folder let us say D:\new.

The problem is that the source path folder is a date as a folder name which keeps changing. The files to be copied inside that stay a.txt and some other files. So how to copy a.txt to another folder.

Can I use something like pattern matching or do you have any other idea?

share|improve this question
    
How do you know what anyname is? Suppose there's another subdirectory someothername - how would you know to copy a.txt from anyname rather that 'someothername`? –  Peter Wright Mar 22 '13 at 8:29
    
@PeterWright i have edited my question. The folder will be having name as the date(current date). So it keeps on changing. –  IT researcher Mar 22 '13 at 9:17
    
OK - so next question - in which date format? Personally, I use YYYYMMDD but my display format is dd/mm/yy. When you execute ECHO %date% what do you see - and how would the date portion of your directoryname be structured? Would the directoryname have been created today? Would it simply be the latest directoryname - or the latest directoryname containing the file if it's possible for the directory to be created after your target but not contain the file? –  Peter Wright Mar 22 '13 at 9:24
add comment

migrated from stackoverflow.com Mar 24 '13 at 11:10

This question came from our site for professional and enthusiast programmers.

1 Answer

This searches in %userprofile% for the first folder with today's date as it's name and copies a.txt from there to the %temp% folder. Please assign your paths as needed.

@echo off &setlocal
set "StartFolder=%userprofile%"
set "TargetFolder=%temp%"
set "FileMask=a.txt"

set "SourceFolder="
for /d /r "%StartFolder%" %%i in (*.*) do if not defined SourceFolder if "%%~nxi" equ "%date%" set "SourceFolder=%%~i"
if not defined SourceFolder echo Folder %date% not found&goto:eof
copy "%SourceFolder%\%FileMask%" "%TargetFolder%"
endlocal
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.