Avatar of occredit
occredit
Flag for United States of America asked on

Scripting- Move files by name and previous date

Experts, I need help creating a script to move a file by name and previous date. The file is created with the same name every time but with a previous date. The script needs to run daily and must grab only the previous date. For example if today’s date is 8/9/2011, it need to move only the file name XBAL268_080811.


Files in the incoming directoy:
XBAL268_080611
XBAL268_080711
XBAL268_080811

Thanks for your support in advance!
Microsoft DOS

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Bill Prew

(1) Does this have to be a DOS BAT solution, or would a VBS vbscript solution be okay (date manipulation much easier there)?

(2) If it must be DOS, would the use of a small freeware utility that does date calculations be acceptable, or no?

~bp
knightEknight

Getting today's date via batch is relatively trivial:

  move  "XBAL268_%date:~4,2%%date:~7,2%%date:~12,2%"  "D:\newHome"

Getting the previous day, via pure batch, is doable, but very complex.

If you receive this file before midnight local time, then perhaps you can use the trivial option above to move the file at 11:59pm ?
occredit

ASKER
Billprew-

I'm willing to try a freeware utility if you have some suggestions.

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
occredit

ASKER
Knight -

I'm going to give your script a try.
SOLUTION
yo_bee

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
occredit

ASKER
Yo_bee -

Thanks! I'll give this a try as well.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
occredit

ASKER
billprew -

That script worked perfectly! Thank you very much! Points will be given shortly.

One more question or assistance if you can.

We also need to send a file with an ouput of PMT0809.txt (PMTDDMM)format. The date is current system date. How can i use this utility to move this file to another directory?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
occredit

ASKER
Sorry, the format of the file is PMTMMDD.txt IE PMT0809.txt
yo_bee

@Occredit

Glad that the DOFF worked, but I would still mess around with Robocopy and the list of options for more robust methods of copying and moving data around with future tasks.

What type of system are you running this from?

Server 2003 R2, Vista, W7 Server 2008 all have this included and if not you can always get this from Microsoft site.

Good luck with this and if you have any questions if you decide to use robocopy I will be more then happy to assist or give insight.

-Mike-
Bill Prew

The DOFF utility basically takes to parameters, the first is the format of the output you want from the command, and the second (which is optional) is any offset you want applied to the date value returned.

So you can get todays date in DDMM format with:

DOFF ddmm

So, updating the script we would be like this:

@echo off

REM Define from and to folder locations
set BaseDir=c:\temp
set DestDir=d:\dest

REM Use DOFF utility to calculate today's date in DDMM format
for /F %%A in ('doff ddmm') do set Today=%%A

REM Move today file to the destination folder
move "%BaseDir%\PMT%Today%.txt" "%DestDir%\"

Open in new window

~bp
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Bill Prew

Oops, just saw your correction on the format of the date, I actually thought that might be the case, but went with what you mentioned first.  You can probably figure out the change, basically the ddmm in the doff line changes to mmdd.

~bp
occredit

ASKER
Billprew - Thank you very much!

Yo_bee - I've used Robocopy before and it works great as well, but the doff ulitily is much easier to script. With that being said, I will be using the robocopy to do archive portion.

Points will be given to both of you.

Bill Prew

Glad DOFF was useful, it's one of those things people either love, or hate.  I find it very useful for date formatting and offsets in batch files, as long as adding the EXE to the system is okay.  

~bp
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.