?
Solved

bat file to add time/date to file name and copy (follow-up Q)

Posted on 2004-11-11
11
Medium Priority
?
9,259 Views
Last Modified: 2012-06-27
Hello Experts!

This is a follow up Q to http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20955634.html? and is allong the same lines. Some one in the Visual Basic TA suggested this post. I have to admit that my coding and knowledge of MSDOS is negligable so please go easy on me ;) I would like to name the file with the following convention: MMDDYYYY.csv (where MMDDYYY is yesterday's date i.e. Date - 1) I don't really care what .csv file it finds in the folder, I just want to grab whatever file is in there, rename it MMDDYYYY.csv and put it in a new folder called "Dropfolder."

I saw the code in the above example, but how do I use that to make the code executible?
How do I set what time it will run at?
What happens if the function runs one day and it can not find a .csv file to rename and move? can it just log the error and get ready for the next day?
To insure that this runs every day can it run on the mainframe?

thank you so much!!
0
Comment
Question by:reidtenkley
  • 4
  • 2
  • 2
8 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 12561370
Hi reidtenkley,

I'll have a script for you shortly, in the mean time:

> How do I set what time it will run at?
Use "Scheduled Tasks" in Windows 2000 or XP (or 2003 Server).  If NT4, then use the AT service.

> What happens if the function runs one day and it can not find a .csv
> file to rename and move? can it just log the error and get ready for
> the next day?

No problem

> To insure that this runs every day can it run on the mainframe?
No.  Not unless the main frame runs DOS or Windows.  Or can connect remotely to Windows drives.



Cheers!
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 12561396
Ok - important question - would there ever be more than one CSV file?

Assuming the answer is no, then this should do the trick:


cd folder
ren *.csv %date:~4,2%%date:~7,2%%date:~-4%.csv
If not exist .\dropfolder md dropfolder
move *.csv dropfolder
0
 

Author Comment

by:reidtenkley
ID: 12797628
I got busy with our officer move the past couple of weeks. I liked leew's answer, but not having that much experience writing non-VB code I wasn't sure how to apply the answer that he gave in practical terms so I was waiting until I had a couple hours to educate myself before responding. Any help would be appreciated.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 12797670
Take the code between the ---- lines below and put it in a file - any name you want with a .cmd or .bat extension (doesn't really matter).

-------------------------------
@echo off
cd folder
ren *.csv %date:~4,2%%date:~7,2%%date:~-4%.csv
If not exist .\dropfolder md dropfolder
move *.csv dropfolder
-------------------------------

Then use the Scheduled Tasks icon in Control Panel to setup the script to run when you want it to.

If you have any more questions about, please ask and provide as much detail as possible.
0
 

Author Comment

by:reidtenkley
ID: 12797699
sorry for being so daft at this topic, but can you replace the default file and folder names that exist in the above according to the following scenario?

Move any file with a .csv extension from folder "DATAFOLDER" to a folder called "REFORMATDATA" and replace the file name with the previous days date.csv- so in this case it would be "12092004.csv"

thank you so much for walking me through this, I swear I'm not a total incompetent. =)


0
 
LVL 97

Accepted Solution

by:
Lee W, MVP earned 1000 total points
ID: 12797907
@echo off
cd\datafolder
If not exist .\reformatdata md reformatdata
move *.csv reformatdata
ren *.csv %date:~4,2%%date:~7,2%%date:~-4%.csv



I know you CAN do it to provide yesterday's date, but doing so requires significantly more coding and I'm not sure in the frame of mind to attempt it.  I would suggest, if you can, deal with today's date, or set the script to run at 11:59pm.

(it's considerably more difficult because of month and year changes - you can subtract 1 from a day, but what happens on Dec 1?  Or worse, Jan 1?  Code would have to be created to backup the month value and the year values when necessary.
0
 
LVL 2

Expert Comment

by:nevahj
ID: 12816136

Try VBScript - the following is a rough example of the MoveFile and method in WSH Script.

FRFolder = "C:\Test\DataFolder\"
TOFolder = "C:\Test\ReformatData\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set fromfolder = fso.GetFolder(FRFolder)
Set filestomv = fromfolder.Files
   For Each fil in filestomv
      strFile = FRFolder & fil.Name
      if right(strFile,4) = ".csv" then
        msgbox strFile
      fso.MoveFile strFile, TOFolder
      end if
   Next


Note: if you are trying to keep a backup of the file then consider keeping the original file name and change or append to the extension. For example, I have several files that each night are exported as "orders.csv", "sales.csv", "refunds.csv", etc. Rather than lose the eaning of the file I rename mine to use a three digit number as the extension, i.e., "orders.001", sales.001, refunds.001, orders.002, orders.003, etc.

This is a snippet of code that I use to create a backup filename based on 1 year:
   strBak  = left(strFile, len(strFile)-3) & right(1000 + DatePart("y", now()),3)    'BACKUP -- 365 DAYS

HTH,
Charles
0
 
LVL 2

Expert Comment

by:nevahj
ID: 12816170

oops - I forgot you wanted to rename the file. In VBScript there is no rename method -- you have to "move" the file (just like Unix):

FRFolder = "C:\Test\DataFolder\"
TOFolder = "C:\Test\ReformatData\"
TodayDate = right(100 + month(date()),2) & right(100 + day(date()),2) & year(date())

Set fso = CreateObject("Scripting.FileSystemObject")
Set fromfolder = fso.GetFolder(FRFolder)
Set filestomv = fromfolder.Files
   For Each fil in filestomv
      strFile = FRFolder & fil.Name
      if right(strFile,4) = ".csv" then
        msgbox strFile
     fso.MoveFile strFile, TOFolder & TodayDate & ".cvs"
      end if
   Next

- Cheers,
Charles
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

840 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