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 anyone point me in the right direction?

I need to schedule a bat file that copies files from a folder whose name changes daily and I need to copy those from yesterday's folder. I.e. if today is feb 12, 2010 I need to copy files from folder 20100211 and tomorrow I will need to copy from folder 20100212.

Any ideas?

share|improve this question

3 Answers 3

In batch file this is near to impossible since you cannot choose format. However, if you have PowerShell on this machine, you could use Get-Date:

$now = get-date -format yyyyMMddTHHmmss
$fileName = $now + "_something.txt"
Copy-Item $source -Destination $fileName
share|improve this answer
1  
That's not true regarding batch files: robvanderwoude.com/datetimentparse.php –  Joey Feb 14 '10 at 16:25
    
@Johannes. It is not quite as simple. While this functions may parse US date format, they do not work for all formats out there. Example is Croatian date format (d.M.yyyy.). Yes, you could probably compensate for it but this is too much dependency on system and particular locale for me to be satisfied with it. I would use it only as last resort. –  Josip Medved Feb 14 '10 at 18:32
    
As mentioned there, the output of date shows the format as well as the separator so it can parse dates in various formats. –  Joey Feb 14 '10 at 21:12
    
@Johannes. As mentioned there, it does not work in current form with single d/m/y designators. I tried it on my system and it does work when I change it to US format, but it does not work form my default. It can be modified - there is no doubt there - but I see this like last resort only if more advanced shell is not available. For me just having that number of lines in batch is quite scary. –  Josip Medved Feb 14 '10 at 21:50

Would it suffice to keep the parent directory of these YYYYMMDD directories synced to another directory? That way, you wouldn't have to worry about the name changes. Robocopy is a great command-line tool for mirroring one directory to another. It became one of the standard commands as of Vista. If you're still using XP, you can get it as part of the Windows Server 2003 Resource Kit Tools. If you don't want everything in the parent directory copied, you can specify files and/or directories to ignore in a robocopy job file.

share|improve this answer

Here is a batch script that will copy files from C:/FromFolder/yyyymmdd to C:/ToFolder (yyyymmdd is yesterday's date).

# Script CopyYesterday.txt
var str yesterday, list, file
set $yesterday = addtime(diff("-1000000"))   # "-1000000" = subtract 1 day, 00 hrs, 00 mins, 00 secs
chex "8]" $yesterday > $yesterday
# We now want to copy files from C:/FromFolder/$yesterday to C:/ToFolder.
lf -n "*" ("C:/FromFolder/"+$yesterday) ($ftype=="f") > $list
while ($list <> "")
do
    lex "1" $list > $file
    system -s "copy /Y" ("\""+$file+"\"") "C:/ToFolder"
done

Copy and save the script in file C:/Script/CopyYesterday.txt. Then schedule this command to run daily.

"C:/biterScripting/biterScripting.exe" "C:/Script/CopyYesterday.txt"

You will need to install biterscripting ( http://www.biterscripting.com ). It is great for flexible file management. The help pages for addtime, lf command etc, are at their site or you can also do "help addtime", etc. in biterscripting.

share|improve this answer
    
You don't need external tools for that task. –  dolmen Mar 27 '11 at 11:37

Your Answer

 
discard

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