; Bat Files (File Renamer) | Michael Ian Lapsley

Bat Files (File Renamer)

Posted on Friday, February 28, 2014, 5:44 AM

I am starting to do some work with batch (.bat) files in windows. These are simple DOS scripts that can be used in windows.

Getting Started

I suggest one download and use notepad++ to work with the .bat file. This is a free software that adds color coding to the code depending on the file name extension. (So it you open a file, fileName.bat, notepad++ will use the color coding for bat files.)

Basic Syntax

There are many resources on the internet to help with syntax and function in bat files. for instance, this site -- http://www.robvanderwoude.com/batchcommands.php -- has a alphabetical list of all commands. A command is issued in this form: [command] input1 input2 ... Type the name of the command followed by inputs. The number an types of inputs depends on the commands.

Some common commands.
  • Comments (This is important to remind yourself what you did in a file. The best way to learn to code is by looking at examples and building from your previous projects)
    • rem comment comment comment ....
  • make directory (Create a new folder, aka directory)
    • mkdir nameOfDirectory
  • directory (list the files and folders in the current directory or folder)
    • dir /options
  • text output
    • echo on (turns on automatic echo)
    • echo off (turns off automatic echo)
      • @echo off (turns off automatic echo without echoing the echo off command)
    • echo text (outputs text in the command line)

Examples

HellowWorld

To create your first bat file do the following:
  1. Install Notepad++
  2. Turn on file extensions
    1. click start button
    2. My computer
    3. in the menu bar at the top select Tools--folder options
    4. select the View tab
    5. make sure the following is unchecked: "Hide extensions for known file types"
    6. Now you will see the .xxx file extension for all files in windows.
  3. Create a new .bat file
    1. Right click on the desktop - new - text document
    2. Rename the file - HelloWorld.bat
  4. Right click on your batch (.bat) file and select "Edit with notepad++"
  5. Copy the following text and paste into the .bat file:
rem this is a comment. It does nothing.
rem the following turns off the echo
@echo off
rem see no more echo :)
echo Hello World!!
rem done!
pause
rem pause causes the window not to close so you can see the output.

6. save the file
7. double click on the file and you shoud see the output in the cmd window

Renamer

Name Extractor (To .csv)

@echo off
@set directory=C:\Directory
echo OriginalFileNames
echo OriginalFileNames > %directory%/OriginalList.txt
echo OriginalFileNames,NEW_SN,TDS,Type,Lot,Level,Week,Sample,File > %directory%/NewList.csv
FOR /r %directory% %%i in (*.raw*) do (
echo %%~nxi
echo %%~nxi >> %directory%/OriginalList.txt
echo %%~nxi >> %directory%/NewList.csv
)
@echo.
@echo Done
@echo.
@echo.
pause
@echo on

Renamer

@echo off
@set directory=Z:\13-1126PorcineControlContinuum\GoodControl\Continuum\Q460
@set list=%directory%\NewList.csv
FOR /F "skip=1 tokens=1,2,3,4,5,6,7,8,9* delims=, " %%A IN (%list%) DO (
echo Change: %%A
echo to:     "%%B_%%C_%%D_%%E_%%F_%%G_%%H_%%I"
ren "%directory%\%%A" "%%B_%%C_%%D_%%E_%%F_%%G_%%H_%%I"
)
@echo.
@echo Done
@echo.
@echo.
pause
@echo on

Links

http://notepad-plus-plus.org/

http://www.robvanderwoude.com/batchcommands.php

http://www.robvanderwoude.com/for.php

http://ss64.com/nt/for_f.html

http://ss64.com/nt/for_r.html

http://answers.yahoo.com/question/index?qid=20080107052807AAnryWH

http://stackoverflow.com/questions/10393248/get-filename-from-string-path

http://stackoverflow.com/questions/15450163/counter-in-batch

http://stackoverflow.com/questions/14013145/windows-batch-file-rename

http://www.dreamincode.net/forums/topic/40896-using-set-a-statements-in-windows-batch-file/

http://stackoverflow.com/questions/206114/batch-files-how-to-read-a-file

http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work

http://stackoverflow.com/questions/9013941/how-to-run-batch-file-from-network-share-without-unc-path-are-not-supported-me

http://www.intelliadmin.com/index.php/2007/02/create-a-date-and-time-stamp-in-your-batch-files/

http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file

http://stackoverflow.com/questions/1064557/creating-a-file-name-as-a-timestamp-in-a-batch-job

No Response to "Bat Files (File Renamer)"

Leave A Reply