General discussion

Locked

Using A Batch File To Rename Text Files

By muyideen ·
I have a scenario whereby an Oracle application I developed generates a text file output. This output must reside in a seprate directory on daily basis such that yesterday's output file must have a different name than today's file.
I have another program which generates a unique filename (a text string) and writes it to another text file.
My problem is how to use a redirection symbol in a DOS batch file to pick this unique filename to automatically rename my output file everyday.

This conversation is currently closed to new comments.

11 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Using A Batch File To Rename Text Files

by muyideen In reply to Using A Batch File To Ren ...

Point value changed by question poster.

Collapse -

Using A Batch File To Rename Text Files

by jacq In reply to Using A Batch File To Ren ...

a simple task in unix.. but in dos/windows you can trick it
if you put a for command in a batchfile it can do the trick even if there is only one file to rename

so if your new name is in file names.txt and the dayly file is output.fil the command in the batchfile would be :

for %%n in (names.txt) do ren output.fil %%n

if you type it at the promt use single %

i hope this helps :)

Collapse -

Using A Batch File To Rename Text Files

by jacq In reply to Using A Batch File To Ren ...

oeps.. too fast.. is different in dos.

but why don't you write in the filename file not the complete rename command ?
then you can just "call" it from the batch file

Collapse -

Using A Batch File To Rename Text Files

by muyideen In reply to Using A Batch File To Ren ...

I tried the first option of this answer and the result was that, rather than picking the text string stored in (names.txt)file, it actually picked "names.txt" itself. So the resulting error message was " A Duplicate filename exists or file cannot be found ..."
The second option in the answer needs a further elaboration.
Nevertheless, I am very grateful to jacq.

Collapse -

Using A Batch File To Rename Text Files

by juanja In reply to Using A Batch File To Ren ...

If you are working on windows, why don't you try writing a script, place it on the task scheduler, and schedule it to run every day at a given time, for example at night. If you need help with the script, let me know.

I hope this helps.

Collapse -

Using A Batch File To Rename Text Files

by muyideen In reply to Using A Batch File To Ren ...

Poster rated this answer

Collapse -

Using A Batch File To Rename Text Files

by jacq In reply to Using A Batch File To Ren ...

ok.. this took osme more thinking but i think i have a solution
(i tried this one so for me it works :)

first make a file containing only the rename command and the first (constant filename)
echo ren output > cmdf

then start a debug
debug cmdf

do a d command to check for a space on the place after the filename (in my case on position 10a) if it is not there add is with e 10a (or any other place)
a space is 20 hex.
then count the number of characters including the space
put that number in hex in register cx
command is r cx
gives : prompt
enter b (11 characters)
type command w for write
now we have the first part of the command canned
now we can combine it with the file containing the new name nfnm.txt to a bat file we can "call" with the copy command
copy cmfd+nfnm.txt renfil.bat
this renfil.bat has now our rename command

in the dayly batchfile you should have :

cd (place to go)
copy cmdf+nmnm.txt renfil.bat
call renfil.bat
del renfil.bat

et voila :)
hope this works for you too

Collapse -

Using A Batch File To Rename Text Files

by muyideen In reply to Using A Batch File To Ren ...

Poster rated this answer

Collapse -

Using A Batch File To Rename Text Files

by Shanghai Sam In reply to Using A Batch File To Ren ...

in addition to answer 1...
it is not really dos .. but they say it's a dos box on nt and win2000 there is a flag in the for statement (/f) which does exactly what i expected to ahppen in the first example and has lots of possibilities more with patterns and such..

so just add /f after the for in example 1 and it will work

Collapse -

Using A Batch File To Rename Text Files

by muyideen In reply to Using A Batch File To Ren ...

Sorry for the delay in responding. I was away from office for a week. Thanks, a lot jacq. Adding /f to your answer 1 really did the work.

Back to Web Development Forum
11 total posts (Page 1 of 2)   01 | 02   Next

Related Forums