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

Author Topic: Help with a .bat file  (Read 3658 times)

0 Members and 1 Guest are viewing this topic.

rjn239

    Topic Starter


    Newbie

    Help with a .bat file
    « on: August 29, 2008, 02:59:36 AM »
    Hello,

    Using the notepad i have created a .bat file which when opened copies a certain file from a location to my memory stick.

    The command i have been using is:

    xcopy "C:\documents and settings\User\my documents\my work\october 2008" H:\Work

    The trouble is, i wanted this to run automatically so as soon as i plugged my memory stick into the computer it would run the .bat file, copying the specified file to my memory stick without me having to do anything.

    Also, i was wondering in the command above the username for my computer is User, but i wanted to use it in other computers of which the username is not User, is there anyway i can get the command to automatically change to the current user that is logged on so it would read something like this??

    xcopy "C:\documents and settings\"Current User"\my documents\my work\october 2008" H:\Work

    Or get it to automatically fill the username in so i don't have to keep opening the script up and changing it?

    I would appreciate your help,

    Thank you in advanced

    Ryan

    fireballs



      Apprentice

    • Code:Terminal
    • Thanked: 3
      Re: Help with a .bat file
      « Reply #1 on: August 29, 2008, 03:10:40 AM »
      Code: [Select]
      xcopy "C:\documents and settings\%username%\my documents\my work\october 2008" H:\Work
      That will work with any username. As for running when you put a stick in, i found this through google:

          * Open Notepad
          * Type in:
            [autorun]
            open=***.bat
            action=Run ***.bat
          * Save the file as autorun.inf
          * Put the file in the root of your USB flash drive
          * The next time you insert your removable drive into your PC, the specified program will autorun from the USB drive

      FB
      Next time google it.

      rjn239

        Topic Starter


        Newbie

        Re: Help with a .bat file
        « Reply #2 on: August 29, 2008, 03:23:58 AM »
        Thank you, the username command worked perfectly,

        the autorun one i did as you said below but then when i insert the memory stick it says 'Run copy work.bat using the program provided on the device' so i click ok and then it brings up the open with window and says 'choose the program you want to use to open this file' and has a list of my currently installed programs??

        Also can i just ask you another quick question i've just noticed the drive letter at the end of my command sometimes changes depending on which computer i put it in for example computer 1 it is H:\ whereas computer 2 it is E:\ because computer 1 has more harddrive partitions installed, so that messes up the xcopy command then, so like the username is there a command that can get the drive to automatically change depending on what it is in my computer? or is there a way i can automatically always dedicated drive R:\ for example to my memory stick no matter what computer i am on?

        Thank you for the quick reply and help

        Ryan

        fireballs



          Apprentice

        • Code:Terminal
        • Thanked: 3
          Re: Help with a .bat file
          « Reply #3 on: August 29, 2008, 03:39:00 AM »
          sorry i edited it before i posted it here cause i didn't realise it was important the INF file shoul read:

                action=Run ***.bat program

          i think that should work. If it doesn't work check out the original article here.

          You can use relative addresses if the batch file is located on the USB stick the just saying
          Code: [Select]
          xcopy "C:\documents and settings\%username%\my documents\my work\october 2008" %CD%\work
          FB
          Next time google it.

          ALAN_BR



            Hopeful

            Thanked: 5
            • Computer: Specs
            • Experience: Experienced
            • OS: Windows 7
            Re: Help with a .bat file
            « Reply #4 on: August 29, 2008, 04:51:18 AM »
            I feel another problem could arise

            You may not need this , BUT I suggest :-

            xcopy "%USERPROFILE%\my documents\my work\october 2008" %CD%\work

            The difference is that  %USERPROFILE%  generally expands to be the same as
            C:\documents and settings\%username%
            but not always

            The vital improvement is that it will not always aim at C:\
            but it will always aim at the System Drive

            The command SET will list variables.
            For most people, this will include items such as :-
            SystemDrive=C:
            SystemRoot=C:\WINDOWS
            They are fundamental, and they will always designate the drive used by the operating system.

            SET will show many other variables some of which have one or more reference to C:\, and these variable are (if defined by Windows on start-up - and not appended by the user) generally based upon whatever is firstly defined as SystemDrive and SystemRoot.
            This feature applies to %USERPROFILE%.

            Regards
            Alan

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Help with a .bat file
            « Reply #5 on: August 29, 2008, 05:25:00 AM »
            %cd% expands to the entire current path. If you only need the drive letter, try this method:

            Code: [Select]
            xcopy "%USERPROFILE%\my documents\my work\october 2008" %~d0\work

             8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            fireballs



              Apprentice

            • Code:Terminal
            • Thanked: 3
              Re: Help with a .bat file
              « Reply #6 on: August 29, 2008, 05:34:33 AM »
              what is %~d0? i've never seen it before and it just creates errors when i try it.

              FB
              Next time google it.

              devcom



                Apprentice

                Thanked: 37
                Re: Help with a .bat file
                « Reply #7 on: August 29, 2008, 05:43:58 AM »
                try for /? in cmd and this is at bottom
                Download: Choice.exe