Home / Microsoft / Microsoft DOS / Rename/replace
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3 ... 7  All - (Bottom) Print
Author Topic: Rename/replace  (Read 6021 times)
one stupid guy
Topic Starter
Greenhorn



Posts: 6


« on: December 30, 2009, 12:41:46 PM »

I can't figure it out...

I'd like to rename a number of files, replacing underscores with a space.
For example, I'd like to rename "the_first_file.txt"  to "the first file.txt"
 
I thought the following would work, but it didn't:

RENAME *_*>* *" "*.*


Can anyone help a brother out?

IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #1 on: December 30, 2009, 01:25:18 PM »

This might work...

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /b') do (
set fileorig=%%a
set filenew=!fileorig:_= !
ren !fileorig! "!filenew!"
)
Save it in the folder which contains the files you want to change.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
Salmon Trout
Sage



Thanked: 546
Posts: 7,954

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #2 on: December 30, 2009, 01:27:20 PM »

Code: [Select]
setlocal enabledelayedexpansion
for /f "delims=" %%A in (*.txt) do (
    set oldname=%%A
    set newname=!oldname:_= !
    ren "!oldname!" "!newname!"
    )
IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,954

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #3 on: December 30, 2009, 01:27:50 PM »

Great minds think alike
IP logged


Proud to be European
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #4 on: December 30, 2009, 01:28:46 PM »

Why thank you ST, strangest thing, I was just about to say that too.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
BillRichardson
Intermediate



Thanked: 15
Posts: 194


« Reply #5 on: December 30, 2009, 01:34:21 PM »


C:\>type  under2.bat

Code: [Select]
@echo off
setlocal enabledelayedexpansion
dir /b the*.txt  >  _txtfiles.txt


for /f  "delims=" %%i in (_txtfiles.txt)  do (
echo  %%i

set var=%%i

For /F "delims=" %%a in ('echo %%i ^| sed 's/_/ /g') do set var3=%%a

echo var =!var!

echo var3 =!var3!



ren  "!var!" "!var3!"

rem ren   "!var3!" "!var!"

)


Output:

C:\> under2.bat
the_first_file.txt
the_fourth_file.txt
the_second_file.txt
the_third_file.txt
 the_first_file.txt
var =the_first_file.txt
var3 =the first file.txt
 the_second_file.txt
var =the_second_file.txt
var3 =the second file.txt
 the_third_file.txt
var =the_third_file.txt
var3 =the third file.txt
 the_fourth_file.txt
var =the_fourth_file.txt
var3 =the fourth file.txt
C:\>
« Last Edit: December 31, 2009, 01:35:03 PM by BillRichardson » IP logged

Bill Richardson
Salmon Trout
Sage



Thanked: 546
Posts: 7,954

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #6 on: December 30, 2009, 01:55:09 PM »

Oh God! he's found about sed now! I shudder to think what's next...

OP: Dear Mr bill... I tried running your script and it said "'sed' is not recognized as an internal or external command,
operable program or batch file."
IP logged


Proud to be European
BillRichardson
Intermediate



Thanked: 15
Posts: 194


« Reply #7 on: December 30, 2009, 02:06:39 PM »

"'sed' is not recognized as an internal or external command,
operable program or batch file."


http://www.computerhope.com/unix/used.htm

ghostdog74 is the expert with sed and awk (gawk)

I'm sure ghostdog74  will help you.
IP logged

Bill Richardson
Salmon Trout
Sage



Thanked: 546
Posts: 7,954

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #8 on: December 30, 2009, 02:15:56 PM »

Suppose they don't want to use your weird sounding program?
IP logged


Proud to be European
BillRichardson
Intermediate



Thanked: 15
Posts: 194


« Reply #9 on: December 30, 2009, 03:53:52 PM »

Suppose they don't want to use your weird sounding program?

http://www.computerhope.com/unix/used.htm

Sed has been used many times on the Hope Board.

Sed is a quick and easy stream editor

Speak with Ghostdog74.
IP logged

Bill Richardson
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #10 on: December 30, 2009, 05:30:23 PM »

I can't figure it out...

I'd like to rename a number of files, replacing underscores with a space.
For example, I'd like to rename "the_first_file.txt"  to "the first file.txt"
 
I thought the following would work, but it didn't:

RENAME *_*>* *" "*.*


Can anyone help a brother out?


if you have many underscores, eg file_____test.txt, do you want to replace them with just 1 space as well?
IP logged

one stupid guy
Topic Starter
Greenhorn



Posts: 6


« Reply #11 on: December 31, 2009, 08:40:48 AM »

I better fess-up on my newbieness; I know less than you think.
For the first proposed solution above (thanks to Helpmeh and Salmon Trout), do I just type it into a DOS emulator, or is it more involved?
Keep in mind that I want this to go through a few hundred files and make this change to them all (swap an underscore for a space).

Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.

I tried to identify the elements of the above solution so as to educate myself.
Below is a line-by-line explanation. Is my understanding correct?


setlocal enabledelayedexpansion - the activity of the command will be kept to a specified folder

for /f "delims=" %%A in (*.txt) do - 'for' is like an "IF" command, but I don't know what /f does.
                                   - I don't get the "delims=" either.
                                   - Are the percentage signs are wildcards?
                                   - I know that *.txt means 'all text files'

    set oldname=%%A         - does this specify the format of the old name?

   set newname=!oldname:_= !  - this makes sense (I think), it changes the underscore to a space

    ren "!oldname!" "!newname!"  - this renames the file, with the new (underscoreless) name


Thank you for your insight and patience,
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #12 on: December 31, 2009, 09:15:21 AM »

I better fess-up on my newbieness; I know less than you think.
For the first proposed solution above (thanks to Helpmeh and Salmon Trout), do I just type it into a DOS emulator, or is it more involved?
Keep in mind that I want this to go through a few hundred files and make this change to them all (swap an underscore for a space).

Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.

I tried to identify the elements of the above solution so as to educate myself.
Below is a line-by-line explanation. Is my understanding correct?


setlocal enabledelayedexpansion - the activity of the command will be kept to a specified folder

for /f "delims=" %%A in (*.txt) do - 'for' is like an "IF" command, but I don't know what /f does.
                                   - I don't get the "delims=" either.
                                   - Are the percentage signs are wildcards?
                                   - I know that *.txt means 'all text files'

    set oldname=%%A         - does this specify the format of the old name?

   set newname=!oldname:_= !  - this makes sense (I think), it changes the underscore to a space

    ren "!oldname!" "!newname!"  - this renames the file, with the new (underscoreless) name


Thank you for your insight and patience,
Honestly, you understood very well for a beginner. I'll just confirm and fix what you said about the script:
Setlocal enabledelayedexpansion I'm not exactly sure what this means, but regular variables in for commands are wrapped in ! and not %.
"Delims=" That means that there are no delimiters (things that seperate the information) to be used.
%%a (or %%A) That is a FOR variable. It is only used in the FOR command. For every line of data, it will become that. So if a line was file_name.txt, then %%a would become file_name.txt .
in (*.txt) That is where the data comes from. For each file, there is a new line. * is a wildcard. If you knew how long the desired file is, you can use a certain amount of ? (question marks are a 1-character wildcard).
Set oldfilename=%%a Doing that allows me to work with the name.
Set newfilename=!oldfilename:_= ! That will make newfilename the same as oldfilename, but every _ becomes a space.
Ren !oldfilename! !newfilename! Pretty explanitory.

Understand a little better now?
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
Salmon Trout
Sage



Thanked: 546
Posts: 7,954

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #13 on: December 31, 2009, 09:27:20 AM »

That's about it, helpmeh, but if I can just jump in with the FOR line...

for /f "delims=" %%A in (*.txt) do

The FOR command works like this

FOR [switch] [option block] %%Variable in (dataset) do (something)

You can type FOR /? at the prompt to get several screens full of detailed help but I will briefly break down what that line does

/f ... switch that tells FOR that (dataset) is to be treated as a series of lines to be processed. In this case, dataset is *.txt which means "one by one, each file with the .txt extension in the current directory".

"delims=" ... option block that tells FOR that each line of output is to be taken whole and not split into "tokens"

%%Variable ... A loop variable is a single letter, a-a or A-Z and the contents of (dataset) are assigned one by one to this variable, in this case %%A will contain, one after the other, the name of each .txt file.

IP logged


Proud to be European
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #14 on: December 31, 2009, 09:39:04 AM »

Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.

You can use vbscript. this replaces multiple _ (and consecutive _ ) with single space.
Code: [Select]
Set objFS = CreateObject("Scripting.FileSystemObject")
Set regEx = New RegExp            ' Create regular expression.
regEx.Global=True
strFolder = WScript.Arguments.Item(0)
strOld = WScript.Arguments.Item(1)
strNew = WScript.Arguments.Item(2)
regEx.Pattern = strOld&"+"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
strFileName = strFile.Name
If objFS.GetExtensionName(strFileName) = "txt" And InStr(strFileName,strOld)>0 Then
strFileName = regEx.Replace(strFileName, strNew)
strFile.Name = strFileName
End If
Next


save as replace.vbs and on command line
Code: [Select]
c:\test> cscript //nologo replace.vbs c:\test _ " "
IP logged

Pages: [1] 2 3 ... 7  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Rename/replace « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.152 seconds with 20 queries.