Home / Microsoft / Microsoft DOS / Help with a .bat file
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: Help with a .bat file  (Read 1072 times)
rjn239
Topic Starter
Newbie



Posts: 2


« 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
IP logged
fireballs
Apprentice



Thanked: 3
Posts: 562

Code:Terminal

« 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
IP logged

Next time google it.
rjn239
Topic Starter
Newbie



Posts: 2


« 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
IP logged
fireballs
Apprentice



Thanked: 3
Posts: 562

Code:Terminal

« 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
IP logged

Next time google it.
ALAN_BR
Hopeful



Thanked: 5
Posts: 346


« 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
IP logged

Acer Laptop TravelMate 244LM. 1.25 GB Ram + 160 GB HDD. C:\ = 20 GB.
Windows XP Home Edition with SP3; Acronis T.I. 11.0 (build*8,101);
Protection ESET NOD32 v 2.7 Antivirus plus Comodo Firewall 3.5.
PerfectDisk 2008 v 9.0.0.64; Partition Wizard
EasyBcd Dual Boot, Default XP, W7 if I really have to
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« 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)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
fireballs
Apprentice



Thanked: 3
Posts: 562

Code:Terminal

« 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
IP logged

Next time google it.
devcom
Apprentice



Thanked: 37
Posts: 521


« Reply #7 on: August 29, 2008, 05:43:58 AM »

try for /? in cmd and this is at bottom
IP logged

Download: Choice.exe
Pages: [1] - (Top) Print 
Home / Microsoft / Microsoft DOS / Help with a .bat file « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.102 seconds with 20 queries.