Solved

batch command to copy a file with specific date

Posted on 2010-11-30
17
416 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 53

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now