I need a script will remove the leading zero from the names of about 20k folders

I need a script will remove the leading zero from the names of about 20k folders.

The folders are in one location (F:\Work), and have sub folders and files within them that should not be affected.

The folders in question all have 6 digits and need to have the leading 0 removed so that they only have 5 digits.


f:\work\000123 would be changed to f:\work\00123.
Who is Participating?
exx1976Connect With a Mentor Commented:
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder("F:\Work")
For each oFile in oFolder.Files
    oFile.name = Right(oFile.Name,(Len(oFile.Name)-1))
oBdAConnect With a Mentor Commented:
Try this batch script; it's currently in test mode and will only display the "ren" commands it would normally run; remove the uppercase ECHO in line 7 to run it for real. While running, no files may be open in the folders, otherwise the names can't be changed. The script can be run several times, it will only process folder names starting with a zero, a length of 6, and consisting of exactly 6 digits.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=F:\Work
for /f "delims=" %%a in ('dir /a:d /b "%SourceFolder%\0*.*" ^| findstr.exe "^[0][0-9][0-9][0-9][0-9][0-9]$"') do (
	set NewName=%%a
	echo %%a --^> !NewName!
	ECHO ren "%SourceFolder%\%%a" "!NewName:~1,5!" 

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.