Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DOS batch to  add prior date to filename on copy

Posted on 2010-11-17
7
Medium Priority
?
906 Views
Last Modified: 2012-06-27
I need to create a dos batch file that will add the PRIOR date  to the filename.   I have automatic file loaded into a network folder.   The file overwrites itself each night.  I need to be able to copy the file to another network folder with the PRIOR date added to the files name.  

I can seem to get it right.  THANKS
0
Comment
Question by:eyes59
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34160139
My article below gives some examples of how to get yesterdays date:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

e.g.
@echo off
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday in YYYYMMDD was %yesterday%
copy c:\sourcedir\yourfile.txt D:\destdir\somefile-%yesterday%.txt
0
 

Author Comment

by:eyes59
ID: 34160158
To be consistent with the naming convention of the other files that have already been save need the date to be appended as  dd.mm.yyyy    or the file would become    somefile_dd.mm.yyyy.txt
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 664 total points
ID: 34160340
You can either format the string as needed in the VBS part (wscript.echo), or process the result. I'll show the latter:
@echo off
> "%temp%\dateparts.vbs" echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2) 
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
REM below transforms YYYYMMDD into dd.mm.yyyy
copy c:\sourcedir\yourfile.txt D:\destdir\somefile-%yesterday:~-2%.%yesterday:~-4,2%.%yesterday:~0,4%.txt

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 672 total points
ID: 34160384
If you want to amend the original file you can just re-order the dd mm and yyyy bits and add some dots in the wcript.echo or do as Qlemo has

Steve

@echo off
echo wscript.echo right(100+day(date-1 ),2) ^& "." ^& right(100 + month(date-1),2) ^& "." ^& year(date - 1) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday in DD.MM.YYYY was %yesterday%
copy c:\sourcedir\yourfile.txt D:\destdir\somefile-%yesterday%.txt
0
 
LVL 57

Assisted Solution

by:Bill Prew
Bill Prew earned 664 total points
ID: 34161219
If you don't mind using a neat little free utility called DOFF, then the code can be pretty simple as chown.

http://www.jfitz.com/dos/index.html#DOFF

~bp

@echo off
for /F "tokens=*" %%A in ('doff dd.mm.yyyy -1') do (
  copy "oldname.ext" "oldname_%%A.ext"
)

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34375990
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question