Solved

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

Posted on 2007-03-22
7
185 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 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