Solved

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

Posted on 2007-03-22
7
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 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