Solved

Adding condition to existsing DOS Script which adds file extension and replaces characters

Posted on 2007-03-22
7
184 Views
Last Modified: 2010-04-16
Hi,

In a dos batch script I use this command to rename files to use the extension of .xml

c:
cd c:\testing2
for %%a in (*.*) do ren %%a %%a.xml

I also use a second batch file to replace any . character with an _ character...

set source=c:\testing2
for /f "tokens=*" %%a in ('dir %source% /a-d /b') do call :process %%a
goto end


:process
set fname=%*
rename %source%\%fname% %fname:.=_%
:end

Would I need to do is two things...

1) Only add the .xml file extension if the file does not already have a .xml file extension

2) Incorporate both scripts together so its all one process

So the end result is hopefully a file with .xml as the file extension with only the . character before the xml file extension, any other would have been changed to _

Can anybody assist?

0
Comment
Question by:arundelr
  • 5
  • 2
7 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18770340
Try this..

c:
cd c:\testing2
for %%a in (*.*) do if not %%~xa==.xml call :process %%dpnxsa
goto :eof

:process
ren %source%\%1 %fname:.=_%.xml
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18770348
type-o above..


c:
cd c:\testing2
for %%a in (*.*) do if not %%~xa==.xml call :process %%~dpnxsa
goto :eof

:process
ren %source%\%1 %fname:.=_%.xml
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18770369
Arg - yet again...  :|

cd /d c:\testing2
for %%a in (*.*) do if not %%~xa==.xml call :process %%~nxsa
goto :eof

:process
set fname=%1
ren %fname% %fname:.=_%.xml
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:arundelr
ID: 18770880
Hi Sirbounty,

I just tried this with a file caled 12345678.zzz which changed into 12345678_zzz.xml  - PERFECT

But then I tried with a file called 1234567991.zzz which is changed into 123456~1_ZZZ.xml

It appears not to handle the longer file names correctly, is there any work around here?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18770897
Sure - simply strip out the short version (the 's' below) and use your original *..


cd /d c:\testing2
for %%a in (*.*) do if not %%~xa==.xml call :process %%~nxa
goto :eof

:process
set fname=%*
ren "%fname%" "%fname:.=_%.xml"
0
 

Author Comment

by:arundelr
ID: 18770943
Perfecto, thank you once again sirbounty ;o)))
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18770979
Happy to help! :^)
Thanx for the grade!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 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