Solved

batch command to copy a file with specific date

Posted on 2010-11-30
17
437 Views
Last Modified: 2012-05-10
Hi,

i need a dos command to schedule a copy of the file who change the name every day, for exemple:

File1_28-11-10

File1_29-11-10

File1_30-11-10

I need a good variable to use a copy command.

Thanks
0
Comment
Question by:cawasaki
[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
  • 5
  • 3
  • +2
17 Comments
 
LVL 10

Expert Comment

by:moon_blue69
ID: 34239729
HI

You can use the forfiles command to achieve this task where you can search for a file how old it is and delete it. You might need to download the command and paste it in system32

ftp://ftp.microsoft.com/ResKit/y2kfix/x86/

0
 

Author Comment

by:cawasaki
ID: 34239780
how can i use this to cpoy a file with specific date in the file name?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 34239798
Check your system date format with DATE-command.

Assuming your system date format is dd-mm-yyyy...
Set File_Prefix=File1_

Set mm=%DATE:~3,2%  
Set dd=%DATE:~0,2%
Set yy=%DATE:~8,2%

REM Variable to adress file
set File_name=%File_Prefix%%dd%-%mm%-%yy%

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 19

Expert Comment

by:Thommy
ID: 34239817
With COPY-command...

Schedule this batch script with standard Windows Task Scheduler...
Set File_Prefix=File1_

Set mm=%DATE:~3,2%  
Set dd=%DATE:~0,2%
Set yy=%DATE:~8,2%

REM Variable to adress file
set File_name=%File_Prefix%%dd%-%mm%-%yy%
 
Copy C:\DIR1\%File_name% C:\DIR2

Open in new window

0
 

Author Comment

by:cawasaki
ID: 34240080
Thommy

not work

0
 
LVL 19

Expert Comment

by:Thommy
ID: 34240134
What's your system date format???

The paramters for the following commands must be adapted to your special system date format.

Open a DOS box and Type in "DATE" to check for your date format.
date.bmp
0
 
LVL 19

Expert Comment

by:Thommy
ID: 34240143
Sorry, have forgotten the commands which must be adapted to your date format:

Set mm=%DATE:~3,2%  
Set dd=%DATE:~0,2%
Set yy=%DATE:~8,2%

0
 
LVL 19

Expert Comment

by:Thommy
ID: 34240160
If this is not your problem, please tell me more detailled what goes wrong!!!
0
 

Author Comment

by:cawasaki
ID: 34240172
hi,

La date du jour est : 30/11/2010
Entrez la nouvelle date : (jj-mm-aa)

Your script map the current date to the file to copy, but if i need to copy a file with old day, its not work...

for exemple if i need to copy to day a file named "File1_28-11-10", its not work because your script use a variable for today and try to copy this file: "File1_30-11-10"
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 34240240
replace the fileName and destinationFolder and execute the script

this is vbscript

save the file as .vbs and execute it as

cscript filename.vbs
fileName = "test_"
destinationFolder = "c:\test"
datePattern = Year(Date) &  "-" & Month(Date) & "-" & Day(Date)
datePattern = Right("0" & Day(date),2) & "-" & Right("0" & Month(date),2) & "-" & Right(Year(date),2)
fileName = fileName & datePattern
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile fileName, destinationFolder

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 34240283
What do you want to do in detail???

Do you want to copy files with current date or with old date???
0
 

Author Comment

by:cawasaki
ID: 34240317
itkamaraj

Not work
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 34240350
what error you are getting ?

please give the correct file name and where you want to move the file ?
0
 

Author Comment

by:cawasaki
ID: 34240422
i want to copy the file, not move him.

the file name is test_29-11-10.txt. i need to copy it to c:\test.

the file name can change, for exemple, to day the file name is test_30-11-10.txt

Tomorow, the file name must be test_01-12-10.txt.

Thanks
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 34240996
try this
fileName = "test_"
destinationFolder = "c:\test"
datePattern = Year(Date) &  "-" & Month(Date) & "-" & Day(Date)
datePattern = Right("0" & Day(date),2) & "-" & Right("0" & Month(date),2) & "-" & Right(Year(date),2)
fileName = fileName & datePattern
fileName = fileName & ".txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.copyFile fileName, destinationFolder

Open in new window

0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 34243454
For a BAT solution, give this a try, saving as a BAT file, and adjust the first 3 SET's to meet your needs.

~bp

@echo off
set FromDir=c:\temp
set ToDir=d:\temp
set BaseName=test_
set CopyName=%BaseName%%DATE:~-10,2%-%DATE:~-7,2%-%DATE:~-2,2%.txt
copy "%FromDir%\%CopyName%" "%ToDir%"

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 34243804
Schedule this batch with Windows Task Scheduler as a daily task, so that the file is copied to destination directory daily...
Set File_Prefix=test_
Set File_Ext=.txt
Set Dest_Dir=c:\test

Set mm=%DATE:~3,2%  
Set dd=%DATE:~0,2%
Set yy=%DATE:~8,2%

REM Variable to adress file
set File_name=%File_Prefix%%dd%-%mm%-%yy%%File_Ext%
 
Copy %File_name% %Dest_Dir%

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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