Computer Hope

Software => Computer programming => Topic started by: mioo_sara on May 27, 2009, 05:22:02 AM

Title: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara 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
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Helpmeh 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.
Title: Re: replace characters "_ " and " . " in file names to space with a batch fil
Post by: gh0std0g74 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


Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Reno 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
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara 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
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Helpmeh 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.
Title: Re: replace characters "_ " and " . " in file names to space with a batch fil
Post by: gh0std0g74 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]
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Reno 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.
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara 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
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Helpmeh 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...
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara 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
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Helpmeh 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.
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on May 29, 2009, 08:50:33 AM
Helpmeh
is your last post a question?
or i should wait?
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Dias de verano 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.


Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara 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
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Helpmeh on May 30, 2009, 06:43:55 AM
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.



Yes I do...any links for information? Delayed expansion totally confuses me, and I have no idea what it does, or how to use it?
Title: Re:
Post by: devcom on May 31, 2009, 10:05:47 AM
try this:

Code: [Select]
@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%A in ('dir /b /s /A-D') do (
set fileName=%%~nA
set filePath=%%~dpA
set fileExt=%%~xA
for /f "tokens=1-2* delims=v" %%B in ('echo !fileName!') do (
set fileName_main=%%B
set fileName_vern=%%C
)
set fileName_main=!fileName_main:_= !
set fileName_main=!fileName_main:.= !
echo REN "!filePath!!fileName!!fileExt!" "!fileName_main!v!fileName_vern!!fileExt!"
)
pause

EDIT this will only work if filenames are set like:
name v ver
Title: Re:
Post by: gh0std0g74 on May 31, 2009, 10:46:57 AM
try this:

Code: [Select]
@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%A in ('dir /b /s /A-D') do (
set fileName=%%~nA
set filePath=%%~dpA
set fileExt=%%~xA
for /f "tokens=1-2* delims=v" %%B in ('echo !fileName!') do (
set fileName_main=%%B
set fileName_vern=%%C
)
set fileName_main=!fileName_main:_= !
set fileName_main=!fileName_main:.= !
echo REN "!filePath!!fileName!!fileExt!" "!fileName_main!v!fileName_vern!!fileExt!"
)
pause

EDIT this will only work if filenames are set like:
name v ver
why do you want to put an extra "v" in the changed file name?
Code: [Select]
C:\test\tmp>dir /B
02.txt
03.txt
04.txt
windows_media.player_12.rar
windows_media.player_12_v1.23.rar

C:\test\tmp>test.bat
REN "C:\test\tmp\02.txt" "02v.txt"
REN "C:\test\tmp\03.txt" "03v.txt"
REN "C:\test\tmp\04.txt" "04v.txt"
REN "C:\test\tmp\windows_media.player_12.rar" "windows media player 12v.rar"
REN "C:\test\tmp\windows_media.player_12_v1.23.rar" "windows media player 12 v1.23.rar"
Press any key to continue . . .
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on May 31, 2009, 10:52:33 AM
thank you dear devcom
you were successful  at the first try !!!!
but if  it's possible put some parameters in  your scripts
parameters
some files do not have version  at the end of their names
example= windows_media.player_full.rar
in  your scripts it adds "v" at the end of most of file's names
so if there is not numbers at the end of file names (example help_me.htm) or...
or if you have any other idea to  solve the problem  try to see ....
it should not add "v" at the end of these kind of files
is it possible?
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: devcom on May 31, 2009, 11:34:07 AM
Code: [Select]
@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%A in ('dir /b /s /A-D') do (
set fileName=%%~nA
set filePath=%%~dpA
set fileExt=%%~xA
for /f "tokens=1-2* delims=v" %%B in ('echo !fileName!') do (
set fileName_main=%%B
set fileName_vern=%%C
if not "!fileName_vern!" equ "" set fileName_vern=v!fileName_vern!
)
set fileName_main=!fileName_main:_= !
set fileName_main=!fileName_main:.= !
echo REN "!filePath!!fileName!!fileExt!" "!fileName_main!!fileName_vern!!fileExt!"
)
pause

PS if file name will have 'v' in it then some _ . couldn't be removed
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on May 31, 2009, 12:19:53 PM
thanks devcom
90% done  :D :)
it can  now rename almost all  files but now it needs 2 more parameters
1-  this program  can  now rename files but what about folders?
it can  detect files in  subfolders but what about folder names  themselves?
2-is there any  other possibility  to  identify  of "v" for version?
below file name show that there are still  some files that needs to be take care of
ABC VME v2.1_Setup.exe

but as i  said it can now work very  nice and its good for release  2
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: devcom on May 31, 2009, 01:13:34 PM
1) just delete /A-D from
Code: [Select]
for /f "tokens=*" %%A in ('dir /b /s /A-D') do (
2) this is hard for me so this can take long time to make this
Title: Re: replace characters "_ " and " . " in file names to space with a batch fil
Post by: gh0std0g74 on May 31, 2009, 05:35:32 PM
2) this is hard for me so this can take long time to make this
try not to use batch but use good string processing tools and you can reduce your development time significantly.
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on June 01, 2009, 01:04:36 AM
thanks dear devcom
95% done by now :)
it can  now rename folders too(thanks again)
1-but  what is that  /A-D ?
what exactly  it does?
2-  can  you  add another line to  search  in  files and folders names for replacing "_" that are located after version?
example= ABC VME v2.1_Setup.exe
i think  (.)character is now beautifully  removed or if it's belong to  version  remains
but we can  always remove "_" whether it's in front , middle or end of file names
thanks
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: devcom on June 01, 2009, 05:13:00 AM
1)
Quote
attributes:
D Directories
R Read-only files
H Hidden files
A Files ready for archiving
S System files
- Prefix meaning not
Code: [Select]
http://www.computerhope.com/dirhlp.htm
2) im out of this, to hard to do in pure batch, i leave this for gh0std0g74, hes good in vbs python perl etc so you will get what you want ;)
Title: Re: replace characters "_ " and " . " in file names to space with a batch fil
Post by: gh0std0g74 on June 01, 2009, 05:46:04 AM
2) im out of this, to hard to do in pure batch, i leave this for gh0std0g74, hes good in vbs python perl etc so you will get what you want ;)
already posted (http://www.computerhope.com/forum/index.php/topic,84290.msg560678.html#msg560678) 3 days ago.
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on June 01, 2009, 06:36:37 AM
dear gh0std0g74
your scripts  just can  rename  windows media  playe
so  what about other file?
can  you  fix  it?
Title: Re: replace characters "_ " and " . " in file names to space with a batch fil
Post by: gh0std0g74 on June 01, 2009, 06:44:51 AM
dear gh0std0g74
your scripts  just can  rename  windows media  playe
so  what about other file?
can  you  fix  it?

just change "*.rar" to "*" for all files.
one last thing, please put in some effort. if you want to use batch, at least read up on batch and understand how each statement works. If you want to use the script i proposed, read up on Python and how its syntax is used.
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Reno on June 01, 2009, 08:17:31 AM
i already gave the answer at post #4, you just need to add the for loop. if you need a lot of batch script, at least learn from the help file as suggested by ghostdog.

Code: [Select]
@echo off & setlocal

for /r %%a in (*.rar) do call:strip "%%a"
goto:eof

:strip
set n=%~n1
set a=%n:_v=&set v= v%
set a=%a:.= %
ren "%~1" "%a:_= %%v%%~x1"
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on June 02, 2009, 07:29:34 AM
gh0std0g74
Quote
at least read up on batch and understand how each statement works
Reno
Quote
at least learn from the help file as suggested by ghostdog.

i  need to inform  some information  about myself
i am  17 years old and i am  not a experienced person something that is so  clear to  you (Reno --gh0std0g74--devcom and...) is not for me !! believe it or not i try  to  search  in  google first and in  most cases i find  my  answer before even  i make a new topic but sometimes things are really  complicated  and i get confused .  so  my  only  hope is computer hope forum and you  guys
 i know that batch  progrming is not a real way  to  make a good  and powerful program but to  me  as a beginner it is
i am  working  on  a kind  of file sorter for my  computer and  my  blog
i know that you  are better and faster in  programing but who's aware of future?!!!!!! ;D ;D
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on June 02, 2009, 07:30:07 AM
Quote
just change "*.rar" to "*" for all files.

what about file name dear gh0std0g74? how can  i  replace windows media player in  your script?

i mixed  yours scriptes (Reno --gh0std0g74--devcom)  and made a wonderful program  but still  i am  not satisfied but i think  i should search  more and  more to  turn into  a more experienced programmer
thanks again  for your cooperation

and reno  your program  will  always remove "." from files names even  version exapmle nero v2.12
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Reno on June 02, 2009, 09:01:24 AM
and reno  your program  will  always remove "." from files names even  version exapmle nero v2.12

because it only detect '_v'
try this, it will detect 'v[0-9]', i couldnt' find a way to hack a for loop to work with 'set &' trick. and findstr/o doesnt display the offset as intended, so this is the manual way.
Code: [Select]
@echo off & setlocal

for /r %%a in (*.rar) do call:strip "%%a"
goto:eof

:strip
set app=%~n1&set ver=
set app=%app:v0=&set ver=v0%
set app=%app:v1=&set ver=v1%
set app=%app:v2=&set ver=v2%
set app=%app:v3=&set ver=v3%
set app=%app:v4=&set ver=v4%
set app=%app:v5=&set ver=v5%
set app=%app:v6=&set ver=v6%
set app=%app:v7=&set ver=v7%
set app=%app:v8=&set ver=v8%
set app=%app:v9=&set ver=v9%
set app=%app:.= %
set app=%app:_= %
set pad=%app%                           
echo Renaming App : %pad:~,25% Ver : %ver%
ren "%~1" "%app%%ver%%~x1"
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on June 02, 2009, 10:27:16 AM
thanks  dear reno 100% done !!! :) :)
wonderful  what a beautiful algorithm !! thanks man
it can  detect "_" and "." all  over the file names and remains version  and extention
just for a final  request can  you  put folders and subfolders in your program?
it just now check and rename files
thanks
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: Reno on June 03, 2009, 02:13:24 AM
the following will rename files and folders that contain "v[0-9]" in its name. it wont work if name contains '=' sign.
Code: [Select]
@echo off & setlocal

for /r /d %%a in (*) do call:strip "%%a"
for /r %%a in (*) do call:strip "%%a"
goto:eof

:strip
set app=%~n1&set ver=
set app=%app:v0=&set ver=v0%
set app=%app:v1=&set ver=v1%
set app=%app:v2=&set ver=v2%
set app=%app:v3=&set ver=v3%
set app=%app:v4=&set ver=v4%
set app=%app:v5=&set ver=v5%
set app=%app:v6=&set ver=v6%
set app=%app:v7=&set ver=v7%
set app=%app:v8=&set ver=v8%
set app=%app:v9=&set ver=v9%
set app=%app:.= %
set app=%app:_= %
if /i "%~nx1" neq "%app%%ver%%~x1" (
set pad=%app%                           
echo Renaming App : %pad:~,25% Ver : %ver% Ext : %~x1
ren "%~1" "%app%%ver%%~x1"
)
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: mioo_sara on June 03, 2009, 06:12:24 AM
thanks dear RENO :P
folders and files are ok  now thanks for your cooperation
have a nice and good day :)
Title: Re: replace characters "_ " and " . " in file names to space with a batch file
Post by: sepgit on September 09, 2009, 10:10:48 PM
Hello I need some help,
I want to replace letter # with R in evry folder and every file.eg.4676867#565:4676867R565
please help me as soon as posible