Script for renaming subfolders with same name

Hey Experts, I have a Folder with hundreds of folders each folder has subfolders the last level of folders are named Lv1 and LV2, I need to rename those folders ONLY to Lv1-org and Lv2-org
Ray PadillaIT DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

NVITEnd-user supportCommented:
Revised.

dir "c:\source\lv*" /s/b /ad >"%temp%\lv.txt"
for /f %%a in (%temp%\lv.txt) do (
  echo ren "%%a" %%~na-Org
)

Open in new window

0
Ray PadillaIT  DirectorAuthor Commented:
Not sure what that is supposed to do but the directory structure is as follows:

_Forupload\ProductName\Images\ProductName\LV1

I need that to look like this when I get done

_ForUpLoad\ProductName\Images\LV1-org

Keep in mind the Lv1 is a folder
0
NVITEnd-user supportCommented:
To run it for real, removed the ECHO.

dir "c:\source\lv*" /s/b /ad >"%temp%\lv.txt"
for /f %%a in (%temp%\lv.txt) do (
  ECHO ren "%%a" %%~na-Org
)

Open in new window

0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Ray PadillaIT  DirectorAuthor Commented:
There are 2 Folders in the images folder Lv1 and Lv2 they both need to keep the name and just append the -org to the end of the folder name
0
NVITEnd-user supportCommented:
Yes. That's what it will do.
Change c:\source to c:\_Forupload

You show _Forupload\ProductName\Images\ProductName
changes to _ForUpLoad\ProductName\Images

The second ProductName is removed. That was not is your original request.
Is that a typo?
0
NVITEnd-user supportCommented:
Open Notepad or another editor.
Copy the code to it
Save it to a file of your choice, e.g. c:\renameLV.bat
Open a CMD prompt
Type: c:\renameLV.bat

You'll see the results echo to the console.

If you are satisfied... to run it for real, remove the ECHO then save and run it again.
0
Ray PadillaIT  DirectorAuthor Commented:
yes that is a typo  The directory structure should not change I just need to find any folder within these folders named Lv1 and name them to Lv1-org same thing with the Lv2 folder it also needs to be renamed to Lv2-org
0
NVITEnd-user supportCommented:
If you are unsure, test it on a few folders first.
Make a different folder structure and put some subfolders in it, similar to the real one.
0
NVITEnd-user supportCommented:
In your example, I assume the _Forupload starts at c:\_Forupload
0
NVITEnd-user supportCommented:
If you do a test, don't forget to adjust the .BAT file path to match your test folder. E.g. if you make a test called c:\test
dir "c:\test\lv*" /s/b /ad >"%temp%\lv.txt"
for /f %%a in (%temp%\lv.txt) do (
  ECHO ren "%%a" %%~na-Org
)

Open in new window

In the real one...
dir "c:\_Forupload\lv*" /s/b /ad >"%temp%\lv.txt"
for /f %%a in (%temp%\lv.txt) do (
  ECHO ren "%%a" %%~na-Org
)

Open in new window

0
Ray PadillaIT  DirectorAuthor Commented:
my results:


C:\Users\Raymond\Desktop>renamefolder.bat

C:\Users\Raymond\Desktop>dir "c:\users\raymond\desktop\_forupload\lv*" /s/b /ad
 1>"C:\Users\Raymond\AppData\Local\Temp\lv.txt"
The system cannot find the file specified.

C:\Users\Raymond\Desktop>for /F %a in (C:\Users\Raymond\AppData\Local\Temp\lv.tx
t) do (ECHO ren "%a" %~na-Org )

C:\Users\Raymond\Desktop>
0
NVITEnd-user supportCommented:
Under "c:\users\raymond\desktop\_forupload", you have made some subfolders and some do contain the LV1 and/or LV2 subfolder?
0
Ray PadillaIT  DirectorAuthor Commented:
in the _ForUpLoad there are subfolders in subfolders as per my previous comment I just need to find the LV1 and rename it to LV1-org
0
NVITEnd-user supportCommented:
This version makes a log file %temp%\RenLV.txt

@echo off
set LogFN=%temp%\RenLV.txt
if exist "%LogFN%" del /q "%LogFN%"

dir "C:\local\test\_Forupload\lv*" /s/b /ad >"%temp%\lv.txt"
for /f %%a in (%temp%\lv.txt) do (
  echo %date% %time% "%%a" %%~na-Org
  echo %date% %time% "%%a" %%~na-Org >>"%LogFN%"
  ren "%%a" %%~na-Org
)

Open in new window


It works here on Windows 7.
See attachments

With the ECHO version...
RenLV.bat

dir "C:\local\test\_Forupload\lv*" /s/b /ad  1>"C:\Users\chris\AppData\
Local\Temp\lv.txt"

for /F %a in (C:\Users\chris\AppData\Local\Temp\lv.txt) do (echo ren "%a" %~na-Org )
(echo ren "C:\local\test\_Forupload\ProductName1\Images\ProductName\lv1" lv1-Org )
ren "C:\local\test\_Forupload\ProductName1\Images\ProductName\lv1" lv1-Org
(echo ren "C:\local\test\_Forupload\ProductName1\Images\ProductName\lv2" lv2-Org )
ren "C:\local\test\_Forupload\ProductName1\Images\ProductName\lv2" lv2-Org
(echo ren "C:\local\test\_Forupload\ProductName2\Images\ProductName\lv1" lv1-Org )
ren "C:\local\test\_Forupload\ProductName2\Images\ProductName\lv1" lv1-Org
(echo ren "C:\local\test\_Forupload\ProductName2\Images\ProductName\lv2" lv2-Org )
ren "C:\local\test\_Forupload\ProductName2\Images\ProductName\lv2" lv2-Org

Open in new window

---
With the real version (ECHO removed)...
RenLV.bat

dir "C:\local\test\_Forupload\lv*" /s/b /ad  1>"C:\Users\chris\AppData\Local\Temp\lv.txt"

for /F %a in (C:\Users\chris\AppData\Local\Temp\lv.txt) do (ren "%a" %~na-Org )
(ren "C:\local\test\_Forupload\ProductName1\Images\ProductName\lv1" lv1-Org )
(ren "C:\local\test\_Forupload\ProductName1\Images\ProductName\lv2" lv2-Org )
(ren "C:\local\test\_Forupload\ProductName2\Images\ProductName\lv1" lv1-Org )
(ren "C:\local\test\_Forupload\ProductName2\Images\ProductName\lv2" lv2-Org )

Open in new window

RenLV1.png
RenLV2.png
0

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
Ray PadillaIT  DirectorAuthor Commented:
OK do I did something stupid and really screwed up. Now I have a bunch of folders named LV1 (1)  These folders have to be LV1 arggggghhh!

C:\Users\Raymond\Desktop\__ForUpload\1-8Inch-Hydrojet-SS-15Deg-3-5-Threaded-Nozz-17-0647_HTML\Images\1-8Inch-Hydrojet-SS-15Deg-3-5-Threaded-Nozz-17-0647_HTML

This is the file structure with each product name being different of course but under that last folder there are 2 folders LV1 (1) and Lv2 I need to revert that Lv (1) back to Lv1

I'm sorry I am in a rush and somehow just screwed up all these folders.
0
NVITEnd-user supportCommented:
You are not doing this on a test folder as I suggested?
0
Ray PadillaIT  DirectorAuthor Commented:
nope, was to much in a hurry!
now I think I have screwed everything up
0
NVITEnd-user supportCommented:
Yikes.

If you still need help...

In the CMD prompt, type:
dir /s /b "C:\Users\Raymond\Desktop\__ForUpload\lv*" > Hasty.txt

Open in new window


Upload Hasty.txt and I'll take a look.
0
Ray PadillaIT  DirectorAuthor Commented:
I ran that and I see nothing where would be the file be?
0
NVITEnd-user supportCommented:
It should be in the folder where the CMD prompt was opened to.
Open the CMD prompt again.
Is the file there?
0
NVITEnd-user supportCommented:
Type: DIR hasty.txt <press enter>
0
Ray PadillaIT  DirectorAuthor Commented:
I would like to remove anything after Lv1 & Lv2  then on a VERY similar folder I need to add to the Folders Lv1 & Lv2  -org
0
NVITEnd-user supportCommented:
Would you like me to look at the hasty.txt file?
0
NVITEnd-user supportCommented:
...would like to remove anything after Lv1 & Lv2  then on a VERY similar folder I need to add to the Folders Lv1 & Lv2  -org
There are 2 things happening here.
Also, this is different from your original request.

I need a clearer explanation.
0
Ray PadillaIT  DirectorAuthor Commented:
Sorry it changed once I screwed things up.  Best way to explain it is I need to copy over a bunch of folders but I DO NOT want to copy over the LV1 and LV2 folders those need to be skipped that was why I needed to rename the original folders before I copied over the other files, but I accidentally renamed the folders of the files I did NOT need to name. So from my Productparts folder I will copy everything into my Forupload folder EXCEPT the LV1 and LV2 folders those need to remain as they are in the uploadfolder.  Those folders contain images I have watermarked and need to get up to our site
0
Ray PadillaIT  DirectorAuthor Commented:
I ownloaded a free app that did this for me in about 15 min.
0
Ray PadillaIT  DirectorAuthor Commented:
Thanks for your effort.
0
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
Scripting Languages

From novice to tech pro — start learning today.