Help with scripting

Tiras25
Tiras25 used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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?


»bp
Test your restores, not your backups...
Top Expert 2016
Commented:
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
setlocal

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


»bp
Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
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.


»bp
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
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:



»bp
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial