-4

Just wondering if anyone can help me. Normally I'm pretty got at finding some code I need an to manipulate it but horrible if I have to start from scratch.

I'm a teacher and want to have all the files labelled consistently as I have to send them off.

What I want to know is there a way to write a batch file to do the following:

I have a subject folder and in each subject folder I have the students name, booklet they are doing in a folder and then there is the file either a .doc or .docx.

I want the batch file to rename the file to what ever it was to Subject_Book#_Name.doc or .docx

My Documents/Subject/Student/Book 1/.doc

\Math A\Jim Book\Book 1 Addition\Addition Booklet.docx 
\Math A\Jim Book\Book 2 Subtraction\Subtraction Booklet.docx
Into
\Math A\Jim Book\Book 1 Addition\Math A_Addition Booklet_Jim Book.docx 
\Math A\Jim Book\Book 2 Subtraction\Math A_Subtraction Booklet_Jim Book.docx 
\Math A\Frank Sims\Book 1 Addition\Addition Booklet.docx 
\Math A\Frank Sims\Book 2 Subtraction\Subtraction Booklet.docx 
Into
\Math A\Frank Sims\Book 1 Addition\Math A_Addition Booklet_Frank Sims.docx 
\Math A\Frank Sims\Book 2 Subtraction\Math A_Subtraction Booklet_Frank Sims.docx 

Now I don't mind if I have to copy the bat file into each folder of if some amazing person can help me out and have it in the main subject folder.

I did see something like this somewhere else, but this would still take a lot of time.

title Rename Bat
echo This bat must be in the folder that 
echo contains the files to be renamed.
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
pause```
Mark
  • 1
  • 1
  • Could you add two example folder listings for a couple of imaginary students to your post (show the before listing and the desired after listing). Include all relevant folder paths and files from `My Documents` downwards. – jarmod Oct 30 '20 at 00:32
  • Math A\Jim Book\Book 1 Addition\Addition Booklet.docx Math A\Jim Book\Book 2 Subtraction\Subtraction Booklet.docx Math A\Frank Sims\Book 1 Addition\Addition Booklet.docx Math A\Frank Sims\Book 2 Subtraction\Subtraction Booklet.docx – Mark Oct 30 '20 at 00:35
  • So, Frank Sim's Addition Booklet would be renamed to `Math A_Book 1_Frank Sims.docx`? – jarmod Oct 30 '20 at 00:49
  • Yeah that is it. I also put it in the main post to make it easier to see. First time using this – Mark Oct 30 '20 at 00:51
  • 1
    PS I'd recommend zipping the entire top-level folder and storing somewhere safely before you try out any proposed solutions. I'm sure you were going to anyway. – jarmod Oct 30 '20 at 01:02
  • Thank you for that. I've tried running the bat file on a copy of the folder but I get "The system cannot find the file specified – Mark Oct 30 '20 at 01:18

2 Answers2

1

As @jarmod mentions in the comments: Keep a safe copy of your original files before you run this operation! You need a backup just in case.

This should do it if you run it from within your main directory. It'll iterate over all matching files in the subdirectories and copy into the current (main) directory.

@echo off
Setlocal enabledelayedexpansion

Set "Pattern=\"
Set "Replace=_"
set "RemovePath=!CD:%Pattern%=%Replace%!_"

For /r %%# in ("*.doc?") Do (
    Set "File=%%~f#"
    set "trimfile=!File:%Pattern%=%Replace%!"
    set "trimfile=!trimfile:%RemovePath%=% %!"

    REM If you remove "echo" from the next line, the copy will happen
    echo copy "!File!" "!trimfile!"
)

Pause

What it does:

  1. Sets a few patterns for replacement. I.e., you want to replace backslash \ with underscore _.
  2. Capture the current directory so we can replace it in the filename later.
  3. Iterate over all matching files using the full path, and replace the \ in the path with _.
  4. Remove the path from the resulting string, since we only want to start the new filename w/the next-level directory name.
  5. Echo the command that'll run once you remove the echo keyword.*

Note: The most important line is "disabled" with an echo command for you to run safely. If it looks good to you, remove the word echo, and the copy operation will happen.

Marc
  • 11,403
  • 2
  • 35
  • 45
  • Thank you both for that. I've tried running the bat file on a copy of the folder but I get "The system cannot find the file specified" – Mark Oct 30 '20 at 01:18
  • Provide the full path to one of your files. Whole thing, drive letter and all. Also indicate how you're attempting to run what I've provided. I assume you're copying the code above into a batch file, saving that batch file in the main directory at the root of your files, and running it from the command line. – Marc Oct 30 '20 at 01:25
  • D:\mligh7\My Documents\1. Class\Class\LSK\Jim Book\Book 1 WHS\Safety Assessment 2020.V1.Final.docx I have tried running the bat file while located in the LSK folder and Class folder but still no difference. Yeah copied it in to a batch file and from command prompt. Some of my students have also done weird stuff and put (1) into the file name if that makes a difference as well. – Mark Oct 30 '20 at 02:03
  • 1
    I think you should put some `echo` statements after each line in the loop that logs the variable that was set in the previous statement. That's about the only way I know to see if there's a specific filename it's failing on. But I tested several strange ones w/out issue. You haven't said where in the process you get the error, so I can't tell if it iterates several files or none at all before failing. More details are better. – Marc Oct 30 '20 at 05:07
  • Set "File=%~f#" set "trimfile=!File:\=_!" set "trimfile=!trimfile:D:_mligh7_My Documents_1. Class_LSK_=!" copy !File! !trimfile! So what I can see it isn't getting to the sub folder that it is needing it is only getting as far as the folder that it is located in – Mark Nov 01 '20 at 09:15
0

An overworked underappreciated teacher!

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN ( 
 'dir /s /b /a-d "%sourcedir%\*.doc" "%sourcedir%\*.docx" '
 ) DO for %%w in ("%%~dpa.") do for %%g in ("%%~dpw.") do for %%q in ("%%~dpg.") do ECHO REN "%%a" "%%~nxq_%%~na_%%~nxg%%~xa"
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances. The listing uses a setting that suits my system.

Greek?

No, Sanskrit.

Primarily, take a dir listing in /b basic form, /s with subdirectories, /a-d but no matching directorynames of the two filemasks.

Process the resultant list, ignoring the default delimiters ("delims=") to %%aand then take the Drive and Path of %%a (%%~dpa) and append ., which evaluates to the parent directory name without the trailing backslash. This appears to be a filename, so %%~dpw is the grandparent directoryname and %%~dpq the great-grandparent directoryname.

And then all we need to do is rename the filename in %%a to the ggp-Name and eXtension (%%~nxq) underscore name of the original file (%%~na) underscore grandparent-Name and eXtension (%%~nxg) eXtension of the original file (%%~xa)

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.

Caution: This is a once-only operation, unless you really want filenames like \Math A\Jim Book\Book 2 Subtraction\Math A_Math A_Subtraction Booklet_Jim Book_Jim Book.docx
then \Math A\Jim Book\Book 2 Subtraction\Math A_Math A_Math A_Subtraction Booklet_Jim Book_Jim Book_Jim Book.docx
...

To run the batch, all you need to do is to execute 'd:\wherever\you put\thisbatch" or if you put thisbatch.bat in a directory which is an element of your path then just thisbatch will suffice.

If you set sourcedir to . (that is, a single full stop) then the batch will be executed from whatever is your current directory.

Magoo
  • 77,302
  • 8
  • 62
  • 84