Home / Software / Computer programming / Code - remove part of file name
0 Members and 3 Guests are viewing this topic. « previous next »
Pages: 1 [2]  All - (Bottom) Print
Author Topic: Code - remove part of file name  (Read 9392 times)
Dias de verano
Guest
« Reply #15 on: January 16, 2009, 01:17:36 PM »

It worked!

Wicked!  :) :) :) :) :) :)

Quote
Your a genius! :P

Yup.

Quote
And it's called football not soccer :P I'm from England ^_^

So am I. It's just that there are so many Seppos on here, I thought I had to use a word they'd understand.

Glad I could give you a hand, shandy  :)
IP logged
Shandy
Topic Starter
Beginner



Thanked: 3
Posts: 111


« Reply #16 on: January 16, 2009, 01:18:59 PM »

 :o :o :o :o What a guy! hehe
IP logged
Dias de verano
Guest
« Reply #17 on: January 16, 2009, 01:22:06 PM »

So is the /1 syntax to ignore letter case?? :) :S

It's not a figure 1, it's a small L, although a capital L works just as well. It forces the output of DIR to be in all lower case, whatever the case of the letters in the filenames.

IP logged
Shandy
Topic Starter
Beginner



Thanked: 3
Posts: 111


« Reply #18 on: January 16, 2009, 01:51:24 PM »

I see, so was it there to convert everything to lowercase so when its looking for that string the case of the letters won't be an issue?
I take it that it didn't work before because it was looking for [www.lokotorrents.com] in lower case and couldn't find that exact string? would it have worked if we changed set deletestring=[www.lokotorrents.com] to set deletestring=[WwW.LoKoTorrents.CoM] ?
IP logged
Dias de verano
Guest
« Reply #19 on: January 16, 2009, 02:14:17 PM »

I see, so was it there to convert everything to lowercase so when its looking for that string the case of the letters won't be an issue?

Yes, that's right.

Quote
I take it that it didn't work before because it was looking for [www.lokotorrents.com] in lower case and couldn't find that exact string?

Yes, that's right too. The new filename was produced from the old one by using the string replace feature of the SET command

set newstring=%oldstring:A=B% where A is the string you are looking for and B is the string you want to replace it with. If B is absent it replaces A with nothing, i.e. removes it. It is case sensitive. For example

set string=Mary had a little lamb
set string=%string:Mary=John%
string becomes John had a little lamb
set string=%string:little =%          (include the space after little!)
string becomes John had a lamb

Quote
would it have worked if we changed set deletestring=[www.lokotorrents.com] to set deletestring=[WwW.LoKoTorrents.CoM] ?

Yes, that's the other way we could have solved the problem.

IP logged
Shandy
Topic Starter
Beginner



Thanked: 3
Posts: 111


« Reply #20 on: January 16, 2009, 02:35:04 PM »

I see how it works now, thanks a lot, this code will be useful! :)
IP logged
sammmurai
Newbie



Posts: 1


« Reply #21 on: May 25, 2010, 07:11:04 AM »

Code: [Select]

@echo off
setlocal enabledelayedexpansion
set deletestring=[www.lokotorrents.com]
echo Ready to start
echo.
pause
echo.
for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
echo Ren "!oldfilename!" "!newfilename!"
)
echo.
echo All done
pause


Copy the above into your editor e.g. Notepad. Save as a .bat or .cmd file in the folder where the files are. Run it. See the results. If you are happy with the way it offers to rename the files, remove 'echo' from the beginning of the line starting 'echo Ren' & save and run it for real.

Maybe backup the files first in case it all goes wrong?

Or change Ren to Copy?

Your choice.

I expect you can see where to edit so it will remove other strings...



You're the MAN!

AWESOME CODE. Just what i needed...

Thanx! Gracias!

IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #22 on: June 01, 2010, 01:36:21 PM »

I miss Dias de verano...
IP logged


Proud to be European
treasuregetter
Newbie



Posts: 2


« Reply #23 on: July 13, 2010, 10:40:38 PM »

Hello. I can only hope you still get this considering the age of this discussion. This script works wonderfully except it is only written to handle one folder/directory of files. Is there a way to run recursively on all subfolders of a primary folder? I'd really appreciate any help you can provide! I have to remove two characters,  "-2" from approximately 35,000 files.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #24 on: July 14, 2010, 12:08:05 AM »

try this

@echo off
setlocal enabledelayedexpansion
set deletestring=-2
echo Ready to start
echo.
pause
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
        set oldfilename=%%~nxF
        set pathname=%%~dpF

        set newfilename=!oldfilename:%deletestring%=!
        echo Ren "!pathname!!oldfilename!" "!newfilename!"
        )
echo.
echo All done
pause
(
IP logged


Proud to be European
treasuregetter
Newbie



Posts: 2


« Reply #25 on: July 17, 2010, 02:32:59 AM »

The code provided was very much appreciated. Unfortunately I am feeling slightly murderous at the moment. This worked very well but I encountered a large number (in the hundreds) of folders with duplicate files in them. I need this batch file to check the folders for a existing file first before renaming the "####_2.xxx" file to remove the _2. This will ensure removal of the old file first. Any more help you can provide would be greatly appreciated!

My best attempts at the code:

#1)

@echo off
setlocal enabledelayedexpansion
set deletestring=_2
echo Ready to start
echo.
pause
echo.


goto findbadfile

:findbadfile
for /f "delims==" %%F in ('dir /b /s /a-d /l *.mp3 ^| find "%deletestring%"') do (
   set oldfilename=%%~nxF
   set pathname=%%~dpF
   set newfilename=!oldfilename:%deletestring%=!
   if exist "!pathname!!newfilename!" (goto deletefirst) else (goto renamer)

   echo.
echo All done
pause
)

:deletefirst
   echo Found existing duplicate file !pathname!!newfilename! that should be removed first...   
   echo Deleting !pathname!!newfilename!
   del -f "!pathname!!newfilename!"
   goto renamer



:renamer
echo.
   echo Renaming....
   echo !pathname!!oldfilename!   to
   echo !pathname!!newfilename!....
   Ren "!pathname!!oldfilename!" "!newfilename!"
rem   echo Press a key to continue or Ctrl-C to stop
   
   goto findbadfile
   
#2)

@echo off
setlocal enabledelayedexpansion
set deletestring=_2
echo Ready to start
echo.
pause
echo.


goto findbadfile

:findbadfile
for /f "delims==" %%F in ('dir /b /s /a-d /l *.mp3 ^| find "%deletestring%"') do (
   set oldfilename=%%~nxF
   set pathname=%%~dpF
   set newfilename=!oldfilename:%deletestring%=!
   if exist "!pathname!!newfilename!" (goto deletefirst) else (goto renamer)
rem   Ren "!pathname!!oldfilename!" "!newfilename!"
   echo.)
echo All done
pause


:deletefirst
   echo Found existing duplicate file !pathname!!newfilename! that should be removed first...   
   echo Deleting !pathname!!newfilename!
rem   del -f "!pathname!!newfilename!"
rem   Ren "!pathname!!oldfilename!" "!newfilename!"
rem   goto renamer



:renamer
echo.
   echo Renaming....
   echo !pathname!!oldfilename!   to
   echo !pathname!!newfilename!....
rem   Ren "!pathname!!oldfilename!" "!newfilename!"
   echo Press a key to continue or Ctrl-C to stop
   
rem   goto findbadfile
   

I have made multiple attempts but to no avail...it either does nothing or ends up deleting files that it shouldn't. I guess I don't understand how the FOR-DO loops work when combined with an IF-THEN-ELSE very well.

Thanks in advance!
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #26 on: July 17, 2010, 02:40:10 AM »

Do you mean that the pre-existing duplicate filename is found in the same folder as the file with -2 in its name which you want to rename, and that you wish to delete this pre-existing file first?

If that is the case, try this


@echo off
setlocal enabledelayedexpansion
set deletestring=-2
echo Ready to start
echo.
pause
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
        set oldfilename=%%~nxF
        set pathname=%%~dpF
        set newfilename=!oldfilename:%deletestring%=!
        if exist "!pathname!!newfilename!" (
            echo Filename conflict
            echo Folder     : !pathname!
            echo Filename : !newfilename!
            echo Deleting file
            Del "!pathname!!newfilename!"
            )
        Ren "!pathname!!oldfilename!" "!newfilename!"
        )
echo.
echo All done
pause


IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #27 on: July 17, 2010, 05:01:06 AM »

Or this

Code: [Select]
@echo off
setlocal enabledelayedexpansion
set deletestring=-2
echo Ready to start
echo.
pause
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
        set oldfilename=%%~nxF
        set pathname=%%~dpF
        set newfilename=!oldfilename:%deletestring%=!
        if exist "!pathname!!newfilename!" (
            echo Filename conflict
            echo Folder            : !pathname!
            echo Present  filename : !oldfilename!
            echo Proposed filename : !newfilename!
            echo This file exists.
            echo Deleting file
            Del "!pathname!!newfilename!"
            )
        Ren "!pathname!!oldfilename!" "!newfilename!"
        )
echo.
echo All done
pause
IP logged


Proud to be European
Pages: 1 [2]  All - (Top) Print 
Home / Software / Computer programming / Code - remove part of file name « 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.122 seconds with 19 queries.