Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   Batch Script that Turns Files and Directories into Variables and Paths into Values (http://www.go4expert.com/forums/batch-script-files-directories-t23262/)

Cleptography 10Sep2010 03:03

Batch Script that Turns Files and Directories into Variables and Paths into Values
 
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



All times are GMT +5.5. The time now is 03:53.