# Solved: DOS Rename based on Date?



## munkdogg (Oct 23, 2009)

Good morning,

As part of a larger batch process, I have a step which removes the date (11232009) from the end of a file. Normally this works great, except when there are multiple files in the working directory. Since the result would be multiple files of the same name, this step fails.

As a workaround, I am attempting to rename the _latest _files in the directory. Thus far my attempts have been unsuccessful.

Is it even possible to identify & rename a particular file based on the date/time properties of that file? Alternatively, is it possible to rename the latest/newest file in a given directory?


----------



## Squashman (Apr 4, 2003)

Yes you could easily rename the newest file in a directory.


```
FOR /F "tokens=*" %%A IN ('dir /b /a-d /tc /o-d *.*') DO (
 rename %%A somenewfilename.foo
 goto :eof
 )
```
This will take the newest file in the directory and rename it and then I force an exit in the For Loop to not parse any more files in the directory.

I wouldn't run the batch file from within the directory you are parsing. So do a change direcotry or pushd into the directory that you need this batch file to work on.

would be nice to see your current batch file.


----------



## munkdogg (Oct 23, 2009)

May thanks for the quick reply!

How exactly might I "push" this into the working data directory?

Current batch file is pretty simple;

ECHO Rename Personnel file(s)
ren Personnel* Personnel
cd D:\Expert\Data
copy dw* D:\Expert\Data\Archive
ECHO Rename Data Wharehouse files
ren dw_staff* DW_Staff
ren dw_relation* DW_Relationships
ren dw_depart* DW_Departments

The original files appear as Personnel10232009, DW_Staff10232009, etc, etc.

A separate db bulk insert references a generic filename instead, like DW_Staff, Personnel, etc, etc.


----------



## Squashman (Apr 4, 2003)

You do a CD just like you are doing now or you use the pushd cmd to change the working directory.

CD D:\Expert\Data

or

pushd D:\Expert\Data

The difference is that if you use PUSHD, you can then use POPD to return to the previous directory you were in.


----------



## munkdogg (Oct 23, 2009)

Thank you again....

When I tested this solution, I received the following statement;

"%%A was unexpected at this time".

I fear that I'm overlooking a condition within this script....is there something I've missed?

Sorry for my rather NooB understanding of this, batch scripting is not my forte...


----------



## Squashman (Apr 4, 2003)

Hey munkdogg, 
Whenever you are having issues with code, it would help if you would continue to post your full code back to the forums so we know what you have changed.

Thanks,


----------



## munkdogg (Oct 23, 2009)

Sorry mate;

pushd d:\expert
FOR /F "tokens=*" %%A IN ('dir /b /a-d /tc /o-d *.*') DO (
rename %%A somenewfilename.foo
goto :eof
)


----------



## TheOutcaste (Aug 8, 2007)

Also helps to be specific about exactly when the error occurs. That error will occur right after typing the For statement, and will only happen if you are typing the lines at the prompt instead of putting them into a batch file and running the file.
If you are typing that at the command prompt you have to use *%A* instead of *%%A*

Also, when you type the FOR statement, you will be prompted *More?* because of the parenthesis (. This is a Line Continuation character in this case. Just type the rest of the lines. The For loop will execute when you type the closing parenthesis.


----------



## munkdogg (Oct 23, 2009)

Thanks all. After running this solution the very first time, I did realize it was designed to be run as a batch file. After making the changes detailed by TheOutcaste, this appears to be working as expected.

Many thanks to each of you for your continued assistance and patience!


----------

