Thursday, July 17, 2014

COMMAND LINES WITH COMMON USE OF FOR LOOP

SOME COMMONLY USED COMMAND LINES ESPECIALLY FOR LOOPS

1.  Create file listing of all files or specific files (bare file names with path) from all subdirectories. It can be used to get listing of all such files on a drive letter.
        ALL FILES EXAMPLE:
DIR /S/B/A-D >> C:\Temp\XLSFILES.TXT

ONLY .XLS FILES EXAMPLE:
DIR /S/B/A-D | FIND /I "*.xls" >> C:\Temp\XLSFILES.TXT


2. Read one text file for path/location of the files and then move those files to a folder. This may be suitable when you have files scattered all  over the place in multiple sub-directories and you only need to move/copy certain files to certain location

    COPY OPERATION EXAMPLE:
    for /f "delims=########" %a in (C:\Temp\XLSFILES.TXT) do copy /Y "%a" I:\DestFolder

    MOVE OPERATION EXAMPLE:
    for /f "delims=########" %a in (C:\Temp\XLSFILES.TXT) do move /Y "%a" I:\DestFolder

3. List file name with path along with its size.
 
   ALL FILES WITHIN A SPECIFIC FOLDER EXAMPLE:
 
   for %F in ("c:\Users\Rajan\*") do @echo %F ------ %~zF

4. Combine logic to create a file listing of multiple file from different sub-directories and then get size of each file.

FILE NAMES AND SIZE USING COMMAND LINE EXAMPLE:
DIR /S/B/A-D >> E:\FileName.txt

for /f "delims=########" %F in (FileName.txt) do @echo %F ------ %~zF 
 
(assumed that user is running second command line from E:\> prompt)

4. If you have large number of text file(s) and you are only interested in certain line (containing specific words) from text file
   (a) Assume you have all your .txt files that are the input test file located in Files folder.
   (b) Create FindText.bat file and add following to it
@echo off
>results.txt (
FOR /F "delims=" %%i IN (TextFind.txt) DO (
FIND "%%i" files\*.txt | FINDSTR /v "^---- ^$"
)
)

Note: This example assumed that command prompt is already pointing to location where the BAT file is located and TextFind.txt is the file containing text that you will be searching for all *.txt files within Files folder.

1 comment:

krystal fx said...
This comment has been removed by a blog administrator.