Solved

Batch file or script to change file names in a directory

Posted on 2014-03-12
16
355 Views
Last Modified: 2014-03-12
I need a batch file or script that when a user clicks on it, they will be prompted to enter in five numbers that will change every file name in a directory before a dash in the beginning of the name.

For example we might have a file named 00000-PGD.dwg and we want to change all the files in that directory to 12345-PGD.dwg

Any assistance would be greatly appreciated.
0
Comment
Question by:regsamp
  • 10
  • 5
16 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39923383
You cannot have two files in the same directory which both have the same name. Can you elaborate on the naming convention?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39923387
Try it with this batch script; it's currently in test mode and will only display the "ren" commands it would normall run. Remove the uppercase ECHO in line 18 to run it for real. Set your source folder (no quotes, even if it contains a space) and file mask (can be *.*) in lines 3 and 4.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp\MyFiles
set SourceMask=*.dwg
:Loop
set /p NewPrefix=Enter the new 5-digit prefix for all files in %SourceFolder%: 
if "%NewPrefix%"=="" goto :eof
echo %NewPrefix%| findstr.exe /r "^[0-9][0-9][0-9][0-9][0-9]$" >NUL
if errorlevel 1 (
	echo Error: your input does not consist of 5 digits!
	echo.
	goto Loop
)
for /f "delims=" %%a in ('dir /b "%SourceFolder%\%SourceMask%"') do (
	echo Processing %%a ...
	for /f "tokens=1* delims=-" %%n in ("%%a") do set NewName=%NewPrefix%-%%o
	echo ... new name: !NewName!
	ECHO ren "%SourceFolder%\%%a" "!NewName!"
)

Open in new window

0
 

Author Comment

by:regsamp
ID: 39923401
Okay, let me try it oBdA
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:regsamp
ID: 39923560
That worked just great. I hate to ask but one last request.

Do you know how the coding could be changed so that it looks for any drawing file with a 00000- in front of it and then when the user is inputting it can be ten characters entered including a - or something?

So for example 00000-PGD.dwg could be changed to Project-10-PGD.dwg by the end user.

And it will only search for 00000- drawings.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39923620
Do you need both the 5 digit change and the 10 character change for 00000 files only, or is the 5 digit change now obsolete and you only require this for 00000-files?
0
 

Author Comment

by:regsamp
ID: 39923679
"Do you need both the 5 digit change and the 10 character change for 00000- files only"

Yes. So the script will only alter .dwg files with 00000- in front of it and we would like the input to allow up to 10 characters that can include text, a number and a -

So for example 00000-PGD.dwg could be changed to Project-10-PGD.dwg by the end user

So see how the user would activate the batch file and any file in the directory with a 00000- the user put in Project-10 and all the .dwg files in that directory became Project-10
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39923695
Sorry, you're confusing me. You say that you still need the 5-digit change (the one I implemented), but then that the new script should only handle files beginning with 00000?
Let me rephrase: Aside from the 00000 to ABCDEFGHIJ change, do you still require the 12345 to 23456 change?
Edit: and does the replacement for the 00000 files have to be exactly 10 characters, or can it be less?
0
 

Author Comment

by:regsamp
ID: 39923721
I am sorry for the confusion. I made a mistake. We do not need the 5-digit change. So just the 00000- (It has to include the dash) to ABCD or a combination of ABC123 or ABC123- ect  to 10 limit and we do not require the 12345 to 23456
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39923749
Try this then; as before, in test mode (line 18):
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp\MyFiles
set SourceMask=00000-*.dwg
:Loop
echo Please enter the new prefix for all '%SourceMask%' files in %SourceFolder%.
set /p NewPrefix=Maximum of 10 characters, empty input cancels: 
if "%NewPrefix%"=="" goto :eof
if not "%NewPrefix:~10,1%"=="" (
	echo Error: your input is longer than 10 characters!
	echo.
	goto Loop
)
for /f "delims=" %%a in ('dir /b "%SourceFolder%\%SourceMask%"') do (
	echo Processing %%a ...
	for /f "tokens=1* delims=-" %%n in ("%%a") do set NewName=%NewPrefix%%%o
	echo ... new name: !NewName!
	ECHO ren "%SourceFolder%\%%a" "!NewName!"
)

Open in new window

0
 

Author Comment

by:regsamp
ID: 39923761
Thank you. I will start testing now.
0
 

Author Comment

by:regsamp
ID: 39923887
Is there a way to get it so that the user does not have to enter in the - and just the 00000 are replaced by whatever the users type in?

So for example 00000-PGD.dwg could be become 1234ABCD-5-PGD.dwg just by inputting 1234ABCD-5
0
 

Author Comment

by:regsamp
ID: 39924269
I just need it so the - does not disappear but I can't seem to get it to do that.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39924571
In line 16, just add the "-" between "%Prefix%" and "%%o":
	for /f "tokens=1* delims=-" %%n in ("%%a") do set NewName=%NewPrefix%-%%o

Open in new window

I understood your So just the 00000- (It has to include the dash) that it should be replaced as well.
0
 

Author Comment

by:regsamp
ID: 39924584
Let me try that.
0
 

Author Comment

by:regsamp
ID: 39924601
That worked perfectly. Thank you so much for the extra help!
0
 

Author Comment

by:regsamp
ID: 39924789
Do you know if there is a way to code so that when batch file is pasted into a directory it can find out which directory it is instead of hardcoding it like this?
set SourceFolder=C:\Temp\MyFiles
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question