Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

batch command to copy a file with specific date

Posted on 2010-11-30
17
Medium Priority
?
445 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 58

Accepted Solution

by:
Bill Prew earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

610 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