View Full Version : Replacing a string in a filename


Andy Yew
08-29-2005, 01:53 AM
I need help in writing something that can take the filename as a string
and being able to rename that file into something else. For example,
"A&B.dgn" to "A and B.dgn"

I've gotten some sample batch scripts that does renaming but I guess I
have a hard time understanding them.

What's also the difference between %%i and %i in the scripts that use
FOR command in them?

Thanks.

Clay Calvert
08-29-2005, 02:21 AM
On Mon, 29 Aug 2005 08:53:39 +0800, Andy Yew <none@anon.com> wrote:

>I've gotten some sample batch scripts that does renaming but I guess I
>have a hard time understanding them.
>
>What's also the difference between %%i and %i in the scripts that use
>FOR command in them?

Use single percents when running directly from the command line. Use
double-percent signs when running in a batch file.
Clay Calvert
CCalvert@Wanguru.com
Replace "W" with "L"

billious
08-29-2005, 03:52 AM
"Andy Yew" <none@anon.com> wrote in message
news:uINToQDrFHA.2072@TK2MSFTNGP14.phx.gbl...
>I need help in writing something that can take the filename as a string
> and being able to rename that file into something else. For example,
> "A&B.dgn" to "A and B.dgn"
>
> I've gotten some sample batch scripts that does renaming but I guess I
> have a hard time understanding them.
>
> What's also the difference between %%i and %i in the scripts that use
> FOR command in them?
>
> Thanks.

Second question's easy. Single % where the FOR is executed directly from the
command-prompt. Double if used WITHIN a batch file.

No doubt the 'replace "&" with " and " within a filename' problem could be
solved using pure-batch, but it's a lot easier using SED (Google is your
friend...)

[01]@echo off
[02]for %%i in ("*&*") do call :renamp "%%i"
[03]goto :eof
[04]
[05]:renamp
[06]echo ren %1 >tmp.txt
[07]echo %1|sed s/\^&/\x20and\x20/>>tmp.txt
[08]sed $!N;s/\n/\x20/ tmp.txt >{t}.bat
[09]call {t}.bat
[10]del {t}.bat
[11]del tmp.txt
[12]goto :eof

Each line begins [number]. Lines will be wrapped in transmission and
need to be rejoined. The [number] at the beginning of each line needs
to be removed.

I'm sure that you can set up a SED script to do the processing more
elegantly - but the above works on XP. Naturally, you'd have to check
whether {t}.bat found a duplicate name (errorlevel would be 1) and no doubt
you'd want the errormessage produced under those circumstances suppressed.

Lots of NT/2K/XP batch solutions in alt.msdos.batch.nt - a long-standing
newsgroup established to tackle batch applications.

HTH

....Bill

Andy Yew
08-29-2005, 04:59 AM
Andy Yew wrote:
> I need help in writing something that can take the filename as a string
> and being able to rename that file into something else. For example,
> "A&B.dgn" to "A and B.dgn"
>
> I've gotten some sample batch scripts that does renaming but I guess I
> have a hard time understanding them.
>
> What's also the difference between %%i and %i in the scripts that use
> FOR command in them?
>
> Thanks.


Thanks for the info all. I'll be checking it out..

Andy

Andy Yew
08-29-2005, 09:44 AM
billious wrote:
> "Andy Yew" <none@anon.com> wrote in message
> news:uINToQDrFHA.2072@TK2MSFTNGP14.phx.gbl...
>
>>I need help in writing something that can take the filename as a string
>>and being able to rename that file into something else. For example,
>>"A&B.dgn" to "A and B.dgn"
>>
>>I've gotten some sample batch scripts that does renaming but I guess I
>>have a hard time understanding them.
>>
>>What's also the difference between %%i and %i in the scripts that use
>>FOR command in them?
>>
>>Thanks.
>
>
> Second question's easy. Single % where the FOR is executed directly from the
> command-prompt. Double if used WITHIN a batch file.
>
> No doubt the 'replace "&" with " and " within a filename' problem could be
> solved using pure-batch, but it's a lot easier using SED (Google is your
> friend...)
>
> [01]@echo off
> [02]for %%i in ("*&*") do call :renamp "%%i"
> [03]goto :eof
> [04]
> [05]:renamp
> [06]echo ren %1 >tmp.txt
> [07]echo %1|sed s/\^&/\x20and\x20/>>tmp.txt
> [08]sed $!N;s/\n/\x20/ tmp.txt >{t}.bat
> [09]call {t}.bat
> [10]del {t}.bat
> [11]del tmp.txt
> [12]goto :eof
>
> Each line begins [number]. Lines will be wrapped in transmission and
> need to be rejoined. The [number] at the beginning of each line needs
> to be removed.
>
> I'm sure that you can set up a SED script to do the processing more
> elegantly - but the above works on XP. Naturally, you'd have to check
> whether {t}.bat found a duplicate name (errorlevel would be 1) and no doubt
> you'd want the errormessage produced under those circumstances suppressed.
>
> Lots of NT/2K/XP batch solutions in alt.msdos.batch.nt - a long-standing
> newsgroup established to tackle batch applications.
>
> HTH
>
> ...Bill
>
>
>
I'm going through some tutorials to find further use for this, but I
have a quick question, I having a hard time finding the command to be
able to use '&'

For eg. I want to do this.. TEST_ to be prefixed to the file, and I
thought the '&' could be of use here, what's the sed regex that I can
use to do it?

sed s/(.*)/TEST_&/ will work? I'm getting syntax errors for that.

Thanks again.

David Candy
08-29-2005, 10:00 AM
Ampersand seperates commands on one line. Therefore you will need to escape it with a ^ (Shift + 6)

So assuming the rest of your line is right

sed s/(.*)/TEST_^&/
--
--------------------------------------------------------------------------------------------------
http://webdiary.smh.com.au/archives/_comment/001075.html
=================================================
"Andy Yew" <none@anon.com> wrote in message news:OyKv6XHrFHA.2624@TK2MSFTNGP15.phx.gbl...
> billious wrote:
>> "Andy Yew" <none@anon.com> wrote in message
>> news:uINToQDrFHA.2072@TK2MSFTNGP14.phx.gbl...
>>
>>>I need help in writing something that can take the filename as a string
>>>and being able to rename that file into something else. For example,
>>>"A&B.dgn" to "A and B.dgn"
>>>
>>>I've gotten some sample batch scripts that does renaming but I guess I
>>>have a hard time understanding them.
>>>
>>>What's also the difference between %%i and %i in the scripts that use
>>>FOR command in them?
>>>
>>>Thanks.
>>
>>
>> Second question's easy. Single % where the FOR is executed directly from the
>> command-prompt. Double if used WITHIN a batch file.
>>
>> No doubt the 'replace "&" with " and " within a filename' problem could be
>> solved using pure-batch, but it's a lot easier using SED (Google is your
>> friend...)
>>
>> [01]@echo off
>> [02]for %%i in ("*&*") do call :renamp "%%i"
>> [03]goto :eof
>> [04]
>> [05]:renamp
>> [06]echo ren %1 >tmp.txt
>> [07]echo %1|sed s/\^&/\x20and\x20/>>tmp.txt
>> [08]sed $!N;s/\n/\x20/ tmp.txt >{t}.bat
>> [09]call {t}.bat
>> [10]del {t}.bat
>> [11]del tmp.txt
>> [12]goto :eof
>>
>> Each line begins [number]. Lines will be wrapped in transmission and
>> need to be rejoined. The [number] at the beginning of each line needs
>> to be removed.
>>
>> I'm sure that you can set up a SED script to do the processing more
>> elegantly - but the above works on XP. Naturally, you'd have to check
>> whether {t}.bat found a duplicate name (errorlevel would be 1) and no doubt
>> you'd want the errormessage produced under those circumstances suppressed.
>>
>> Lots of NT/2K/XP batch solutions in alt.msdos.batch.nt - a long-standing
>> newsgroup established to tackle batch applications.
>>
>> HTH
>>
>> ...Bill
>>
>>
>>
> I'm going through some tutorials to find further use for this, but I
> have a quick question, I having a hard time finding the command to be
> able to use '&'
>
> For eg. I want to do this.. TEST_ to be prefixed to the file, and I
> thought the '&' could be of use here, what's the sed regex that I can
> use to do it?
>
> sed s/(.*)/TEST_&/ will work? I'm getting syntax errors for that.
>
> Thanks again.

Andy Yew
08-29-2005, 10:26 AM
David Candy wrote:
> Ampersand seperates commands on one line. Therefore you will need to escape it with a ^ (Shift + 6)
>
> So assuming the rest of your line is right
>
> sed s/(.*)/TEST_^&/

Thanks for the info, I've been trying to find a definite resource for
Windows version of Sed and it's been slow going.

This is what I want to achieve, as part of the original script, to
append something infront of the line..


sed -e s/\^&/\x20and\x20/ -e s/^./Test_/>>tmp.txt

sorta got what I wanted, however, I realised I need to deal with the
quote marks that you would get if the file has spaces in it. Thus,
"A&B.dwg" is to be conversted to "Test_A and B.dwg"

Right now I have "A&B.dwg" changing to Test_A and B.dwg"

If you can point me to a good resource/man page just for windows would
be great as well. Thanks. Putting quote marks in gives me syntax errors.
Escaping it will not work since the quote marks don't go in. \x20
means spaces right?

Andy Yew
08-29-2005, 10:31 AM
Andy Yew wrote:
> David Candy wrote:
>
>> Ampersand seperates commands on one line. Therefore you will need to
>> escape it with a ^ (Shift + 6)
>>
>> So assuming the rest of your line is right
>>
>> sed s/(.*)/TEST_^&/
>
>
> Thanks for the info, I've been trying to find a definite resource for
> Windows version of Sed and it's been slow going.
>
> This is what I want to achieve, as part of the original script, to
> append something infront of the line..
>
>
> sed -e s/\^&/\x20and\x20/ -e s/^./Test_/>>tmp.txt
>
> sorta got what I wanted, however, I realised I need to deal with the
> quote marks that you would get if the file has spaces in it. Thus,
> "A&B.dwg" is to be conversted to "Test_A and B.dwg"
>
> Right now I have "A&B.dwg" changing to Test_A and B.dwg"
>
> If you can point me to a good resource/man page just for windows would
> be great as well. Thanks. Putting quote marks in gives me syntax errors.
> Escaping it will not work since the quote marks don't go in. \x20 means
> spaces right?
>
>

Ok, I got this...

sed -e s/\^&/\x20and\x20/ -e s/[a-z,A-Z,0-9]/AP3D_^&/

That seems to work, but seemed like a rather in-elegant way of doing it.
Just curious as to how to deal with quote marks.

Thanks again for all your help.

Andy Yew
08-29-2005, 10:33 AM
Andy Yew wrote:
> David Candy wrote:
>
>> Ampersand seperates commands on one line. Therefore you will need to
>> escape it with a ^ (Shift + 6)
>>
>> So assuming the rest of your line is right
>>
>> sed s/(.*)/TEST_^&/
>
>
> Thanks for the info, I've been trying to find a definite resource for
> Windows version of Sed and it's been slow going.
>
> This is what I want to achieve, as part of the original script, to
> append something infront of the line..
>
>
> sed -e s/\^&/\x20and\x20/ -e s/^./Test_/>>tmp.txt
>
> sorta got what I wanted, however, I realised I need to deal with the
> quote marks that you would get if the file has spaces in it. Thus,
> "A&B.dwg" is to be conversted to "Test_A and B.dwg"
>
> Right now I have "A&B.dwg" changing to Test_A and B.dwg"
>
> If you can point me to a good resource/man page just for windows would
> be great as well. Thanks. Putting quote marks in gives me syntax errors.
> Escaping it will not work since the quote marks don't go in. \x20 means
> spaces right?
>
>

The worry I have is in the event I do have a file that's "(A&B).dwg"
coming up in the list and that would not be picked up properly...
Matching against the quote and being able to replace it again with a
double quote would probably be my best bet. Will search further.

David Candy
08-29-2005, 10:55 AM
Type

FOR %A IN ("c:\Documents And Settings\David Candy\My Documents\My Pictures\*.jpg") DO ren "%A" "p%~nA%~xA"

Will change all jpg files in a folder to p<original name>.jpg

The case of the A (as in %A or %~<letter>A) is important. FOR/IN/DO is uppercase to highlight the actual command syntax.

Replace path with your particular one. If you drag the titlebar icon of an open folder into the cmd window it will type it for you.

I would, if doing this for me, export file names and use Word's Search & Replace (it's similar to regexp in wildcard mode) to make a batch file. I've done this for at least 20 years (although with XEdit rather than Word).

He is a script that does Regexp on the file contents. You should be able to modify it to names.

ReplaceRegExp Filename SearchString ReplaceString
Quotes are required for spaces. Use 0xnn for wierd characters. Wildcard list is below..

On Error Resume Next
Set ShellApp = CreateObject("Shell.Application")
ReportErrors "Creating Shell.App"
set WshShell = WScript.CreateObject("WScript.Shell")
ReportErrors "Creating Wscript.Shell"
Set objArgs = WScript.Arguments
ReportErrors "Creating Wscript.Arg"
Set regEx = New RegExp
ReportErrors "Creating RegEx"
Set fso = CreateObject("Scripting.FileSystemObject")
ReportErrors "Creating FSO"

WshShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\" & Wscript.ScriptName & "\", Chr(34) & Wscript.ScriptFullName & Chr(34)
WshShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\" & Left(Wscript.ScriptName, Len(Wscript.ScriptName)-3) & "exe" & "\", Chr(34) & Wscript.ScriptFullName & Chr(34)
ReportErrors "Updating App Paths"
If objArgs.Count = 0 then
MsgBox "No parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "Only one parameter", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 2 then
Set srcfile = fso.GetFile(objArgs(0))
ReportErrors "srcFile"
If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
If err.number <> 0 then
Msgbox err.description & " " & srcFile.path, 48, "Serenity's Search"
err.clear
else
ReportErrors "TS" & " " & srcFile.path
Src=ts.readall
If err.number = 62 then
err.clear
else
ReportErrors "ReadTS" & " " & srcFile.path
regEx.Pattern = objArgs(1)
regEx.IgnoreCase = True
regEx.Global = True
If regEx.Test(Src) = True then
Msgbox "Found in " & srcfile.path, 64, "Serenity's Search"
End If
End If
End If
ReportErrors "Check OK" & " " & srcFile.path

Elseif objArgs.count = 3 then
Set srcfile = fso.GetFile(objArgs(0))
ReportErrors "srcFile"
If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
If err.number <> 0 then
Msgbox err.description & " " & srcFile.path, 48, "Serenity's Search"
err.clear
else
ReportErrors "TS" & " " & srcFile.path
Src=ts.readall
If err.number = 62 then
err.clear
else
ReportErrors "ReadTS" & " " & srcFile.path
regEx.Pattern = objArgs(1)
regEx.IgnoreCase = True
regEx.Global = True
NewSrc= regEx.Replace(Src, objArgs(2))
If NewSrc<>Src then
Msgbox "Replacement made in " & srcfile.path, 64, "Serenity's Search"
TS.close
Set TS = srcFile.OpenAsTextStream(2, 0)
ts.write newsrc
ReportErrors "Writing file"
End If
End If
End If
ReportErrors "Check OK" & " " & srcFile.path


Else
MsgBox "Too many parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"

ReportErrors "All Others"
End If

Sub ReportErrors(strModuleName)
If err.number<>0 then Msgbox "An unexpected error occurred. This dialog provides details on the error." & vbCRLF & vbCRLF & "Error Details " & vbCRLF & vbCRLF & "Script Name" & vbTab & Wscript.ScriptFullName & vbCRLF & "Module" & vbtab & vbTab & strModuleName & vbCRLF & "Error Number" & vbTab & err.number & vbCRLF & "Description" & vbTab & err.description, vbCritical + vbOKOnly, "Something unexpected"
Err.clear
End Sub

Pattern Property
See Also
Global Property | IgnoreCase Property

Applies To: RegExp Object

Requirements
Version 2

Sets or returns the regular expression pattern being searched for.

object.Pattern [= "searchstring"]Arguments
object
Required. Always a RegExp object variable.
searchstring
Optional. Regular string expression being searched for. May include any of the regular expression characters defined in the table in the Settings section.
Settings
Special characters and sequences are used in writing patterns for regular expressions. The following table describes and gives an example of the characters and sequences that can be used.

Character Description
\ Marks the next character as either a special character or a literal. For example, "n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".
^ Matches the beginning of input.
$ Matches the end of input.
* Matches the preceding character zero or more times. For example, "zo*" matches either "z" or "zoo".
+ Matches the preceding character one or more times. For example, "zo+" matches "zoo" but not "z".
? Matches the preceding character zero or one time. For example, "a?ve?" matches the "ve" in "never".
. Matches any single character except a newline character.
(pattern) Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]...[n]. To match parentheses characters ( ), use "\(" or "\)".
x|y Matches either x or y. For example, "z|wood" matches "z" or "wood". "(z|w)oo" matches "zoo" or "wood".
{n} n is a nonnegative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood".
{n,} n is a nonnegative integer. Matches at least n times. For example, "o{2,}" does not match the "o" in "Bob" and matches all the o's in "foooood." "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m} m and n are nonnegative integers. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?".
[xyz] A character set. Matches any one of the enclosed characters. For example, "[abc]" matches the "a" in "plain".
[^xyz] A negative character set. Matches any character not enclosed. For example, "[^abc]" matches the "p" in "plain".
[a-z] A range of characters. Matches any character in the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z".
[^m-z] A negative range characters. Matches any character not in the specified range. For example, "[m-z]" matches any character not in the range "m" through "z".
\b Matches a word boundary, that is, the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb".
\B Matches a non-word boundary. "ea*r\B" matches the "ear" in "never early".
\d Matches a digit character. Equivalent to [0-9].
\D Matches a non-digit character. Equivalent to [^0-9].
\f Matches a form-feed character.
\n Matches a newline character.
\r Matches a carriage return character.
\s Matches any white space including space, tab, form-feed, etc. Equivalent to "[ \f\n\r\t\v]".
\S Matches any nonwhite space character. Equivalent to "[^ \f\n\r\t\v]".
\t Matches a tab character.
\v Matches a vertical tab character.
\w Matches any word character including underscore. Equivalent to "[A-Za-z0-9_]".
\W Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".
\num Matches num, where num is a positive integer. A reference back to remembered matches. For example, "(.)\1" matches two consecutive identical characters.
\n Matches n, where n is an octal escape value. Octal escape values must be 1, 2, or 3 digits long. For example, "\11" and "\011" both match a tab character. "\0011" is the equivalent of "\001" & "1". Octal escape values must not exceed 256. If they do, only the first two digits comprise the expression. Allows ASCII codes to be used in regular expressions.
\xn Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" & "1". Allows ASCII codes to be used in regular expressions.


--
--
--------------------------------------------------------------------------------------------------
http://webdiary.smh.com.au/archives/_comment/001075.html
=================================================
"Andy Yew" <none@anon.com> wrote in message news:%23pRv8xHrFHA.248@TK2MSFTNGP14.phx.gbl...
> Andy Yew wrote:
>> David Candy wrote:
>>
>>> Ampersand seperates commands on one line. Therefore you will need to
>>> escape it with a ^ (Shift + 6)
>>>
>>> So assuming the rest of your line is right
>>>
>>> sed s/(.*)/TEST_^&/
>>
>>
>> Thanks for the info, I've been trying to find a definite resource for
>> Windows version of Sed and it's been slow going.
>>
>> This is what I want to achieve, as part of the original script, to
>> append something infront of the line..
>>
>>
>> sed -e s/\^&/\x20and\x20/ -e s/^./Test_/>>tmp.txt
>>
>> sorta got what I wanted, however, I realised I need to deal with the
>> quote marks that you would get if the file has spaces in it. Thus,
>> "A&B.dwg" is to be conversted to "Test_A and B.dwg"
>>
>> Right now I have "A&B.dwg" changing to Test_A and B.dwg"
>>
>> If you can point me to a good resource/man page just for windows would
>> be great as well. Thanks. Putting quote marks in gives me syntax errors.
>> Escaping it will not work since the quote marks don't go in. \x20 means
>> spaces right?
>>
>>
>
> Ok, I got this...
>
> sed -e s/\^&/\x20and\x20/ -e s/[a-z,A-Z,0-9]/AP3D_^&/
>
> That seems to work, but seemed like a rather in-elegant way of doing it.
> Just curious as to how to deal with quote marks.
>
> Thanks again for all your help.

billious
08-29-2005, 01:18 PM
"Andy Yew" <none@anon.com> wrote in message
news:e4gjRzHrFHA.248@TK2MSFTNGP14.phx.gbl...
> Andy Yew wrote:
>> David Candy wrote:
>>
>>> Ampersand seperates commands on one line. Therefore you will need to
>>> escape it with a ^ (Shift + 6)
>>>
>>> So assuming the rest of your line is right
>>>
>>> sed s/(.*)/TEST_^&/
>>
>>
>> Thanks for the info, I've been trying to find a definite resource for
>> Windows version of Sed and it's been slow going.
>>
>> This is what I want to achieve, as part of the original script, to append
>> something infront of the line..
>>
>>
>> sed -e s/\^&/\x20and\x20/ -e s/^./Test_/>>tmp.txt
>>
>> sorta got what I wanted, however, I realised I need to deal with the
>> quote marks that you would get if the file has spaces in it. Thus,
>> "A&B.dwg" is to be conversted to "Test_A and B.dwg"
>>
>> Right now I have "A&B.dwg" changing to Test_A and B.dwg"
>>
>> If you can point me to a good resource/man page just for windows would be
>> great as well. Thanks. Putting quote marks in gives me syntax errors.
>> Escaping it will not work since the quote marks don't go in. \x20 means
>> spaces right?
>>
>>
>
> The worry I have is in the event I do have a file that's "(A&B).dwg"
> coming up in the list and that would not be picked up properly... Matching
> against the quote and being able to replace it again with a double quote
> would probably be my best bet. Will search further.
>

I'm having a little bit of difficulty figuring out quite what you're doing
and what's not working for you.

You say --- quote-begin-------

"A&B.dwg" is to be conversted to "Test_A and B.dwg"

Right now I have "A&B.dwg" changing to Test_A and B.dwg"

---------quote-end ---

Isn't this what you want to do?

Then you say sed -e s/\^&/\x20and\x20/ -e s/[a-z,A-Z,0-9]/AP3D_^&/ "seems to
work"

What's all the problems with quotes? Quotes can't form part of a filename,
and the routine I gave you originally should generate {t}.bat formatted as

REN "original&name.ext" "original and name.ext"

so there appears to be no requirement to play with quotes at all.

If you want to add a prefix, try

echo %1|sed -e s/^&/\x20and\x20/ -e s/^^/prefix/ >>tmp.txt

(note that the "\" following the first "s/" I used in my original post is
redundant)

The point being that BATCH sees "&" and "^" as special characters so they
have to be escaped by "^"

SED interprets \xnn as a specification of a HEX character - \x20 is a space.
Using \x20 thus distinguishes the string after the -e flag, and SED is aware
that it hasn't received a garbled command. "s/&/" is not a valid SED
command, nor is "and" nor is "/".

You could also possibly use the -f command for SED, as in

echo %1|sed -f PFXNOAMP.SED >>tmp.txt

where PFXNOAMP.SED might contain

s/&/\x20and\x20/
s/^/prefix/

which would allow you to easily build
replace-my-ampersands-and-prefix-the-name filters of many varieties.

Using

echo %1|sed -e s/^&/\x20and\x20/ -e s/^^/prefix/ >>tmp.txt

and also using

echo %1|sed -f pfxnoamp.sed >>tmp.txt

I changed 'A&B.dwg' to 'prefixA and B.dwg'
and '(A&B).dwg' to 'prefix(A and B).dwg'

using the original script I suggested (with the mentioned changes on line
07, naturally)


Suggestion : try Googling for HHSED (Howard Helman's 1991 SED version - very
well-known)

To save you time, try this page: http://www.cornerstonemag.com/sed/

which will give you a heap of information on various SED versions. Eric
Pement's SED One-liners is a mine of useful information.

HTH

....Bill

Stefan Kanthak
08-30-2005, 12:21 AM
"Andy Yew" <none@anon.com> wrote:

> billious wrote:
> > "Andy Yew" <none@anon.com> wrote in message
> > news:uINToQDrFHA.2072@TK2MSFTNGP14.phx.gbl...
> >
> >>I need help in writing something that can take the filename as a string
> >>and being able to rename that file into something else. For example,
> >>"A&B.dgn" to "A and B.dgn"
> >>
> >>I've gotten some sample batch scripts that does renaming but I guess I
> >>have a hard time understanding them.
> >>
> >>What's also the difference between %%i and %i in the scripts that use
> >>FOR command in them?
> >>
> >>Thanks.
> >
> >
> > Second question's easy. Single % where the FOR is executed directly from the
> > command-prompt. Double if used WITHIN a batch file.
> >
> > No doubt the 'replace "&" with " and " within a filename' problem could be
> > solved using pure-batch, but it's a lot easier using SED (Google is your
> > friend...)

There's no need to bother with SED and ECHO and temporary batch files:

--- .CMD ---
FOR %%! IN (*^&*.dwg) DO CALL :RENAME "%%!"
GOTO :EOF

:RENAME
SET FROM=%1
SET TO=%FROM:&= and %
IF EXIST %TO% GOTO :EOF
RENAME %FROM% %TO%
GOTO :EOF
--- EOF ---

Stefan

Andy Yew
08-30-2005, 01:49 AM
billious wrote:
> "Andy Yew" <none@anon.com> wrote in message
> news:e4gjRzHrFHA.248@TK2MSFTNGP14.phx.gbl...
>
>>Andy Yew wrote:
>>
>>>David Candy wrote:
>>>
>>>
>>>>Ampersand seperates commands on one line. Therefore you will need to
>>>>escape it with a ^ (Shift + 6)
>>>>
>>>>So assuming the rest of your line is right
>>>>
>>>>sed s/(.*)/TEST_^&/
>>>
>>>
>>>Thanks for the info, I've been trying to find a definite resource for
>>>Windows version of Sed and it's been slow going.
>>>
>>>This is what I want to achieve, as part of the original script, to append
>>>something infront of the line..
>>>
>>>
>>>sed -e s/\^&/\x20and\x20/ -e s/^./Test_/>>tmp.txt
>>>
>>>sorta got what I wanted, however, I realised I need to deal with the
>>>quote marks that you would get if the file has spaces in it. Thus,
>>>"A&B.dwg" is to be conversted to "Test_A and B.dwg"
>>>
>>>Right now I have "A&B.dwg" changing to Test_A and B.dwg"
>>>
>>>If you can point me to a good resource/man page just for windows would be
>>>great as well. Thanks. Putting quote marks in gives me syntax errors.
>>>Escaping it will not work since the quote marks don't go in. \x20 means
>>>spaces right?
>>>
>>>
>>
>>The worry I have is in the event I do have a file that's "(A&B).dwg"
>>coming up in the list and that would not be picked up properly... Matching
>>against the quote and being able to replace it again with a double quote
>>would probably be my best bet. Will search further.
>>
>
>
> I'm having a little bit of difficulty figuring out quite what you're doing
> and what's not working for you.
>
> You say --- quote-begin-------
>
> "A&B.dwg" is to be conversted to "Test_A and B.dwg"
>
> Right now I have "A&B.dwg" changing to Test_A and B.dwg"
>
> ---------quote-end ---
>
> Isn't this what you want to do?
>
> Then you say sed -e s/\^&/\x20and\x20/ -e s/[a-z,A-Z,0-9]/AP3D_^&/ "seems to
> work"
>
> What's all the problems with quotes? Quotes can't form part of a filename,
> and the routine I gave you originally should generate {t}.bat formatted as
>
> REN "original&name.ext" "original and name.ext"
>
> so there appears to be no requirement to play with quotes at all.
>
> If you want to add a prefix, try
>
> echo %1|sed -e s/^&/\x20and\x20/ -e s/^^/prefix/ >>tmp.txt
>
> (note that the "\" following the first "s/" I used in my original post is
> redundant)
>
> The point being that BATCH sees "&" and "^" as special characters so they
> have to be escaped by "^"
>
> SED interprets \xnn as a specification of a HEX character - \x20 is a space.
> Using \x20 thus distinguishes the string after the -e flag, and SED is aware
> that it hasn't received a garbled command. "s/&/" is not a valid SED
> command, nor is "and" nor is "/".
>
> You could also possibly use the -f command for SED, as in
>
> echo %1|sed -f PFXNOAMP.SED >>tmp.txt
>
> where PFXNOAMP.SED might contain
>
> s/&/\x20and\x20/
> s/^/prefix/
>
> which would allow you to easily build
> replace-my-ampersands-and-prefix-the-name filters of many varieties.
>
> Using
>
> echo %1|sed -e s/^&/\x20and\x20/ -e s/^^/prefix/ >>tmp.txt
>
> and also using
>
> echo %1|sed -f pfxnoamp.sed >>tmp.txt
>
> I changed 'A&B.dwg' to 'prefixA and B.dwg'
> and '(A&B).dwg' to 'prefix(A and B).dwg'
>
> using the original script I suggested (with the mentioned changes on line
> 07, naturally)
>
>
> Suggestion : try Googling for HHSED (Howard Helman's 1991 SED version - very
> well-known)
>
> To save you time, try this page: http://www.cornerstonemag.com/sed/
>
> which will give you a heap of information on various SED versions. Eric
> Pement's SED One-liners is a mine of useful information.
>
> HTH
>
> ...Bill
>
>
Thanks guys, I've got enough info to do what I need, just to clarify
though, the script that you provided Bill, puts in "A and B.dwg" into
the tmp.txt file. Adding a prefix just directly will result in TEST_"A
and B.dwg" into the resultant batch file.

I searched up some hex values and that sorted it out. So that using \x22
will give me the " marks at the right place, so that

sed s/^./\x22Test_/ will convert "A and B.dwg" to "Test_A and B.dwg"

Andy

Andy Yew
08-30-2005, 02:09 AM
Stefan Kanthak wrote:
> "Andy Yew" <none@anon.com> wrote:
>
>
>>billious wrote:
>>
>>>"Andy Yew" <none@anon.com> wrote in message
>>>news:uINToQDrFHA.2072@TK2MSFTNGP14.phx.gbl...
>>>
>>>
>>>>I need help in writing something that can take the filename as a string
>>>>and being able to rename that file into something else. For example,
>>>>"A&B.dgn" to "A and B.dgn"
>>>>
>>>>I've gotten some sample batch scripts that does renaming but I guess I
>>>>have a hard time understanding them.
>>>>
>>>>What's also the difference between %%i and %i in the scripts that use
>>>>FOR command in them?
>>>>
>>>>Thanks.
>>>
>>>
>>>Second question's easy. Single % where the FOR is executed directly from the
>>>command-prompt. Double if used WITHIN a batch file.
>>>
>>>No doubt the 'replace "&" with " and " within a filename' problem could be
>>>solved using pure-batch, but it's a lot easier using SED (Google is your
>>>friend...)
>
>
> There's no need to bother with SED and ECHO and temporary batch files:
>
> --- .CMD ---
> FOR %%! IN (*^&*.dwg) DO CALL :RENAME "%%!"
> GOTO :EOF
>
> :RENAME
> SET FROM=%1
> SET TO=%FROM:&= and %
> IF EXIST %TO% GOTO :EOF
> RENAME %FROM% %TO%
> GOTO :EOF
> --- EOF ---
>
> Stefan
>
>
Whoa. That's great! Didn't know you could specify patterns to change in
the Set command.

Thanks!

billious
08-30-2005, 02:29 PM
"Andy Yew" <none@anon.com> wrote in message
news:%23EL0DzPrFHA.2624@TK2MSFTNGP15.phx.gbl...
[snipalot]

> Thanks guys, I've got enough info to do what I need, just to clarify
> though, the script that you provided Bill, puts in "A and B.dwg" into the
> tmp.txt file. Adding a prefix just directly will result in TEST_"A and
> B.dwg" into the resultant batch file.
>

Very true.

However, provided the prefix doesn't contain spaces or other weird
characters, it works happily - ie.

ren "A&B.dwg" TEST_"A and B.dwg"

will happily change the name to 'TEST_A and B.dwg'

Not how I'd planned it, but another little wonder in the strange world of
batch.....

....Bill

billious
08-30-2005, 02:32 PM
"Stefan Kanthak" <postmaster@1.0.0.127.in-addr.arpa> wrote in message
news:uUOCKJPrFHA.304@TK2MSFTNGP11.phx.gbl...
> "Andy Yew" <none@anon.com> wrote:
>
>> billious wrote:
>> > "Andy Yew" <none@anon.com> wrote in message
>> > news:uINToQDrFHA.2072@TK2MSFTNGP14.phx.gbl...
>> >
>> >>I need help in writing something that can take the filename as a string
>> >>and being able to rename that file into something else. For example,
>> >>"A&B.dgn" to "A and B.dgn"
>> >>
>> >>I've gotten some sample batch scripts that does renaming but I guess I
>> >>have a hard time understanding them.
>> >>
>> >>What's also the difference between %%i and %i in the scripts that use
>> >>FOR command in them?
>> >>
>> >>Thanks.
>> >
>> >
>> > Second question's easy. Single % where the FOR is executed directly
>> > from the
>> > command-prompt. Double if used WITHIN a batch file.
>> >
>> > No doubt the 'replace "&" with " and " within a filename' problem could
>> > be
>> > solved using pure-batch, but it's a lot easier using SED (Google is
>> > your
>> > friend...)
>
> There's no need to bother with SED and ECHO and temporary batch files:
>
> --- .CMD ---
> FOR %%! IN (*^&*.dwg) DO CALL :RENAME "%%!"
> GOTO :EOF
>
> :RENAME
> SET FROM=%1
> SET TO=%FROM:&= and %
> IF EXIST %TO% GOTO :EOF
> RENAME %FROM% %TO%
> GOTO :EOF
> --- EOF ---
>
> Stefan
>
>

OOh nice!

I like that!

....Bill