MSFN Forum: Simple BATch rename... or is it - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simple BATch rename... or is it Rate Topic: -----

#1 User is offline   Pachilles 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 08-October 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 13 November 2012 - 06:47 PM

A simple renaming of files becomes a serious headache, and I'm not sure why it's getting so complicated.

Folder:
C:\My Files

Example list of files:
Mr. Apple.removethis.txt
My.apples.are.rotten.removethis.txt
ThisAppleIsBad.removethis.doc
I Like Apple Pie because apples are great.(Awesome even).removethis.pdf
...


Obviously (by my examples), I want to remove .removethis from all of the file names.
Simple, right?

RENAME (like most DOS) gets confused with spaces in files and folders.
RENAME gets lost with the use of periods.

I can usually work around these issues, but for some reason everything I try causes different things to happen to different files, and every work-around I come up with falls flat somewhere.
I won't even try listing them, except my latest attempt.
BTW... I've manipulated where my quotes are... pretty much everywhere, and this is my best result.

I just know that I'm missing something really simple... but this whole mission was supposed to be simple.

BAT:
for %%i in (*.txt) do (
set MyFile=%%i
rename "%MyFile%" "%MyFile:~0, -11%.txt"
)



Results in:

Quote

C:\My Files>z-MyBatchFile.bat

C:\My Files>for %i in (*.txt) do (
set MyFile=%i
rename "" "~0, -11.txt"
)

C:\My Files>(
set MyFile=Mr. Apple.removethis.txt
rename "" "~0, -11.txt"
)
The system cannot find the file specified.

C:\My Files>(
set MyFile=My.apples.are.rotten.removethis.txt
rename "" "~0, -11.txt"
)
The system cannot find the file specified.

C:\My Files>



#2 User is online   5eraph 

  • Update Packrat
  • Group: Supreme Sponsor
  • Posts: 954
  • Joined: 04-July 05
  • OS:XP Pro x64
  • Country: Country Flag

Posted 13 November 2012 - 07:47 PM

The extra space in %MyFile:~0, -11% is breaking the expression. Don't use a space after the comma.

Scratch that. "SETLOCAL EnableDelayedExpansion" is needed.

The following code should work. Notice the use of exclamation marks in place of some percent signs. And 11 becomes 15 to offset the file extension.

SETLOCAL EnableDelayedExpansion
for %%i in (*.txt) do (
set MyFile=%%i
rename "!MyFile!" "!MyFile:~0, -15!.txt"
)

This should also work, and doesn't require changing 11 to 15.

SETLOCAL EnableDelayedExpansion
for %%I in (*.txt) do (
set MyFile=%%~nI
rename "%%I" "!MyFile:~0, -11!%%~xI"
)

And this too, but will remove all instances of the text to remove regardless of where in the file name they're located.

SETLOCAL EnableDelayedExpansion
for %%i in (*.txt) do (
set MyFile=%%i
set Myfile=!MyFile:.removethis=!
rename "%%i" "!MyFile!"
)

This post has been edited by 5eraph: 13 November 2012 - 08:16 PM


#3 User is offline   Pachilles 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 08-October 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 13 November 2012 - 09:12 PM

OMG OMG!

Life saver!

I'm sooo pleased!


So, this should work for all types of files (last code suggestion: changing *.txt to *.*)?
SETLOCAL EnableDelayedExpansion
for %%i in (*.*) do (
set MyFile=%%i
set Myfile=!MyFile:.removethis=!
rename "%%i" "!MyFile!"
)



Oh, and having .removethis, actually being .remove.this won't mess with it (having an additional period)?

Testing... testing... excited testing...

This would solve ALL of my renaming issues!

Tested and works! YES!

I bow to the master!

<later>
Ok, calm now. 35k+ files with the unwanted text... easy fix, never the problem.
Managing the constant flow of new files with the offending text? Big Pain. But no longer... thanks to you.

This post has been edited by Pachilles: 13 November 2012 - 09:39 PM


#4 User is online   5eraph 

  • Update Packrat
  • Group: Supreme Sponsor
  • Posts: 954
  • Joined: 04-July 05
  • OS:XP Pro x64
  • Country: Country Flag

Posted 14 November 2012 - 01:46 AM

Glad I could help. :)

SS64.com has more information on variable substring replacements that may interest you.

#5 User is offline   Pachilles 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 08-October 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 14 November 2012 - 10:36 AM

Nice!

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy