Solved

batch command to copy a file with specific date

Posted on 2010-11-30
17
430 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 54

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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