Solved

Batch file or script to change file names in a directory

Posted on 2014-03-12
16
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

739 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