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

Author Topic: Attribute to txt and back doesn't work in batch, please help me  (Read 3600 times)

0 Members and 1 Guest are viewing this topic.

Pimda

    Topic Starter


    Starter

    Attribute to txt and back doesn't work in batch, please help me
    « on: October 24, 2009, 08:50:16 AM »
    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.
    « Last Edit: October 24, 2009, 09:30:10 AM by Pimda »

    Salmon Trout

    • Guest
    Re: Attribute to txt and back doesn't work in batch, please help me
    « Reply #1 on: October 24, 2009, 10:17:34 AM »
    %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.


    Pimda

      Topic Starter


      Starter

      Re: Attribute to txt and back doesn't work in batch, please help me
      « Reply #2 on: October 24, 2009, 10:30:47 AM »
      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.

      Salmon Trout

      • Guest
      Re: Attribute to txt and back doesn't work in batch, please help me
      « Reply #3 on: October 24, 2009, 11:10:21 AM »
      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.







      « Last Edit: October 24, 2009, 11:44:58 AM by Salmon Trout »

      Pimda

        Topic Starter


        Starter

        Re: Attribute to txt and back doesn't work in batch, please help me
        « Reply #4 on: October 24, 2009, 12:17:06 PM »
        Holy sh... this stuff works. Thank you so very very much.
        Oh man, if you'd only know how happy I am now  ;D
        Well, case closed I'd say. Thanks again  ;)

        Salmon Trout

        • Guest
        Re: Attribute to txt and back doesn't work in batch, please help me
        « Reply #5 on: October 24, 2009, 12:33:47 PM »
        Quote
        this stuff works.

        Only tried and tested solutions carry the Salmon Trout label, unlike certain other brands  :)

        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: Attribute to txt and back doesn't work in batch, please help me
        « Reply #6 on: October 24, 2009, 01:54:54 PM »
        You guys are overcomplicating things.

        Set /p var=<Filename.txt
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        Salmon Trout

        • Guest
        Re: Attribute to txt and back doesn't work in batch, please help me
        « Reply #7 on: October 24, 2009, 01:59:52 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.