Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: writing a variable to another batch file  (Read 5454 times)

0 Members and 1 Guest are viewing this topic.

Dan O

    Topic Starter


    Rookie
    • Yes
  • Experience: Experienced
  • OS: Windows 7
writing a variable to another batch file
« on: January 26, 2012, 08:28:17 AM »
Hello Everyone,

I am attempting to write a batch file that will create a custom script based on the information gathered in it. I am running into an issue when I try to write a variable to the new script. I am not trying to pass the value, but the variable name itself. This is what I am trying to do:

Code: [Select]
ECHO set cur_yyyy=%date:~10,4% >> %userprofile%\Desktop\Upgrade.bat
But when I view the new file it shows up as

Code: [Select]
ECHO set cur_yyyy=2012
Is there any way to do change the syntax so that is not read as 2012 when it is passed?

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: writing a variable to another batch file
« Reply #1 on: January 26, 2012, 09:14:31 AM »
Code: [Select]
ECHO set cur_yyyy=^%date:~10,4^%>new.bat

Dan O

    Topic Starter


    Rookie
    • Yes
  • Experience: Experienced
  • OS: Windows 7
Re: writing a variable to another batch file
« Reply #2 on: January 26, 2012, 10:16:51 AM »
I originally thought that as well, but but the second ^ doesn't apply. I also tried ^^ with no change.

The line simply doesn't show up in the new file.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: writing a variable to another batch file
« Reply #3 on: January 26, 2012, 10:18:13 AM »
Works for me.
Code: [Select]
H:\>ECHO set cur_yyyy=^%date:~10,4^%>new.bat

H:\>type new.bat
set cur_yyyy=%date:~10,4%

H:\>

Dan O

    Topic Starter


    Rookie
    • Yes
  • Experience: Experienced
  • OS: Windows 7
Re: writing a variable to another batch file
« Reply #4 on: January 26, 2012, 10:35:05 AM »
I have to be missing something here. Below is the entire script so far. I did try what you said above, and it worked when I typed it in the cmd window. The part I am having issues with is at the bottom. This is what I changed it to:

Code: [Select]
@echo off
setlocal ENABLEDELAYEDEXPANSION

REM Timestamp Generator
REM Parse the date (e.g., Fri 02/08/2008)
set cur_yyyy=%date:~10,4%
set cur_mm=%date:~4,2%
set cur_dd=%date:~7,2%

set timestamp=%cur_mm%%cur_dd%%cur_yyyy%



REM Gather input for file locations
REM :MENU1
REM set _intranetDirVer=
REM echo The default installation folder for SimplifyIT is C:\inetpub\wwwroot\simplifyIT\
REM set /p _intranetDirVer="Is this the location of your current installation? (yes/no)"


REM IF %_intranetDirVer%==yes GOTO MENU1a
REM IF %_intranetDirVer% NEQ "yes" GOTO MENU1b


REM :MENU1a
set _intranetDir=C:\inetpub\wwwroot\simplifyIT\
REM GOTO MENU2

REM :MENU1b
REM cls
REM echo If not at C:\inetpub\wwwroot\simplifyIT\
REM set /p _intranetDir="where is your installation currently? "
REM GOTO MENU2

REM :MENU2
REM cls
REM set _intranetDirVer=
REM echo The default backup folder for SimplifyIT is C:\Backups\%timestamp%\Inetpub\wwwroot\simplifyIT, where %timestamp% is the current date.
REM set /p _intranetDirVer="Is this location acceptable? (yes/no)"


REM IF %_intranetDirVer%==yes GOTO MENU2a
REM IF %_intranetDirVer% NEQ "yes" GOTO MENU2b


REM :MENU2a
set _backupDir=C:\Backups\%timestamp%\Inetpub\wwwroot\simplifyIT
REM GOTO MENU3

REM :MENU2b
REM cls
REM echo If not at C:\Backups\%timestamp%\Inetpub\wwwroot\simplifyIT
REM set /p _backupDir="where would you like to backup to? "
REM GOTO MENU3


REM :MENU3
REM cls
REM set _intranetDirVer=
REM echo The default file repository for new releases is %userprofile%\Desktop\NewRelease\simplifyIT\.
REM set /p _intranetDirVer="Is this location acceptable? (yes/no)"


REM IF %_intranetDirVer%==yes GOTO MENU3a
REM IF %_intranetDirVer% NEQ "yes" GOTO MENU3b


REM :MENU3a
set _newReleaseDir=%userprofile%\Desktop\NewRelease\simplifyIT\
REM GOTO SUMMARY

REM :MENU3b
REM cls
REM echo If not at %userprofile%\Desktop\NewRelease\simplifyIT\
REM set /p _newReleaseDir="where is your new file repository? "
REM GOTO SUMMARY

REM :SUMMARY
cls
echo File Summary
echo.
echo The location of the installation is:
echo %_intranetDir%
echo.
echo The location of the backup is:
echo %_backupDir%
echo.
echo The location of the new file repository is:
echo %_newReleaseDir%
echo.
pause

REM --------------------BEGIN WRITING NEW BATCH FILE------------------------------
ECHO @ECHO OFF >> %userprofile%\Desktop\Upgrade.bat
ECHO setlocal ENABLEDELAYEDEXPANSION >> %userprofile%\Desktop\Upgrade.bat
ECHO set cur_yyyy=^%date:~10,4^% >> %userprofile%\Desktop\Upgrade.bat
ECHO set cur_mm=^%date:~4,2^% >> %userprofile%\Desktop\Upgrade.bat
ECHO set cur_dd=^%date:~7,2^% >> %userprofile%\Desktop\Upgrade.bat
ECHO set timestamp=^%cur_mm^%^%cur_dd^%^%cur_yyyy^% >> %userprofile%\Desktop\Upgrade.bat



Now when I run it, I get this in Upgrade.bat:

Code: [Select]
@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
set timestamp=^

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: writing a variable to another batch file
« Reply #5 on: January 26, 2012, 10:38:26 AM »
My bad.  You have to double the parenthesis in a batch file.
Code: [Select]
ECHO set cur_yyyy=%%date:~10,4%%>new.bat

Dan O

    Topic Starter


    Rookie
    • Yes
  • Experience: Experienced
  • OS: Windows 7
Re: writing a variable to another batch file
« Reply #6 on: January 26, 2012, 10:44:01 AM »
ok, that worked! Thanks!

Salmon Trout

  • Guest
Re: writing a variable to another batch file
« Reply #7 on: January 26, 2012, 10:55:43 AM »
Works for me.

That was done at the prompt.

You can escape percent signs (%) using a caret (^) at the prompt. In a batch file it is different. You escape a percent sign with another percent sign (to echo one percent sign in a batch, you use two of them)

At the prompt:

Code: [Select]
C:\test>echo echo ^%var1^% > test.txt
Output of command typed at the prompt (test.txt):

Code: [Select]
echo %var1%
test.bat:

Code: [Select]
@echo off
echo echo ^%var1^% > test2.txt
echo echo %%var1%% >> test2.txt

Output of batch (test2.txt):

Code: [Select]
echo
echo %var1%

Notice that the first echo is blank.