Home / Software / Computer programming / replace characters "_ " and " . " in file names to space with a batch file
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3  All - (Bottom) Print
Author Topic: replace characters "_ " and " . " in file names to space with a batch file  (Read 14220 times)
mioo_sara
Topic Starter
Beginner



Posts: 123


« on: May 27, 2009, 05:22:02 AM »

replace characters "_ " and " . " in  file names to  space  with a batch file
==========================================================
hi
is there a way  for a .bat file to  search  in  files and folders names to  replace characters "_ " and " . " to  space?
attention=
1-(.) character that separate file names from extention  should be ignored   e.g= my.rar
2-(.) character between  numbers that indicate version  of software should be ignored
example=
windows_media.player_12_v1.23.rar
should be converted to
windows media player 12 v1.23.rar
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #1 on: May 27, 2009, 06:06:30 AM »

replace characters "_ " and " . " in  file names to  space  with a batch file
==========================================================
hi
is there a way  for a .bat file to  search  in  files and folders names to  replace characters "_ " and " . " to  space?
attention=
1-(.) character that separate file names from extention  should be ignored   e.g= my.rar
2-(.) character between  numbers that indicate version  of software should be ignored
example=
windows_media.player_12_v1.23.rar
should be converted to
windows media player 12 v1.23.rar
The "." won't work because it will make it "v1 23 rar" with no extension. BUT! You can do the underscores.

@echo off
echo Enter Filename
set /p name=
set name2=%name:_= %
ren %name% %name2%

That should work.
IP logged

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



Thanked: 37
Posts: 590


« Reply #2 on: May 27, 2009, 06:29:12 AM »

there are different format of "versioning". If you have consistent names for versions such as the sample, here's an alternative solution done in Python (on windows)
Code: [Select]
import glob,os
os.chdir(os.path.join("C:\\","test"))
for files in glob.glob("*.rar"):  #get rar files
    filename = os.path.splitext(files)
    ext=filename[-1]
    thefile = filename[0]
    if "v" in thefile:  #if there is version, check where the letter "v" is
        ind = thefile.index("v")
        change = thefile[:ind].replace("."," ").replace("_"," ")+thefile[ind:].replace("_","")+ext
    else:
        change = thefile.replace("."," ").replace("_"," ")+ext
    os.rename(files,change) 
output
Code: [Select]
C:\test>dir /B  window*rar
windows_media.player_12.rar
windows_media.player_12_v1.23.rar

C:\test>python test.py

C:\test>dir /B window*rar
windows media player 12 v1.23.rar
windows media player 12.rar


« Last Edit: June 01, 2009, 05:44:42 AM by gh0std0g74 » IP logged

Reno
Hopeful



Thanked: 32
Posts: 323




« Reply #3 on: May 27, 2009, 06:47:32 AM »

The "." won't work because it will make it "v1 23 rar" with no extension. BUT! You can do the underscores.
it will works with a bit workaround  ;)

Code: [Select]
@echo off & setlocal

call:strip "windows_media.player_12.rar"
call:strip "windows_media.player_12_v1.23.rar"
goto:eof

:strip
set n=%~n1
set a=%n:_v=&set v= v%
set a=%a:.= %
echo %a:_= %%v%%~x1

output:
Code: [Select]
D:\batch>strip
windows media player 12.rar
windows media player 12 v1.23.rar
IP logged
mioo_sara
Topic Starter
Beginner



Posts: 123


« Reply #4 on: May 27, 2009, 11:29:45 AM »

dear Helpmeh
your scripts needs me to  type a file name to  get started !!
well  i  have 1000 file every  day  and week  and i wanted to  program  scan folder and subfolders for finding and replacing whatever is possible but if i should type !! ok  i could do it with a ready  software called "batch  file renamer 2.71"
but i want to  my  own  software do it and do it automatically and unfortunately  i need batch  codes to  complete my  software
thanks
===============


dear gh0std0g74
thanks for your scripts i  havent worked with  pyton can it support batch  codes?
and put a link for download and try  that
by the way   i dont want to  put address or type any  thing i want double click  on  file and fix file names in current folder and subfolders
thanks

===============

dear Reno
i tried your script and it made a "strip" file but no file has ever been  renamed
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #5 on: May 27, 2009, 03:09:56 PM »

dear Helpmeh
your scripts needs me to  type a file name to  get started !!
well  i  have 1000 file every  day  and week  and i wanted to  program  scan folder and subfolders for finding and replacing whatever is possible but if i should type !! ok  i could do it with a ready  software called "batch  file renamer 2.71"
but i want to  my  own  software do it and do it automatically and unfortunately  i need batch  codes to  complete my  software
thanks
Ok...Umm

@echo off
for /f "delims=;" %%A in ('dir /b FOLDERNAME') do (
set a=%%A
set a=%a:_= %
ren %%A %a%
)
that should remove the _ with every file in the same folder as the batch file...not the SHOULD, this is untested.
IP logged

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



Thanked: 37
Posts: 590


« Reply #6 on: May 27, 2009, 06:51:33 PM »

dear gh0std0g74
thanks for your scripts i  havent worked with  pyton can it support batch  codes?
you don't need batch if you work with Python. Its a programming language more powerful than DOS(cmd.exe+tools).
Just type "Activestate python" in google.
[/quote]
IP logged

Reno
Hopeful



Thanked: 32
Posts: 323




« Reply #7 on: May 27, 2009, 11:12:30 PM »

dear Reno
i tried your script and it made a "strip" file but no file has ever been  renamed

my turn to reply,
i only give example how to strip the file name of unneeded characters. i leave the rest for you to modify the code. hint: use 'for' loop & 'ren' command. search in the forum, there are way too many of this kind of example.
IP logged
mioo_sara
Topic Starter
Beginner



Posts: 123


« Reply #8 on: May 28, 2009, 04:35:00 AM »

it did'nt work dear  Helpmeh
i tried to  replace FOLDERNAME with anything else but it did'nt work why?
can you  test  it yourself?
thanks
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #9 on: May 28, 2009, 03:12:17 PM »

it did'nt work dear  Helpmeh
i tried to  replace FOLDERNAME with anything else but it did'nt work why?
can you  test  it yourself?
thanks
Did you add a full address to the folder (EG. C:\Documents and Settings)? Try adding /s after the /b in my script...that also might fix it...
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
mioo_sara
Topic Starter
Beginner



Posts: 123


« Reply #10 on: May 29, 2009, 03:52:47 AM »

no it did'nt work dear  Helpmeh
i  tried below scripts  but it could not rename "my_folder" folder to  my folder

@echo off
for /f "delims=;" %%A in ('dir /b/s my_folder') do (
set a=%%A
set a=%a:_= %
ren %%A %a%
)

by  the way.. should i  type  my  folders names one by  one in  batch  file?!!!
there should be other way  to  find them  automatically
thanks
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #11 on: May 29, 2009, 06:08:20 AM »

no it did'nt work dear  Helpmeh
i  tried below scripts  but it could not rename "my_folder" folder to  my folder

@echo off
for /f "delims=;" %%A in ('dir /b/s my_folder') do (
set a=%%A
set a=%a:_= %
ren %%A %a%
)

by  the way.. should i  type  my  folders names one by  one in  batch  file?!!!
there should be other way  to  find them  automatically
thanks
Wait, the FOLDERNAME is the folder for what you want renamed.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
mioo_sara
Topic Starter
Beginner



Posts: 123


« Reply #12 on: May 29, 2009, 08:50:33 AM »

Helpmeh
is your last post a question?
or i should wait?
IP logged
Dias de verano
Guest
« Reply #13 on: May 29, 2009, 10:26:49 AM »

The "." won't work because it will make it "v1 23 rar" with no extension. BUT! You can do the underscores.

You forget that you can isolate the name part of the filename+extension using the FOR variable modifier ~n

Also you need to learn about delayed expansion.


IP logged
mioo_sara
Topic Starter
Beginner



Posts: 123


« Reply #14 on: May 29, 2009, 02:31:06 PM »

Dias de verano
can  you  write  a script? can  you  help  me with  that?
i am  not that professional
IP logged
Pages: [1] 2 3  All - (Top) Print 
Home / Software / Computer programming / replace characters "_ " and " . " in file names to space with a batch file « 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.133 seconds with 20 queries.