Saturday, March 25, 2017

powershell - Batch Rename all PNG files in folders and subfolders

I am trying to rename all PNG files within several subfolders. See below for my explanation on what I am doing:


What is currently in place:
COMPANY
SERVER1
(Random).png
COMPANY2
SERVER32
(Random).png
What I want:
COMPANY
SERVER1
Company - Server1 - (Date PNG Modified).png
COMPANY2
SERVER32
Company2 - Server32 - (Date PNG Modified).png

How can I accomplish this? The part that I am having trouble with is getting the First and second directory name as well as the date modified that I can stick into the file name.


The current script that i have is below:


@echo off
for /r %%D in (.) do (
for /f "delims=" %%F in ('dir /b /A:-D "%%D\*.png"') do (
echo ren "D:\FinalDir\%%F" "%%~nxD-VirtualBootTest-3-3-18.png"
)) >> "D:\FinalDir\RenameCommands.txt"
FOR /R "D:\OriginalDir" %%i IN (*.png) DO COPY "%%i" "D:\FinalDir"

What this is doing right now is gathering all of the PNG files in all directories in the folder that building a batch file to rename all of the files that it copies out into the FinalDir. The renaming end result will end up looking like SERVER1-VirtualBootTest-3-3-18.png. I cant figure out how to make the date change with the file and add what the directory is two directories above the file.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...