1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Batch Script that Turns Files and Directories into Variables and Paths into Values

Discussion in 'Programming' started by Cleptography, Sep 9, 2010.

  1. Cleptography

    Cleptography New Member

    Joined:
    Sep 2, 2010
    Messages:
    39
    Likes Received:
    7
    Trophy Points:
    0
    This script is a utility that accepts command line arguments based on variations of the dir command in windows which in turn creates a batch script containing directories and file names as variables and path names as values.
    The idea is simple pass this script some arguments to create another batch script containing all set commands, then the idea is when writing a script the outputted file could be used as a call to setting the variables and paths, so %cmd.exe% outputted would equal C:\WINDOWS\system32 etc...

    Code:
    @echo off
     setlocal
    
    ::
    ::
    REM This script uses variations of the dir command to create a batch script
    REM Which will set the file names and / or both directory names as a variable
    REM And the path as its value
    REM Ultimately the goal is for the outputed batch file containing the variables
    REM To be used as a call to batch from another batch inorder to set files and
    REM Directories to there path which is the value
    REM So %cmd.exe% outputed would equal C:\WINDOWS\system32
    
        :NOTE:
        REM That if directory names contain a . they will be interpreted as a file
        REM Rather than a directory name
    ::
    ::
    
    :Check_ARGS_If_Not_Exist_Goto_USAGE
    if [%~1]==[] goto :USAGE
    
    :Set_Output_File_Name
    set OutputFile=%~1
    
    :Create_Batch_Header
    echo.@echo off>>%OutputFile%
    echo.>>%OutputFile%
    
    :Pass_Command_Line_Switches_And_Arguments
    shift
    
        if /i "%~1"=="-sdd" shift&&goto :SINGLE-Directory_Include_Directory_Names_Only
        if /i "%~1"=="-sdf" shift&&goto :SINGLE-Directory_Include_File_Names_Only
        if /i "%~1"=="-ssf" shift&&goto :SINGLE-Directory_Include_Specific_File_Names_Only
        if /i "%~1"=="-sfd" shift&&goto :SINGLE-Directory_Include_File_And_Directory_Names
        if /i "%~1"=="-asd" shift&&goto :ALL-Sub_Directories_Include_Directory_Names_Only
        if /i "%~1"=="-asf" shift&&goto :ALL-Sub_Directories_Include_File_Names_Only
        if /i "%~1"=="-ads" shift&&goto :ALL-Sub_Directories_Include_Specific_File_Names_Only
        if /i "%~1"=="-adf" shift&&goto :ALL-Sub_Directories_Include_File_And_Directory_Names
    
    goto :ERROR01
    
    :SINGLE-Directory_Include_Directory_Names_Only
    for /f "tokens=*" %%A in ('dir /a:d /b "%~1"') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :SINGLE-Directory_Include_File_Names_Only
    for /f "tokens=*" %%A in ('"dir /b "%~1" | Find ".""') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :SINGLE-Directory_Include_Specific_File_Names_Only
    for /f "tokens=*" %%A in ('"dir /b "%~1" | Find /i "%~2""') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :SINGLE-Directory_Include_File_And_Directory_Names
    for /f "tokens=*" %%A in ('dir /b "%~1"') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :ALL-Sub_Directories_Include_Directory_Names_Only
    for /f "tokens=*" %%A in ('dir /a:d /b /s "%~1"') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :ALL-Sub_Directories_Include_File_Names_Only
    for /f "tokens=*" %%A in ('"dir /b /s "%~1" | Find ".""') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :ALL-Sub_Directories_Include_Specific_File_Names_Only
    for /f "tokens=*" %%A in ('"dir /b /s "%~1" | Find /i "%~2""') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :ALL-Sub_Directories_Include_File_And_Directory_Names
    for /f "tokens=*" %%A in ('dir /b /s "%~1"') do (
        echo.set %%~nxA=%~1>>%OutputFile%
    )
    goto :EOF
    
    :USAGE
    echo.Usage:    %0 [(Output File) (Switch) (Directory) (File Extensions)]
    echo.
    echo.Switches:
    echo.
    echo.-sdd (Would Process Directory and Only Directories)
    echo.-sdf (Would Process Directory and Only Files)
    echo.-ssf (Would Process Directory and Only Specific Files)
    echo.-sfd (Would Process Directory Include Directories and Files)
    echo.-asd (Would Process Sub Directories and Only Directories)
    echo.-asf (Would Process Sub Directories and Only Files)
    echo.-ads (Would Process Sub Directories and Specific Files)
    echo.-adf (Would Process Sub Directories Include Directories and Files)
    echo.
    echo.Examples:
    echo.
    echo.%0 "My Output File.bat" -sdd "C:\WINDOWS"
    echo.%0 "My Output File.bat" -sdf "C:\WINDOWS"
    echo.%0 "My Output File.bat" -ssf "C:\WINDOWS" ".exe"
    echo.%0 "My Output File.bat" -sfd "C:\WINDOWS"
    echo.%0 "My Output File.bat" -asd "C:\WINDOWS"
    echo.%0 "My Output File.bat" -asf "C:\WINDOWS"
    echo.%0 "My Output File.bat" -ads "C:\WINDOWS" ".dll"
    echo.%0 "My Output File.bat" -adf "C:\WINDOWS"
    goto :EOF
    
    :ERROR01
    echo.ERROR!!!
    echo.
    echo.Either an improper switch was used or
    echo.No switch at all was provided
    echo.Type %0 from the command line to
    echo.Retrieve usage info
    goto :EOF
     

Share This Page