It always seems so easy at first...

Hewlett-packard / Iq505
January 28, 2009 at 18:12:47
Specs: Windows Vista , Dual Core/4GB
Okay folks... I have the following beast of burden that I am trying to shove around on my screen but its not cooperating...
::Batch Tool to accept user input to create new folder and copy files from template folder.

@echo off
Set /P Dashboard_Name=Please Enter a name for your new dashboard here.
XCOPY /E /I /Q /Y ".\Tools\Dashboards\Dashboard_Template\*" ".\Tools\Dashboards\%USERPROFILE%"

I thought I was so smart in adding the period instead of the long path name...and then when I added the %USERPROFILE% at the end of the pathname I just knew that I was gonna put the best batch scripters to shame... until of course the batch file just glared at me in disgust and did nothing after I entered the name for the folder. :( Tearssssss.... oh please tell me what I did wrong this time. Will I ever get this right? I used several other mutations of the above code to try to force it to work but of course I am here again because I have failed horribly at such a simple task. Oh the shame of it all. lol.

See More: It always seems so easy at first...

Report •

January 28, 2009 at 18:50:02
First, drop the @echo off. You don't need it, and it only hinders debugging. To this day, between 80%-90% of the Command Scripts I write never turn echo off.

Second, your XCOPY line is completely hosed. Your relative paths are screwed (Do you expect everyone who runs this script will have the template directory in their user directory?), and, well, let's just see what you're asking XCOPY to do:

C:\Users\root>echo XCOPY /E /I /Q /Y ".\Tools\Dashboards\Dashboard_Template\*" ".\Tools\Dashboards\%USERPROFILE%"
XCOPY /E /I /Q /Y ".\Tools\Dashboards\Dashboard_Template\*" ".\Tools\Dashboards\C:\Users\root"

Now, let's take it one step further, and replace the ".\" with what it represents, the current directory:

C:\Users\root>setlocal enabledelayedexpansion

C:\Users\root>set a=XCOPY /E /I /Q /Y ".\Tools\Dashboards\Dashboard_Template\*" ".\Tools\Dashboards\C:\Users\root"

C:\Users\root>set a=!a:.\=C:\Users\root\!
XCOPY /E /I /Q /Y "C:\Users\root\Tools\Dashboards\Dashboard_Template\*" "C:\Users\root\Tools\Dashboards\C:\Users\root"


Report •

January 28, 2009 at 20:27:26
Hi Razor... Thanks for the reply. I was following along pretty well even in my newbie brain until I hit this chunk of your code...


um.... uh....what in the worlds is that? Just when I thought I had finished reading the SET /? then you spit out this new wad of code on me. :( tearsssss.... anyway.. I will experiment with this in a few hours. If you have any more solutions please feel free to post. Thanks again.

Report •

January 28, 2009 at 21:13:38
It was actually set a=!a:.\=%CD%\!, but the %CD% part had already expanded out.

Anyways, it's a combination of these relevant parts:

E:\Documents and Settings\root>set /?
Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

  variable  Specifies the environment-variable name.
  string    Specifies a series of characters to assign to the variable.

 . . . . .

Environment variable substitution has been enhanced as follows:


would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.

 . . . . .

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

 . . . . .

%CD% - expands to the current directory string.

EDIT: I might as well post the actual script (called a.cmd, obviously) (Also, notice the lack of @ECHO OFF):

setlocal enabledelayedexpansion
set a=XCOPY /E /I /Q /Y ".\Tools\Dashboards\Dashboard_Template\*" ".\Tools\Dashboards\%USERPROFILE%"
set a=!a:.\=%CD%\!
@echo %a%

Report •

Related Solutions

Ask Question