Batch rename in DOS or Windows Powershell

jasong1968

New member
VIP
Local time
9:24 AM
Messages
107
Hi - sorry if this is not the right forum - please feel free to move this is need be.

So, I have this for batch renaming files in a folder:

Dir | rename-item -newname { $_.name -replace "oldname","newname" }

And it works like a charm. The thing I can not figure out how to do is add characters - w/o changing any. So let's say I have 3 files: 1.txt 2.txt and 3.txt - I want to change them to be x1.txt x2.txt and x3.txt

So I try replacing 'replace' with 'add' like this:

Dir | rename-item -newname { $_.name -add "x", }

But it doesn't work. Does anyone know if this is possible, and if so, what I am missing in my command line?

Thanks!
 

My Computer

OS
Windows7 64
DOS - String Manipulation (String Concatenation)

If I had to guess, the code you need is:
Code:
Dir | rename-item -newname { %$_.name%x%% }

I modified a vbs script I wrote for someone else. This will work while respecting extensions, but it'll also rename folders
Code:
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\path\to\folder\of\files"
Set objFolder = objFS.GetFolder(strFolder)
For Each File In objFolder.Files
	strFileName = File.Name
	For i=1 To Len(strFileName)
		if Mid(strFileName,i,1) = "." Then
			strEndOfFirst = i
		End If
	Next
	strFirstName = Mid(strFileName,1,strEndOfFirst - 1)	
	strLastName = Mid(strFileName,strEndOfFirst)
	strNewName = strFirstName & "x" & strLastName
	File.Name=strNewName
Next
save a rename.vbs , and change "C:\path\of\folder\of\folders" to the folder where the files are located

After that, I'd recommend taking a look at ReNamer. it's pretty powerful and should do what you want easily.
 
Last edited:

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Apple 17" iMac MA199LL (Early 2006)
OS
Windows 8 Pro (32-bit)
CPU
1.83GHz Intel Core Duo
Memory
2GB 667MHz DDR2 SDRAM (PC2-5300) (upgrade)
Graphics Card(s)
ATI Radeon X1600 with 128MB GDDR3 memory
Monitor(s) Displays
17-inch TFT active-matrix LCD, millions of colors
Screen Resolution
1440 x 900
Hard Drives
Hitachi 320GB HDT721032SLA360 7200RPM SATA II (upgrade)
Keyboard
Microsoft Wired Keyboard 600
Mouse
Microsoft Basic Optical Mouse v2.0
Internet Speed
4 Mbps
Antivirus
Microsoft Security Essentials
Browser
Google Chrome
Other Info
WEI:
Base Score: 3.9 Processor: 4.4 Memory 4.7
Graphics: 3.9 Gaming Graphics: 4.1 Primary HD: 5.9
Back
Top