10

I'm writing a batch file for automating the creation of typical folder structures for products that we sell. I would like to be able to call my batch file with 2 optional arguments; the name of the supplier and a file for creating lots of folders at once. If no supplier is supplied the script asks via standard input who the supplier is. If no file is supplied the script asks for the name of the folder you wish to create. If a file is passed as an argument I would like the script to read the file line by line and create a folder for each line, named after the contents of that line. Here is the :readFile function:

:readFile
    echo "Reading from file: %theFile%"
    FOR /F "delims=," %%a IN (%theFile%) do (
        call:makeFolder %%a
    )
    goto:EOF

Here is the :makeFolder function that optionally takes the argument of the name of the folder to create. If no argument is supplied it asks for the name via standard input.

:makeFolder  
    if [%1]==[] (
        set /p product="Enter product name: "
    ) else (
        set product=%1
    )
    if exist "P:\%supplier%\Products\%product%" (
        echo.
        echo The folder '%product%' already exists.
        echo.
        goto:EOF
    )
    mkdir "P:\%supplier%\Products\%product%\Images\Web Ready"
    mkdir "P:\%supplier%\Products\%product%\Images\Supplied"
    mkdir "P:\%supplier%\Products\%product%\Images\Edited"
    goto:EOF

My problem is that in the :makeFolder function %1 refers to the 1st argument given on the command line, not the one provided in the :readFile function. How can I achieve this? Caveat: I'm very new to batch scripting so you may have to speak to me like I'm a bit stupid.

7
  • 5
    %1 is the first argument, if you got the first parameter from the command line, it could be possible that an 'exit /b'day is missing after your main code Commented Oct 24, 2011 at 15:44
  • @jeb: This should be an answer, in my opinion. I realise that it is a guess, but it is a likely guess. And even if it turns out wrong in the OP's case, it should still be there as a possible answer, for other people (those who might come across this question) to consider it. Commented Oct 24, 2011 at 17:34
  • @jeb I'm sorry I don't understand, what is exit /b'day? I understand that $1 is the first parameter from the command line, but as I understand it you also use $1 when referencing the first argument of a function. My question is what can I do to access the first argument passed to :makeFolder without referencing the 1st command line argument? Commented Oct 24, 2011 at 18:42
  • %1 is the first parameter on the commandline only in the main part of the batch file. Inside a function, %1 means the first parameter of that function. Inside a function you have no direct access to the parameters of the commandline. Commented Oct 24, 2011 at 20:06
  • I think you are wrong with your problem description. I suggest you to insert an echo Parameter for makeFolder: %%a before the call to it, and an echo Parameter received %1 in the first line of makeFolder. This may help you to see what is happening. Commented Oct 25, 2011 at 2:06

1 Answer 1

14

I rebuild the file and it worked

@echo off
set "supplier=C:\temp\supp\"
set "product=Car"
echo test1,myComment,myValue > myFile.txt
call :readFile "myFile.txt"
EXIT /B

:readFile
echo "Reading from file: %~1"
FOR /F "usebackq delims=," %%a IN ("%~1") do (
    call :makeFolder %%a
)
goto:EOF

:makeFolder  
if "%1"=="" (
    set /p product="Enter product name: "
) else (
    set "product=%1"
)
if exist "%supplier%\Products\%product%" (
    echo(
    echo The folder '%product%' already exists.
    echo(
    goto:EOF
)
echo "%1"
echo mkdir "%supplier%\Products\%product%\Images\Web Ready"
echo mkdir "%supplier%\Products\%product%\Images\Supplied"
echo mkdir "%supplier%\Products\%product%\Images\Edited"
goto:EOF

But I would recommend to use delayed expansion, as you can got problems with the percent expansion of special characters( In this case not very relevant, as special characters are a bad choice for file/directory names).

Sign up to request clarification or add additional context in comments.

2 Comments

... expansion of (what?)
@Birger Good question, after 5 years I can't remember what I wanted to write, so I have to guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.