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

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!!
reidtenkleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
reidtenkleyAuthor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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
reidtenkleyAuthor Commented:
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
Lee W, MVPTechnology and Business Process AdvisorCommented:
@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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nevahjCommented:

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
nevahjCommented:

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.