 | Junior Member with 5 posts. | | | | Solved: DOS Rename based on Date? 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? | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | Yes you could easily rename the newest file in a directory. Code: 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. | | Junior Member with 5 posts. | | | | 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.
Last edited by munkdogg : 23-Oct-2009 10:21 AM.
| | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | 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. | | Junior Member with 5 posts. | | | | 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... | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | 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, | | Junior Member with 5 posts. | | | | Sorry mate;
pushd d:\expert
FOR /F "tokens=*" %%A IN ('dir /b /a-d /tc /o-d *.*') DO (
rename %%A somenewfilename.foo
goto :eof
) | | Distinguished Member with 5,485 posts. | | Join Date: Aug 2007 Location: Oregon, USA Experience: Intermediate | | 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.
__________________ Microsoft MVP - Windows Desktop Experience
Of course I know all the answers  ; I just don't always match the answers to the right questions Are you aware of the New Signature Limitations? | | Junior Member with 5 posts. | | | | 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! | |
Smart Search
| Find your solution! | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |  WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
| You Are Using: |
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 01:40 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd. | |
|