Computing.Net > Forums > Disk Operating System > Set variable to contents of a file.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Set variable to contents of a file.

Reply to Message Icon

Name: Ecoastharley
Date: June 24, 2003 at 09:27:17 Pacific
OS: WinNT
CPU/Ram: P5/1Gb
Comment:

Hello,

I was just trying to set a variable to the contents of a file in a batch file. For example... I did a CD>Current_dir.txt to find out where I am.. and then want to set a CRNT_DIR=current_dir.txt... but can't figure out the syntax.

Any thoughts? Would be greatly appreciated.

Or a better way to get the current dir in a batch file.. so I can copy some files to it.

~Bob



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: June 25, 2003 at 01:57:57 Pacific
Reply:

The following sample batch guides you to the solution; anyway there are other ways to fit your need.

@Echo Off

:: XCD.BAT Syntax: XCD , batch to store the
:: current directory into a variable

For /F "tokens=*" %%A in ('CD') Do Set Crnt_Dir=%%A

Echo.
Echo The current directory is %Crnt_Dir%

Good work.


0

Response Number 2
Name: Secret_Doom
Date: June 25, 2003 at 10:50:06 Pacific
Reply:

However, if you just want to copy files to the current directory, use the following syntax:

copy c:\path\file.ext file.ext

Or this one:

copy c:\path\file.ext .

The dot (.) on the begin of a filename's path stands for the current directory (doesn't mean it will be expanded to the CD's name, a command like "echo ." won't display the current directory.

Now, if you'll be changing the directory and need the original CD on a variable, you'd better use IVO's method. On NT5+ (Win 2K/XP), the dynamic variable %CD% carries the CD by default. But I reckon on NT4- it's different. So, on NT5+, all you gotta do is:

set ORIGINAL_CD=%CD%

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Set variable to contents of a file.

Inserting lines to top of a file www.computing.net/answers/dos/inserting-lines-to-top-of-a-file/16317.html

Changing part of a file www.computing.net/answers/dos/changing-part-of-a-file/1128.html

Address of a file on a drive www.computing.net/answers/dos/address-of-a-file-on-a-drive/2191.html