We help IT Professionals succeed at work.

File rename based on date and transfer

341 Views
Last Modified: 2013-11-26
Okay. The situation's basically this: I have a local folder under the name C:\Program Files\Local and a destination folder called C:\Program Files\Destination. Every day, a new .TXT file will be placed inside the local folder.

Basically I need a application/script of sorts to be able to do the following-
1) Rename that .TXT by stamping a date in front of the current file's name and changing the format into a .GDB (example, office001.txt is renamed into 091109office001.gdb (DD/MM/YY))

2) Transfer the renamed file into the destination folder ( C:\Program Files\Destination).

Doesn't matter what order it happens. It can be transferred, THEN renamed, or the other way round.

For a bonus function, if possible, I would like to set a limiter to the number of files the Destination folder can contain. If the total amount of files inside the Destination folder > 2, the 3rd and the oldest file must be deleted.

I'm really desperate for a solution as my forte isn't in this field, and I was assigned this task as I was the most savvy within the department. I have wasted too much time on it already and need to move on fast.

I apologize for getting so straight to the point, and I would heavily appreciate answers that are usable from the copy.

Both batch files and VB, VB.net files are welcome.

Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
First, for many reasons I would refrain from using dd/mm/yy in a filename.
For a pure DOS approach, we need your current date format reported by

echo %date%
 
I assume you get British format,  maybe prepended with a weekday, like
Mon, 09/11/2009

Then it would be (see snippet). The del command is only echoed instead of executed, if you are sure that it is what you want then remove the echo keyword.


@echo off
setlocal EnableDelayedExpansion
REM change this:
set src=:\Program Files\Local
set dst=C:\Program Files\Destination
 
set dt=%date:~-0,4%%date:~-7,2%%date:~10,2%
REM  dd/mm/yyyy => yyyymmdd
 
for %%F in (%src%\*) do move %%F %dest%\%dt%%%~nxF
 
set cnt=0
for /F %%C in ('dir %dest% /b | find /c "\"') do set cnt=%%C
if cnt GTR 2 (
  for /F %%F in ('dir %dest% /b /a:-d /o:-d) do set oldfile=%%F
  echo del !oldfile!
)

Open in new window

Commented:
Limitl3ss

Please try the following batch file.

NOTE 1: Set "maxfiles" to the maximum number of files you wish to keep in your destination folder

NOTE 2: "set datestamp" assumes your system's date format (singapore:english) is DD/MM/YYYY.
                Please amend if necessary.



@echo off
setlocal enabledelayedexpansion

set source=C:\Program Files\Local
set destination=C:\Program Files\Destination

set maxfiles=2
set datestamp=%date:~0,2%%date:~3,2%%date:~-2%

for %%a in ("%source%\*.txt") do move "%%a" "%destination%\%datestamp%%%~na.gdb"

set count=0
for /f "tokens=*" %%a in ('dir /a-d /b /o-d "%destination%\*.gdb"') do (
   set /a count+=1
   if !count! gtr %maxfiles% del /f "%destination%\%%a"
)

Author

Commented:
The first script doesn't work so well for me for some strange reason, tho I can see and understand the logic behind it and it should serve my purpose.

The one by t0t0 works great tho, after replacing the datetime stamp that came out really bad on my system.

Just one thing tho. Can I have the move function replaced by a copy paste? I double checked the specs required, and I actually need a copy-paste of only the MOST RECENTLY MODIFIED file. Meaning to say that the previous files (eg. office001, office002, office003) should be untouched, and only office003.txt should be copied over and renamed, leaving the original untouched. And when office004.txt comes in, if I ran the same batch file, it should only affect 004.

I've been searching high and low for a sample that only affects the most recent file, and all work fine. But I can't seem to apply the same to the batch that you experts provided (both of the above).

Sorry for having to depend on you guys again. Really am hopeless in this matter.

Commented:
>>"The first script doesn't work"

Of course it doesn't work. Basically, in terms of batch file scripting it's a poor effort. Obviously the code was never tested. Lines 4, 10, 13, 15 and 16 contain errors!

Here's the modified code:

NOTE 1
I've added an extra code to prevent from copying the same file more than once should you run the batch repeatedly without adding a new .TXT file to the source folder.

NOTE 2
I've also separated the two functions: copy and delete, as it might be useful to have the second part of the code run independently as a way of adjusting 'maxfiles' and trimming the files in 'destination'.

NOTE 3
Please re-edit the 'set datestamp=' line again



@echo off
setlocal enabledelayedexpansion

set source=C:\Program Files\Local
set destination=C:\Program Files\Destination

set maxfiles=3
set datestamp=%date:~0,2%%date:~3,2%%date:~-2%

if exist "%source%\*.txt" (
   for /f "tokens=*" %%a in ('dir /a-d /b /od "%source%\*.txt"') do set file=%%~na
   if not exist "%destination%\!file!.gdb" (
      copy "%source%\!file!.txt" "%destination%\%datestamp%!file!.gdb" >nul
   )
)

if exist "%destination%\*.gdb" (
   set count=0
   for /f "tokens=*" %%a in ('dir /a-d /b /o-d "%destination%\*.gdb"') do (
      set /a count+=1
      if !count! gtr %maxfiles% del /f "%destination%\%%a"
   )
)
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Oh dear.... My head's swelling.....
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Of course the code has been tested, but I did not paste my test case, hence some typos and slips of the pen.

Now, after all the vaingloriousness, the best of all scripts (again, the datetime parsing needs adjustment for your local format):

@echo off
 
REM change this:
set src=c:\Program Files\Local
set dest=C:\Program Files\Destination
set maxfiles=2
 
set dt=%date:~-0,4%%date:~-7,2%%date:~10,2%
REM  dd/mm/yyyy => yyyymmdd
 
set file=
for /F "tokens=*" %%F in ('dir "%src%\*.txt" /b /a:-d /o:d') do set file=%%~nF
if defined file xcopy "%src%\%file%.txt" "%dest%\%dt%%file%.gdb" /d/y
 
for /F "skip=%maxfiles% tokens=*" %%F in ('dir "%dest%\*.gdb" /b /a:-d /o:-d') do del /f "%%F"

Open in new window

Commented:
Qlemo

Looks like we've got a battle here... the guru has taken on the wizard... Competition is always in the customers' favour as it demands increasingly better results - and this time, you've come back with the "skip=" thing... I like that. I'm in two minds about the "xcopy" thing though as I've had a re-think about repeated copy - does it really matter from a logical viewpoint?


Commented:
Limitl3ss

Hopefully, you're still jumping up and down in excitement from my previous code.

I have made some improvements. See below:



@echo off
setlocal enabledelayedexpansion

set source=C:\Program Files\Local
set destination=C:\Program Files\Destination

set maxfiles=2
set datestamp=%date:~0,2%%date:~3,2%%date:~-2%

for /f "tokens=*" %%a in ('dir /a-d /b /od "%source%\*.txt" 2^>nul') do set file=%%~na

copy /y "%source%\!file!.txt" "%destination%\%datestamp%!file!.gdb" >nul

for /f "skip=%maxfiles% tokens=*" %%a in ('dir /a-d /b /o-d "%destination%\*.gdb" 2^>nul') do (
   del /f "%destination%\%%a"
)
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
xcopy /d will care about the "same file repeated" stuff. If the text file is of same date, no copy. Only if it has changed, it will be transferred.
I don't think it does matter. If you start the batch on the same file, but tomorrow, the same file will be copied, with a new time stamp.
However, the xcopy takes care of the "same day, same file" issue on-the-fly, so why not?

Commented:
Qlemo

I can see your logic VERY clearly but I'm still in two minds whether to submit to it's need.

I've also decided to do away with the "if defined" thing. Good programming but brute force and redirection does it the short-hand way.

Some of these shortcuts can obscure the logic of the code.

BTW, also done away with "set file=" by relying on COPY to trap ".txt". Incidentally, COPY sends it's error message to STDOUT - not STDERR hence no need for "2>&1" - again, obscure logic.

This is going to be a close one....

Commented:
Qlemo

As I said, this is a close one as both codes are improved - mine probably a tad sharper than yours -although I'm sure you'll argue that one until the cows come home. This is interesting....

I await the asker's reply with baited breath....

Author

Commented:
Both solutions were extremely close, really. I would award you both the 250 points, but seeing as t0t0's answer came in first, he wins this time. Apologies to Qlemo, tho. I would still like to use yours as a future reference should the application ever require a different approach.

I'd like to thank you both of you for your attentiveness.

Author

Commented:
FYI, both scripts worked perfectly. I do somewhat prefer Qlemo's version as it does cover up for an aspect that will probably won't ever come up. However, t0t0's is much more staightforward, and I'm currently using the improved script he submitted just about an hour ago.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Then you should have selected that one as a solution, not the first one.

Commented:
Limitl3ss
Thank you. Well chosen.


Qlemo
The guru has marked the net with his code and awaits the wizard's retaliatory challenge.

BTW, elements from both codes are snipped for the scrapbook. (Still can't get over the convenience of the skip thing in this application. Perfect!).

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
I was amazed about that trick, too :-)

Commented:
Amazing....You took the words right out of my mouth.... Amazing!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.