0

I'm trying to create a .bat file which will change the file names on selected folder by the user. The idea is to add a sub-string at the end of the files' names unless they already has it (like a watermark signature).

For example: filename.mp4 will become filename.made_by_me.mp4

The files can be from these types: .avi .mkv .mp4

I also want it to be able to check if the filename contains Upper-case or not. If it does, it will add .Made_By_Me instead of .made_by_me to the filename.

At last, if the filename already contains the sub-string, it won't add it again.

.

.

.

.

My code so far:

for /f "delims=" %%a in ('dir /b /a-d *.avi *.mkv *.mp4 ^|findstr /iv ".Made_by_me"') do ren "%%~a" "%%~na.Made_by_me%%~xa"

I hope what I have requested is possible and really appreciate any help you can provide.

Silver007
  • 99
  • 9

2 Answers2

2

EDIT: Your request about "let the user select the folder where the .bat file run" is not clear enough; however, I modified the code below in order to do so:

2nd EDIT: I modified my solution in order to use one of the methods to select a folder given at this question as requested in a comment; I used the simplest one (the WSH Shell.Application object BrowseForFolder one).

@if (@CodeSection == @Batch) @then


@echo off
setlocal EnableDelayedExpansion

rem Select the folder via WSH Shell.Application BrowseForFolder method
for /F "delims=" %%a in ('cscript //nologo //E:JScript "%~F0"') do set "folder=%%a"
if not defined folder goto :EOF
cd "%folder%"

for /F "delims=" %%a in ('dir /B /A-D *.avi *.mkv *.mp4 ^| findstr /I /V ".made_by_me"') do (
   set "fileName=%%a"

   rem Convert the file name to lowercase letters
   for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
      set "fileName=!fileName:%%a=%%a!"
   )

   if "!fileName!" equ "%%a" (
      rem The original file name have all lowercase letters
      ren "%%a" "%%~Na.made_by_me%%~Xa"
   ) else (
      rem The original file name include at least one uppercase letter
      ren "%%a" "%%~Na.Made_By_Me%%~Xa"
   )

)
goto :EOF


@end


// JScript section

var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 5);
WSH.echo(folder ? folder.self.path : "");
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you ever so much! I really do appreciate it. Despite giving me the solution, I really would like to **understand the code behind it** since i'm kind of new to the batch files language. Plus, how do I make it so I let the user choose a directory to apply the `.bat` to (the user choose a folder in which it will run everytime the `.bat` gets activated)? – Silver007 Jan 16 '16 at 20:01
  • 1
    See the edit in my answer. If you want clearer answers, please post clearer questions... – Aacini Jan 16 '16 at 22:45
  • I'm sorry. Let me be clearer. I would like that once the user will click on the batch file, it will open him/her an option to choose a folder in the PC, like in this topic: http://stackoverflow.com/questions/15885132/file-folder-chooser-dialog-from-a-windows-batch-script The chosen path will be used to do the rest of the code. I just do not know how to mix between the two. – Silver007 Jan 17 '16 at 17:12
  • How can I even know that? I am not a mind reader! **`:/`** Anyway, see my 2nd edit... – Aacini Jan 17 '16 at 18:28
  • Again - sorry for that, and I'm really greatful for all of your help, yet it seems that while activating the example code from the topic i've mentioned above, it let me to choose from all the folders in my PC, while activating your 2nd edit's code lets me choose only from "My Documents" folder? Thanks again, I really DO appreciate all of your help. – Silver007 Jan 17 '16 at 19:47
  • Well, the explanation of the change is given in such post after "the fourth argument specifies the root of the hierarchy" (I used a 5 instead of 0x00). If this answer aids you, please click on its check-mark and also upvote it... – Aacini Jan 17 '16 at 20:51
1

Here's an alternative to Aacini's solution:

@echo off

setlocal enabledelayedexpansion
for %%a in (*.avi *.mkv *.mp4) do (
    set x=%%a
    set sign=made_by_me
    for %%u in ("A=a" "B=b") do if not "!x:%%~u!" == "!x!" set sign=Made_By_Me
    if !sign! == made_by_me if "!x:made_by_me=!" == "!x!" ren "%%a" "%%~na.!sign!%%~xa"
    if !sign! == Made_By_Me if "!x:Made_By_Me=!" == "!x!" ren "%%a" "%%~na.!sign!%%~xa"
)

The only thing you need to do is to complete the alphabet in there.

Henrik
  • 332
  • 3
  • 12