Renaming extensions of files in DOS

Hi

I am using XP professional.

i have a folder, with following files
c:\Source\STMTPRNT.SPL.Extracted900-026-61122007
                STMTPRNT.SPL.Extracted900-027-41122007
                STMTPRNT.SPL.Extracted900-028-21122007
                STMTPRNT.SPL.Extracted900-030-81122007
                STMTPRNT.SPL.Extracted900-031-61122007
i want to move them to another folder
c:\Destination\STMTPRNT.SPL.Extracted900-026-61122007.txt
                STMTPRNT.SPL.Extracted900-027-41122007.txt
                STMTPRNT.SPL.Extracted900-028-21122007.txt
                STMTPRNT.SPL.Extracted900-030-81122007.txt
                STMTPRNT.SPL.Extracted900-031-61122007.txt

i am using the following command

xcopy c:\source\*.extracted*  c:\destination\*.extracted*.txt /s

but in the destination only one file is created as
c:\Destination\STMTPRNT.SPL.txt

Please, help me. I am new to DOS, detailed explanation is highly appriciated.

Thanks
                 
Prashanth_atthaluriAsked:
Who is Participating?
 
Christopher McKayConnect With a Mentor Microsoft Network AdministratorCommented:
Because you have multiple "." in your filenames it may be messing up where the extention is.

Try this :
@echo off
xcopy "C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*" C:\FDIC\VisionBKP\Data\visiontxt\ /s
for /f "delims=" %%i in ('dir /a-d /b /s C:\FDIC\VisionBKP\Data\visiontxt\') do ren "%%i" "%%~nxi.txt"

Open in new window

0
 
Paul MacDonaldDirector, Information SystemsCommented:
Try:

xcopy c:\source\*.extracted*  c:\destination\*.txt /s

0
 
treetop3Commented:
HI,

MSDOS will only read and create files with the 8.3 file format

 xxxxxxxx.xxx (8.3)

For more detail have a look at this article

http://en.wikipedia.org/wiki/8.3_filename

Regards,
TT
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Prashanth_atthaluriAuthor Commented:
Hey paul

i tried the command you mentioned, its creating only one file, which means all the previous files are  replaced.

Thanks
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
I would recommend trying something like this instead:


for /f %i in ('dir /a-d /b c:\source\*extracted*) do ('xcopy c:\source\%i c:\destination\%i.txt /s')


Hope this helps!

:o)

Bartender_1
0
 
rabindrajhaCommented:
xcopy /s/c/h/d "source"\*.* "destination"\*.*
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Sorry, second set of quotes is not necessary, this command from the command line should work:

for /f %i in ('dir /a-d /b c:\source\*extracted*) do (xcopy c:\source\%i c:\destination\%i.txt /s)

If you want to use this command in a batch file it should look like this:


for /f %%i in ('dir /a-d /b c:\source\*extracted*) do (
xcopy c:\source\%i c:\destination\%i.txt /s
)


Hope this helps!

:o)

Bartender_1
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
~sighs~ nothing like using copy/paste to replicate errors....

Sorry, try this command from the command line:

for /f %i in ('dir /a-d /b c:\source\*extracted*') do (xcopy c:\source\%i c:\destination\%i.txt /s)

this for a batch file:

for /f %%i in ('dir /a-d /b c:\source\*extracted*') do (
xcopy c:\source\%i c:\destination\%i.txt /s
)
0
 
Prashanth_atthaluriAuthor Commented:
hey Bartender_1

I have used
for /f %%i in (dir /a-d /b C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*) do (xcopy C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\%i C:\FDIC\VisionBKP\Data\visiontxt\%i.txt /s)

i got the following error:

%%i was unexpected at this time.

Thanks
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
If this is from the command line, try this:

for /f %i in ('dir /a-d /b C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*') do (xcopy C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\%i C:\FDIC\VisionBKP\Data\visiontxt\%i.txt /s)
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
If used from the command line, the variable is denoted with a single %, if used in a batch file, it needs a double %%.
Also, please note, that in either case you need to use a single quote around the command 'dir /a-d /b c:\path\*extracted*'

Hope this helps!

:o)

Bartender_1
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You are better off doing the copy/move part first, and then rename files - that is much easier to accomplish, and faster.

pushd c:\FDIC\VisionBKP\Data
xcopy ARCHIVE_unzipped\*extracted* visiontxt\
ren visiontxt\*extracted*. *extracted*.txt
popd


Bartender,

There is no use of FOR /F in that case (and the parens around the xcopy are superfluous). It works, but it is cumbersome. A simple
for %i in (c:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*) do xcopy "%i" "C:\FDIC\VisionBKP\Data\visiontxt\%~nxi.txt"
is much better. Further, the /s for xcopy is even wrong - we do not want to go into subfolders.
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Qlemo,

~sheepish grin~ You're right of course. I didn't even bother to check the switches for the xcopy command.
0
 
Prashanth_atthaluriAuthor Commented:
Hey Bartender

  Sorry for misleading example, there are many subfolders from which i have to copy the files.

when i run this cmd
for /f %i in ('dir /a-d /b C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*') do (xcopy C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\%i C:\FDIC\VisionBKP\Data\visiontxt\%i.txt /s)

I think there is a problem, i.e when ever there is a space in folder name eg:
C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\vision-20091116140126 [289]_rebuild\
then the variable is just returning C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\vision-20091116140126

so i am getting, the following error
C:\FDIC\VisionBKP\Data\visiontxt\STATEMENTS>ren "c:\FDIC\VisionBKP\Data\visiontxt\STATEMENTS\vision-20101217235906" "i.txt"
The system cannot find the file specified.

Please, let me know the solution.

Thanks



0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Ok,

Just to ensure I have a clear picture here, you have a number of files, in multiple subfolders, some containing spaces in the names, all within the "C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped" folder, and you want to copy all of the files to a backup location and append the ".txt" to the filenames. Am I correct in assuming you wish to retain the folder structure in the backup location?

0
 
Prashanth_atthaluriAuthor Commented:
Yes Sir.
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Ok, Are you attempting to do this in a single command you can enter from the command line, or can this be put into a batch file?

I don't know of any way with simple commands to do this all in one go. Qlemo is correct, at this point it is simpler to copy the files over, then do the renaming.

Strictly speaking, you could enter multiple commands on the command line and accomplish this, but that's what a batch file is supposed to be for, to take multiple lines of commands, and execute them in sequence.

0
 
Prashanth_atthaluriAuthor Commented:
Please, give me the batchfile.

i tried what 'Qlemo' said. Copied first and tried to rename later. But did not kow how to use 'REN' to rename files in subfolders.

Thanks
0
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
If there is more to do than just copy some files and rename them, and in particular when having to process subfolders, the batch approach with processing each file individually is better indeed.
Two approaches:
(a) FOR loop copying one file per step, and using the new name as target
(b) xcopying in one go, and use a FOR loop for processing all folders in the target location.
I prefer (b):
@echo off
pushd C:\FDIC\VisionBKP\Data\visiontxt
xcopy /s/e "C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted" .
for /R %%D in (.) do ren "%%~D\*" *.txt
popd

Open in new window

0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Ok, in your batch file you'll want this:



@echo off
xcopy "C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*" C:\FDIC\VisionBKP\Data\visiontxt\ /s
for /f "delims=" %%i in ('dir /a-d /b /s C:\FDIC\VisionBKP\Data\visiontxt\') do ren "%i" "%~ni.txt"

Open in new window

0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Wups. Guess I'm slow.

:o)

Bartender_1
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Ehem. You forgot to use %% instead of % in your rename command ;-).
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
You're correct of course.

:o)

Bartender_1
@echo off
xcopy "C:\FDIC\VisionBKP\Data\ARCHIVE_unzipped\*extracted*" C:\FDIC\VisionBKP\Data\visiontxt\ /s
for /f "delims=" %%i in ('dir /a-d /b /s C:\FDIC\VisionBKP\Data\visiontxt\') do ren "%%i" "%%~ni.txt"

Open in new window

0
 
Prashanth_atthaluriAuthor Commented:
Hey Bartender

when i ran this cmd

for /f "delims=" %i in ('dir /a-d /b /s C:\FDIC\Westbridge\VisionBKP\Data\visiontxt\Statements') do ren "%i" "%~ni.txt"

i got following error

ren "C:\FDIC\Westbridge\VisionBKP\Data\visiontxt\Statements\vision-20101217235906 [289]\STMTSAVG.spl.Extracted500-100-312172010" "STMTSAVG.spl.txt"
A duplicate file name exists, or the file cannot be found.

I think .extracted* part is getting replaced by .txt. Please, look into it.
I appriciate the pain you are taking to help me.

Thanks a lot.
0
 
Prashanth_atthaluriAuthor Commented:
Hey, Bartender Thanks so much.
Thanks Qlemo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.