Solved

batch command to copy a file with specific date

Posted on 2010-11-30
17
398 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

15 Experts available now in Live!

Get 1:1 Help Now