Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Computer code always runs from top to bottom. The only way to go "backwards"
- //is to use "checkpoints" you can go back to, as explained a little further below.
- //Batch is just a "script" of sorts that the CMD will follow, like an actor for a movie.
- //It will follow what's written on the script from start to finish.
- //NOTE : ^ is a special character that allows us to use letters like ? and _ in echo
- //commands. Don't let it bother you too much for now, just pretend it isn't there.
- //Here I will write code that will automatically rename file extensions in mass.
- //First things first, I want to make commands invisible to the user so things don't get
- //cluttered.
- @echo off //When a command is launched in the CMD, it gets displayed on the screen.
- //@ runs a command *without* displaying it in the command prompt.
- //Therefore, we can run "echo off" here without it displaying in the window.
- //What "echo off" does is simply hide all the next commands the same way
- //@ did with "echo off".
- :MAIN //:MAIN is a "checkpoint" that you can go back to within the code.
- //If you write "goto MAIN", the CMD will be send back up to where :MAIN is, and
- //will start reading code from that point on.
- cls //cls just clears what's written on the screen.
- //It isn't needed in this case, but you're always safer adding it and not needing it
- //than not adding it and needing it.
- echo MASS EXTENSION CHANGER //These three lines are messages that I demand the CMD to
- echo. //display. They will be displayed even if "echo off" was run
- echo Original extension, no period ^: //beforehand. You can use "echo." to display a blank line, or
- //"echo message" to display a message.
- set /p ex1=^> //set defines a variable. /p is a "switch" that you add after the
- //command to tell it how to do it's job. For an example, if you add
- ///p after set, you'll tell set to display a Prompt.
- //ex1 is the variable you're modifying.
- //The standard formatting for a variable is just
- //thing1=thing2, but in this scenario, what's after the = is
- //whatever the user writes, so we write a message there to be
- //displayed on the screen instead.
- //Here ex1 (extension 1) will be defined as whatever the user writes.
- //To use the variable's value within our code, we will write %ex1%, or !ex1!.
- //The difference between the two doesn't matter in simple programs, so just use
- //whichever one you like most for now.
- echo. //Now we ask for the user to tell us what he'd like his extensions
- echo Target extension, no period ^: //changed to, and display a message for it.
- set /p ex2=^> //Here is the same story as above, except we're defining "ex2".
- echo. //Here we confirm the user's choice.
- echo Change all .%ex1% files in the current folder to .%ex2% files^^? //I have used variables within echo to
- //display whatever the user typed in.
- choice //The choice command gives the user a simple Y/N choice. It can be
- //configured to accept more letters, display a message, ect, but just
- //focus on the Y/N part for now.
- //If the user presses Y, it will set the variable errorlevel to 1 because
- //Y is the first choice in Y and N.
- //If the user presses N, it will set the variable errorlevel to 2 because
- //N is the second choice in Y and N.
- if %errorlevel%==2 goto MAIN //The if command allows us to run commands under certain circumstances only.
- //In this case, in plain english, if %errorlevel% = 2, go to the checkpoint MAIN.
- //If the user pressed N, the program will go back to the MAIN checkpoint up above.
- //If the user did NOT press N, then that means that he obviously pressed Y, which
- //means we can move on to the actual work.
- rename *.%ex1% *.%ex2% //The rename command doesn't do ten different things - It renames.
- //In Windows in general, * refers to "everything". Which means here that
- //"everything that has .%ex1% gets renamed to filename.%ex2%".
- //Example :
- //If %ex1% = jpg_large, %ex2% = jpg, then it will rename all files named
- //"*whatever*.jpg_large" to "*whatever*.jpg".
- echo. //If the code made it this far with no errors, we can assume it went fine.
- echo Job complete. //We tell the user about it.
- pause //We pause the program so the user can read the message, and then close the window.
- //The pause command pauses a program and waits for the user to press a key to continue.
- //If there is nothing left in the script for the CMD to read, it will close on it's own.
- //Hence why I placed "pause" at the very end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement