1.

Solve : Code - remove part of file name?

Answer»

Hi can someone create some code for me please to remove [www.lokotorrents.com] from the file name of a WHOLE bunch of files in the same directory in one command line please?

example:
C:\Documents and Settings\Administrator\My Documents\Downloads\filea[www.lokotorrents.com].nul
and
C:\Documents and Settings\Administrator\My Documents\Downloads\fileb[www.lokotorrents.com].nul

to remove [www.lokotorrents.com] from all the files in one GO thanks if you can help Why has it got to be in one line?
okay, it can be on multiple lines in which case can you create a batch file for it to run from? noticing a pattern here? I'm lazy!
I'm sure it could probably be done in one line anyways using a wild card. Basically I want to remove that part of the file name from 100's of files but not using lame-*censored* GUI windows which will take me hours. First person to give me the code wins a speed-boat.Quote

[www.lokotorrents.com]

Is this correct, that the unwanted section starts with a [ and ends with a ] ... ?

Yuuuup! you can do it?? awesome.... oh there isn't ACTUALLY a speed-boat by the way... SORRY.

[www.lokotorrents.com] <--- Remove all including '[' and ']' 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...

Quote from: Shandy on January 13, 2009, 02:50:36 PM
I'm sure it could probably be done in one line anyways using a wild card.

I doubt it.
Awesome code
Didn't work though :/
Here is what I ran as a .bat inside the folder:

@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%=!
Ren "!oldfilename!" "!newfilename!"
)
echo.
echo All done
pause

Thanks ANYWAY I'm still impressed Quote from: Shandy on January 15, 2009, 08:09:16 AM
Awesome code
Didn't work though :/

Well, it worked for me on a folder full of test files named like you said.
You don't actually say in what way it "didn't work" so I can't actually suggest what is wrong. Maybe the files are read only? Maybe you didn't describe what you wanted properly?

Code: [Select]S:\Test\Batch>dir *.nul
Volume in drive S is USBHD
Volume Serial Number is 2C51-AA7F

Directory of S:\Test\Batch

13/01/2009 22:25 0 Test-file02[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file03[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file04[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file05[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file06[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file07[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file08[www.lokotorrents.com].nul
13/01/2009 22:25 0 Test-file01[www.lokotorrents.com].nul
8 File(s) 0 bytes
0 Dir(s) 187,333,132,288 bytes free

S:\Test\Batch>test8.bat
S:\Test\Batch>dir *.nul
Volume in drive S is USBHD
Volume Serial Number is 2C51-AA7F

Directory of S:\Test\Batch

13/01/2009 22:25 0 Test-file02.nul
13/01/2009 22:25 0 Test-file03.nul
13/01/2009 22:25 0 Test-file04.nul
13/01/2009 22:25 0 Test-file05.nul
13/01/2009 22:25 0 Test-file06.nul
13/01/2009 22:25 0 Test-file07.nul
13/01/2009 22:25 0 Test-file08.nul
13/01/2009 22:25 0 Test-file01.nul
8 File(s) 0 bytes
0 Dir(s) 187,333,132,288 bytes free

S:\Test\Batch>



Test8.bat

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



Does this code only work on files with the extension .nul? By the looks of it, it works on all file types.
C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>setlocal enabledelayedexpansion

C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>set deletestring=[www.lokotorrents.com]


C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>for /F "delims==" %F in ('dir /b | find
"[www.lokotorrents.com]"') do (
set oldfilename=%F
set newfilename=!oldfilename:[www.lokotorrents.com]=!
Ren "!oldfilename!" "!newfilename!"
)

C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>pause
Press any key to continue . . .


That's what happened with the last code when i put @echo on at top and pause at the bottom. ^_^I have a feeling you just moved the goal posts, as we say in soccer countries.

Is this the string you wish to find and remove?

Quote
[WwW.LoKoTorrents.CoM]

Or is it this (as you originally stated)?

Quote
[www.lokotorrents.com]

because there is a difference. As you should be able to see. Which might explain why my code "didn't work".

And why, if I am right, you will need to change this line:

Code: [Select]for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
to this:

Code: [Select]for /f "delims==" %%F in ('dir /b /l ^| find "%deletestring%"') do (
The /l switch for DIR forces the file listing to be all lower case.

And the answer to this question

Quote
Does this code only work on files with the extension .nul? By the looks of it, it works on all file types.

is: No, it doesn't only work on files with the extension .nul, and you are right, it works on all file types. If you wanted to restrict it to only certain file types, you could modify the FOR command line like this. Let's say you only want to rename .nul files...

for /f "delims==" %%F in ('dir /b /l *.nul ^| find "%deletestring%"') do (



It's [WwW.LoKoTorrents.CoM] as you said, but I'll try that other line and see what happens this is fun! lol
Thanks for the help It worked! Your a genius!
And it's called football not soccer I'm from England ^_^So is the /1 syntax to ignore letter case?? :S


Discussion

No Comment Found