There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus batch bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop lcd malware memory modem monitor motherboard network printer problem ram registry router slow software sound toshiba trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless xbox
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: DOS Rename based on Date?

Reply  
Thread Tools
munkdogg's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2009
23-Oct-2009, 10:37 AM #1
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?
Squashman's Avatar
Trusted Advisor with 18,705 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
23-Oct-2009, 10:59 AM #2
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.
munkdogg's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2009
23-Oct-2009, 11:15 AM #3
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 at 11:21 AM..
Squashman's Avatar
Trusted Advisor with 18,705 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
23-Oct-2009, 02:12 PM #4
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's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2009
23-Oct-2009, 03:08 PM #5
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's Avatar
Trusted Advisor with 18,705 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
23-Oct-2009, 11:55 PM #6
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's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2009
24-Oct-2009, 12:27 AM #7
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's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
24-Oct-2009, 04:48 PM #8
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 Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

munkdogg's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2009
26-Oct-2009, 11:04 AM #9
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!
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




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.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:58 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.