Help with scripting

Can someone help to produce a simple script/batch?  Need to script rename file in the certain path.
So the c:\folder\filename.pdf, will be c:\folder\folder_filename.pdf

Appreciate anyone's help!
LVL 17
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
What files do you want to rename, are they all the files in that one folder?  Or just certain files in that folder?  And no subfolders need to be searched, right?

Bill PrewIT / Software Engineering ConsultantCommented:
If those assumptions are correct then here is a small BAT script that you could use to rename all the files in a single folder.  Just adjust the BaseDir value near the top to be the folder you want to process.

@echo off

set BaseDir=c:\folder

for %%D in ("%BaseDir%") do set Parent=%%~nD

for /f "tokens=*" %%F in ('dir /b "%BaseDir%\*.*"') do (
    ECHO ren "%BaseDir%\%%~F" "%Parent%_%%~F"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
That script is currently in "test" mode, it will only display the REN command it would have executed, not actually rename any files.  If it looks right when you test it, then remove the ECHO before the REN and run it again to rename the files.

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Bill PrewIT / Software Engineering ConsultantCommented:
There are also very powerful GUI utilities that you can use to do complex file renaming, if that is something that might be useful.  A popular example is:

That's a one-liner in PowerShell. In test mode, it will only display what it would rename; remove the -WhatIf at the end to run it for real:
gci c:\temp -Filter *.pdf | ren -NewName {"$($_.DirectoryName.Split('\')[-1])_$($_.Name)"} -WhatIf

Open in new window

Tiras25Author Commented:
PS probably won't work for that user.  Non-IT person.

each user folder will have files, some/most in pinyin
and output should be user/foldername_filename.extension

Working with Bill's script now.  Maybe utilize GUI utilities he recommended.  Thank you again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.