Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. //Computer code always runs from top to bottom. The only way to go "backwards"
  2. //is to use "checkpoints" you can go back to, as explained a little further below.
  3. //Batch is just a "script" of sorts that the CMD will follow, like an actor for a movie.
  4. //It will follow what's written on the script from start to finish.
  5.  
  6. //NOTE : ^ is a special character that allows us to use letters like ? and _ in echo
  7. //commands. Don't let it bother you too much for now, just pretend it isn't there.
  8.  
  9. //Here I will write code that will automatically rename file extensions in mass.
  10.  
  11. //First things first, I want to make commands invisible to the user so things don't get
  12. //cluttered.
  13.  
  14.  
  15. @echo off //When a command is launched in the CMD, it gets displayed on the screen.
  16. //@ runs a command *without* displaying it in the command prompt.
  17. //Therefore, we can run "echo off" here without it displaying in the window.
  18.  
  19. //What "echo off" does is simply hide all the next commands the same way
  20. //@ did with "echo off".
  21.  
  22.  
  23. :MAIN //:MAIN is a "checkpoint" that you can go back to within the code.
  24. //If you write "goto MAIN", the CMD will be send back up to where :MAIN is, and
  25. //will start reading code from that point on.
  26.  
  27.  
  28. cls //cls just clears what's written on the screen.
  29. //It isn't needed in this case, but you're always safer adding it and not needing it
  30. //than not adding it and needing it.
  31.  
  32.  
  33. echo MASS EXTENSION CHANGER //These three lines are messages that I demand the CMD to
  34. echo. //display. They will be displayed even if "echo off" was run
  35. echo Original extension, no period ^: //beforehand. You can use "echo." to display a blank line, or
  36. //"echo message" to display a message.
  37.  
  38.  
  39. set /p ex1=^> //set defines a variable. /p is a "switch" that you add after the
  40. //command to tell it how to do it's job. For an example, if you add
  41. ///p after set, you'll tell set to display a Prompt.
  42. //ex1 is the variable you're modifying.
  43.  
  44. //The standard formatting for a variable is just
  45. //thing1=thing2, but in this scenario, what's after the = is
  46. //whatever the user writes, so we write a message there to be
  47. //displayed on the screen instead.
  48.  
  49. //Here ex1 (extension 1) will be defined as whatever the user writes.
  50. //To use the variable's value within our code, we will write %ex1%, or !ex1!.
  51. //The difference between the two doesn't matter in simple programs, so just use
  52. //whichever one you like most for now.
  53.  
  54.  
  55. echo. //Now we ask for the user to tell us what he'd like his extensions
  56. echo Target extension, no period ^: //changed to, and display a message for it.
  57.  
  58.  
  59. set /p ex2=^> //Here is the same story as above, except we're defining "ex2".
  60.  
  61.  
  62. echo. //Here we confirm the user's choice.
  63. echo Change all .%ex1% files in the current folder to .%ex2% files^^? //I have used variables within echo to
  64. //display whatever the user typed in.
  65.  
  66.  
  67. choice //The choice command gives the user a simple Y/N choice. It can be
  68. //configured to accept more letters, display a message, ect, but just
  69. //focus on the Y/N part for now.
  70.  
  71. //If the user presses Y, it will set the variable errorlevel to 1 because
  72. //Y is the first choice in Y and N.
  73.  
  74. //If the user presses N, it will set the variable errorlevel to 2 because
  75. //N is the second choice in Y and N.
  76.  
  77.  
  78. if %errorlevel%==2 goto MAIN //The if command allows us to run commands under certain circumstances only.
  79. //In this case, in plain english, if %errorlevel% = 2, go to the checkpoint MAIN.
  80. //If the user pressed N, the program will go back to the MAIN checkpoint up above.
  81.  
  82. //If the user did NOT press N, then that means that he obviously pressed Y, which
  83. //means we can move on to the actual work.
  84.  
  85.  
  86. rename *.%ex1% *.%ex2% //The rename command doesn't do ten different things - It renames.
  87. //In Windows in general, * refers to "everything". Which means here that
  88. //"everything that has .%ex1% gets renamed to filename.%ex2%".
  89.  
  90. //Example :
  91. //If %ex1% = jpg_large, %ex2% = jpg, then it will rename all files named
  92. //"*whatever*.jpg_large" to "*whatever*.jpg".
  93.  
  94.  
  95. echo. //If the code made it this far with no errors, we can assume it went fine.
  96. echo Job complete. //We tell the user about it.
  97.  
  98.  
  99. pause //We pause the program so the user can read the message, and then close the window.
  100. //The pause command pauses a program and waits for the user to press a key to continue.
  101.  
  102.  
  103. //If there is nothing left in the script for the CMD to read, it will close on it's own.
  104. //Hence why I placed "pause" at the very end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement