Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

Batch Command for Renaming files in folders

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 AJim BookBook 1 AdditionAddition Booklet.docx 
Math AJim BookBook 2 SubtractionSubtraction Booklet.docx
Into
Math AJim BookBook 1 AdditionMath A_Addition Booklet_Jim Book.docx 
Math AJim BookBook 2 SubtractionMath A_Subtraction Booklet_Jim Book.docx 
Math AFrank SimsBook 1 AdditionAddition Booklet.docx 
Math AFrank SimsBook 2 SubtractionSubtraction Booklet.docx 
Into
Math AFrank SimsBook 1 AdditionMath A_Addition Booklet_Frank Sims.docx 
Math AFrank SimsBook 2 SubtractionMath 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```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...