0

I've got a user that creates multiple exported sets of textures from a visual editing program he uses daily. When these export the filenames come out with a pattern of "texture*" (e.g. texture_43522, texture511235, texture-341231).

What my user needs is a script he can run in one of these exported directories to add the letter "b" to the end the word texture so that he can import these textures properly into the next step of his workflow. (e.g. textureb_43522, textureb511235, textureb-341231)

I found a similar question here: similar question, replacing "-" with "_" but I am not well versed in batch scripting and am unable to figure out how to inject the pattern "texture" into the script instead of the dash.

Any guidance, reference, or code samples are welcome.

EDIT:

A solution I wound up using:

Get-ChildItem -Filter "*texture*" -Recurse | Rename-Item -NewName {$_.name -replace 'texture','textureB' } 

Please look at nixda's answer below and use the one best for your situation

  • The answer from nixda below is valid, but I wound up coming to this solution myself: `Get-ChildItem -Filter "*texture*" -Recurse | Rename-Item -NewName {$_.name -replace 'texture','textureB' }` – Miguet Schwab Sep 03 '15 at 14:29

1 Answers1

0

I hope PowerShell as CMDs successor is ok

Dir D:\test | Where {$_ -notmatch '^textureb' } | ForEach {
    Ren $_.Fullname $_.Name.Replace('texture','textureb')
}
nixda
  • 26,823
  • 17
  • 108
  • 156
  • Thank you for your answer, haven't had a chance to come back and comment until just now. Your solution did work but I wound up coming to this command myself: `Get-ChildItem -Filter "*texture*" -Recurse | Rename-Item -NewName {$_.name -replace 'texture','textureB' }` – Miguet Schwab Sep 03 '15 at 14:28
  • @MiguetSchwab I intentionally chose the `where-object` so you can use a regular expression. My version would also rename files called `123texture456.txt` where as yours won't. Key here is the `^` sign which marks the filename start – nixda Sep 03 '15 at 15:16
  • thanks for the explanation, wouldn't the asterisk character before and after my pattern allow for it to capture "texture" despite any preceding characters? – Miguet Schwab Sep 03 '15 at 18:49
  • @MiguetSchwab Narf, I successfully confused myself. So here we go again. `-notmatch '^textureb'` means *grab all files that aren't **starting** with textureb* ie. are already renamed from a previous run. Whereas yours mean: Grab all files which have *texture* somewhere in its name. This includes also files like *textureb123* which are already renamed froma previous run. Or 123texture.txt would also be renamed but they shouldn't since they don't fit your initial request. You should stick with my version – nixda Sep 03 '15 at 20:42