Reputation: 23
Here's the current file structure: a heading folder, a person folder, and then a set of files pertaining to that person. For example, here are just the FOLDERS. Oh, BTW, obviously I'm not spending days processing data on cartoon characters; these are just illustrative to hopefully make understanding easier.
The Simpsons
Homer Simpson
Homer Simpson - At the Plant
Homer Simpson - At Duffs Bar
Homer Simpson - On a Date with Marge
Bart Simpson
Bart Simpson - Bart Writing on the Chalkboard
Bart Simpson - Playing with Millhouse
Bart Simpson - Earns Money for Camp Krusty
Bart Simpson - Sings with Lisa
Etc...
and because having the name repeated is so redundant on a large scale, I've been trying to just make:
The Simpsons
Homer Simpson
At the Plant
At Duffs Bar
On a Date with Marge
Bart Simpson
Bart Writing on the Chalkboard
Playing with Millhouse
Earns Money for Camp Krusty
Sings with Lisa
Etc...
Basically, I've been manually stripping the
<artist name><space><dash><space>
from the "base" of each child folder underneath each person. all manually from literally a thousand folders in Windows File Explorer today and trying to research/try different batch files to just do the rename programmatically. I have thousands to go, so I thought I'd ask for help.
I know StackOverflow is NOT a file writing service and it's insulting for others to think they can get someone else to do their work. I totally get it.
I have a dozen or so things that I 'think' should work, but they don't. I started to list code I've tried but I think this would be more succinct:
for recursive directories tokens=3 delims=dash %%<some variable> in (*. ) do rename <filename element 1><space><dash><space><filename element 2> with <filename element2>
Upvotes: 1
Views: 1948
Reputation: 1
Not sure if anyone is still looking for this, and I was not able to comment on the above answer due to my lack of rep, so I'm hoping I can provide some clarity here. I've had some success with the following:
Instead of using ren "%%b" "!folder:* - =!"
you will need to use a varied method to Aacini's version above, utilizing the move
command instead.
Ren
or Rename
will only rename files not folders.
Using Move
you can move a folder as well as change the directory's name in the process.
Borrowing from Aacini's code, you could try something like this:
@echo off
setlocal EnableDelayedExpansion
rem Use the following line so you can run this batch file directly from the main dir, I.E.
rem "The Simpsons"
pushd %~dp0
rem Process all "person" folders here
for /D %%a in (*) do (
rem Set a new variable for the recursive directory searches
set ndir=%%a
rem Set a variable to make referencing the future directories easier as well as
rem entering this "person" folder (you may need to place a "\" between %~dp0 and %ndir%)
rem As I mention below -- may need a subroutine for this as well.
set fdir=%~dp0%ndir%
cd "%fdir%"
rem Rename all folders here, as requested
for /F "usebackq tokens=* delims=" %%b in (`dir /B /AD`) do (
rem For making the New Directory Name, we will need to set %%b to a new
rem variable and then call out a 'subroutine' from within the for loop.
set fnm=%%b
call :Moverino
)
rem Failing to call out to a 'subroutine' will kill the variables in my experience.
:Moverino
rem Adding an echo here proves your fnm variable took the filename - not required
echo %fnm%
rem Here you are substituting every character before the ' - ' to equal "nothing"
set fnm1=%fnm:* - =%
rem Adding another echo here to echo the new fnm1 variable - this will prove whether the
rem edit works in strings at all, regardless if it is file vs directory.
echo %fnm1%
rem This timeout was just to check to make sure things worked in debugging
rem The code moves so fast, it's easy to miss what's happening.
REM timeout /t 1
rem Here is the sweet-sauce. "Ren" or "Rename" will not rename directories, however,
rem Moving directories from one to another as a different name will change the name!
Move "%~dp0%fnm%" "%~dp0%fnm1%"
Keep in mind that some things fail terribly in For loops, so if the change directory does not work, you might need to call that out into another subroutine. If this is the case, you will need to setup some kind of checksum to prevent you from utilizing the moving portion of the code too. This should only change the names of the folders within the directories.
I have not verified whether :* - =%
will do exactly what you're hoping, but this type of command has not failed me before.
Upvotes: 0
Reputation: 67216
I don't used to answer this type of questions, but your request seems funny to me, so here it is!
@echo off
setlocal EnableDelayedExpansion
rem Enter to "heading" folder
cd "The Simspons"
rem Process all "person" folders here
for /D %%a in (*) do (
rem Enter to this "person" folder
cd "%%a"
rem Rename all folders here, as requested
for /F "delims=" %%b in ('dir /B /A:D') do (
set "folder=%%b"
rem For new name: remove all characters from beginning until " - "
ren "%%b" "!folder:* - =!"
)
rem Go back to the "heading" folder
cd ..
)
This program fail if the names of the folders may include an exclamation mark !
character.
Upvotes: 1