Solved

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

Posted on 2007-03-22
7
182 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

744 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

11 Experts available now in Live!

Get 1:1 Help Now