Solved

Renaming extensions of files in DOS

Posted on 2011-02-16
25
866 Views
Last Modified: 2012-05-11
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
                 
0
Comment
Question by:Prashanth_atthaluri
  • 12
  • 7
  • 3
  • +3
25 Comments
 
LVL 33

Expert Comment

by:paulmacd
ID: 34909046
Try:

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

0
 
LVL 2

Expert Comment

by:treetop3
ID: 34909086
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
 

Author Comment

by:Prashanth_atthaluri
ID: 34909186
Hey paul

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

Thanks
0
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34909287
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
 
LVL 3

Expert Comment

by:rabindrajha
ID: 34909456
xcopy /s/c/h/d "source"\*.* "destination"\*.*
0
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34909767
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34909832
~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
 

Author Comment

by:Prashanth_atthaluri
ID: 34909921
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34909949
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34909986
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 34910082
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34910099
Qlemo,

~sheepish grin~ You're right of course. I didn't even bother to check the switches for the xcopy command.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Prashanth_atthaluri
ID: 34916474
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34917708
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
 

Author Comment

by:Prashanth_atthaluri
ID: 34918826
Yes Sir.
0
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34919379
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
 

Author Comment

by:Prashanth_atthaluri
ID: 34919454
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
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 150 total points
ID: 34919593
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34919701
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
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34919713
Wups. Guess I'm slow.

:o)

Bartender_1
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 34919747
Ehem. You forgot to use %% instead of % in your rename command ;-).
0
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34919843
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
 

Author Comment

by:Prashanth_atthaluri
ID: 34929298
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
 
LVL 22

Accepted Solution

by:
Bartender_1 earned 350 total points
ID: 34930075
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
 

Author Closing Comment

by:Prashanth_atthaluri
ID: 34930373
Hey, Bartender Thanks so much.
Thanks Qlemo
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now