1.

Solve : Attribute to txt and back doesn't work in batch, please help me?

Answer»

I'm trying to make a batch file which makes an attribute from a folder by using the %cd% command. After the batch has made the attribute it writes it to a text file. Then after exiting the batch I want to make another batch open the text file and then make a new attribute from it.
This is what I have (well, only the essential part of the code for this question)
Code: [Select]set Path=%cd%
echo %path% >> Path.txt
So after this I want something like Code: [Select]set Path=Path.txt but that doesn't do what I want because I want the original path as output, not the file name.
I hope I made clear what my problem is.
I've been looking for hours to find a solution, but I really haven't been able to find it anywhere. So can anybody please please help me.%cd% is not a "command" it is a variable. It holds the path of the current directory. Regarding your script, it would be dangerous and foolish to set the PATH variable to contain just one folder, because then many programs and commands would not work. Why do you want to do this? I am suspicious of those scripts where the author only presents the "essential part". What are they hiding? Sounds like a would be script kiddie or prankster.

Well, what my program is MEANT to do is make a backup of a file and then copy another file to that place to to it's place, and there's also an uninstall option which deletes the other file and then place the original file back. I'm using this to be able to switch between those two files. But to make it more versatile I wanted to be able to USE the batch not only from the folder it's in, but from any folder it's in. For this I made this piece of code (which again is not my complete program, but that would just be a huge field of text with a lot of echo's):
Code: [Select]set Path=%cd%
if not exist "%Path%\main-bg.bmp" GOTO Manual
Manual:
SET Path=
SET /P Path=Type the path where main-bg.bmp is and then press Enter
if exist "%Path%\main-bg.bmp" echo %path% >> %cd%Path.txt This checks if the original file is there, and if it is it makes a txt file with the right folder (in the same folder as the batch file, so this doesn't have to be the same folder as main-bg.bmp), I wanted to use this to be able to run the uninstall without having to set the path again. So the uninstall will have to read the .txt and use that to determine at what path it should be.

My complete program is way to big to post because of a lot of echo's to make it easier to use. I hope this post made it clearer what i am trying to do, but I assure you it's not some sort of virus or such nasty stuff.What you do not seem to realise is that (put simply) there is a system variable used by Windows called %PATH% which stores a list of folders where executable programs and scripts may be, so that when you want to run say Regedit you don't have to type "C:\Windows\Regedit.exe", you can just type the name. If you alter the contents of %path% then many programs will not work therefore. In general to avoid odd behaviour and confusion people avoid creating variables which have the same name as system variables. It would be better to choose another name for example %mypath% or whatever.

There are at least two ways I know of to get a line of text from a file into a variable.

Previously save to file. Don't forget to use quotes around a path+filename which has spaces.

Code: [Select]echo %mypath%>"c:\path with spaces\mypath.txt"
(1) load from file using SET /P

SET /P normally gets ENTER-terminated input from STDIN (the keyboard); by using the < redirection SYMBOL it can be made to get a (one only, the first) CR/LF terminated line from a file.

Note: File should have only 1 line; if it has more, variable will be set to the FIRST line.
Code: [Select]set /p mypath=<"c:\path with spaces\mypath.txt"


(2) load from file using FOR

Precede set with an @ symbol to avoid echoing to the screen

Note: File should have only 1 line; if it has more, variable will be set to the LAST line.
Code: [Select]FOR /F "usebackq delims==" %%A IN ("c:\path with spaces\mypath.txt") DO @set mypath=%%A


Using EITHER of the above 2 methods, the quotes around the path\filename are optional if there are no spaces, but necessary if there are spaces.







Holy sh... this stuff works. Thank you so very very much.
OH man, if you'd only know how happy I am now
Well, case closed I'd say. Thanks again Quote

this stuff works.

Only tried and tested solutions carry the Salmon Trout label, unlike certain other brands You guys are overcomplicating things.

Set /p var=Quote from: Helpmeh on October 24, 2009, 01:54:54 PM
You guys are overcomplicating things.

Set /p var=<Filename.txt

That was the first of my 2 offered solutions. Didn't you notice? The solution using FOR is capable of further development e.g. if extraction of tokens is desired.


Discussion

No Comment Found