how to make a variable destination for COPY command
Hi guys, It seems that my title is very confusing. I dont know how to ask about it but let me explain. :) I created a batch file so that it will run automatically when I insert a usb drive. Here's the script cd \httrack rmdir tds.as /s/q httrack -r2 -i tds.as/kundeftp/tdstouch +*.pdf +mime:text/html copy tds.as\kundeftp\tdstouch\*.pdf e:\tds If you noticed on the 4th line, Im using the copy command. Now, I want the destination to be changed automatically when the USB drive is inserted to a computer. (the batch file is stored on the usb drive) Lets say, on the first computer, the usb drive is detected as E drive and on the second computer, it is detected as G drive. So, is there a way to make it as variable? Hope u guys understood my question... :) English is not my native language. I would appreciate if I can get a response right away. Thank you! Ed :) Ed
April 26th, 2011 8:56am

Hi guys, It seems that my title is very confusing. I dont know how to ask about it but let me explain. :) I created a batch file so that it will run automatically when I insert a usb drive. Here's the script cd \httrack rmdir tds.as /s/q httrack -r2 -i tds.as/kundeftp/tdstouch +*.pdf +mime:text/html copy tds.as\kundeftp\tdstouch\*.pdf e:\tds If you noticed on the 4th line, Im using the copy command. Now, I want the destination to be changed automatically when the USB drive is inserted to a computer. (the batch file is stored on the usb drive) Lets say, on the first computer, the usb drive is detected as E drive and on the second computer, it is detected as G drive. So, is there a way to make it as variable? Hope u guys understood my question... :) English is not my native language. I would appreciate if I can get a response right away. Thank you! Ed :) Ed %~dp0 is a variable to call out the path location of the batch file itself. So... there's your floating variable... Please mark as an answer if this resolves your questions. Example: @ECHO OFF ECHO. THIS BATCH FILE IS LOCATED AT %~dp0 PAUSE>NUL EXIT Move that batch file anywhere and run it... it should change when you move it and execute. Steve Kline Microsoft Certified IT Professional: Server Administrator Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7 Microsoft Certified Product Specialist & Network Product Specialist Red Hat Certified System Administrator This posting is "as is" without warranties and confers no rights.
Free Windows Admin Tool Kit Click here and download it now
April 26th, 2011 9:24am

Hi Steve, Thanks for your response. Im not good at this and this is the first time that I created a batch file.. :) is this what you mean? copy tds.as\kundeftp\tdstouch\*.pdf %~dp0:\td ?? :) im not sure.. Thanks buddy.. :) Ed
April 26th, 2011 9:38am

Hi Steve, Thanks for your response. Im not good at this and this is the first time that I created a batch file.. :) is this what you mean? copy tds.as\kundeftp\tdstouch\*.pdf %~dp0:\td ?? :) im not sure.. Thanks buddy.. :) Ed For example: When I insert the USB drive to computer number 1, it detected as G drive. So the script will automatically change to copy tds.as\kundeftp\tdstouch\*.pdf g:\tds and when I insert the usb drive to other computer, it detected as E drive. so the script will automatically change to copy tds.as\kundeftp\tdstouch\*.pdf e:\tds something like that. So, is that the variable? :) sorry if i have a lot questions.. I really appreciate you're helping me.. Thanks. Ed
Free Windows Admin Tool Kit Click here and download it now
April 26th, 2011 9:46am

Yes, exactly... if the batch file is at the root of the flash drive. It does not change the letter on the batch file itself... it's a windows variable. It's pertenant to the OS and the batch file being executed. Windows interprets %~dp0 as "batch file location" Also... note that with this batch file, you can also use %cd%... but this is inefficient if you use any CD lines to change the directory. You can use CD to change the directory and still use %~dp0 to reference the directory of the batch file itself. Example - Copying Drive:\Folder1\file.txt to the root with a batch file on the root of the drive called copyfile.bat. -- START FILE -- (Example Batch File) @echo offtitle filecopy.bat:PrepareSamplemkdir folder1echo Hello! > Folder1\file.txtif exist %~dp0\file.txt (goto Exists) else (goto NotExist) :Existsmsg %USERNAME% This file already exists!echo. Press any key to finish this script!pause>nulexit :NotExistcd Folder1copy /y file.txt %~dp0\file.txtstart explorer.exe %~dp0msg %USERNAME% This file has been copied to %~dp0! Have a nice day!EXIT --END FILE-- Best Regards, Steve Kline Microsoft Certified IT Professional: Server Administrator Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7 Microsoft Certified Product Specialist & Network Product Specialist Red Hat Certified System Administrator This posting is "as is" without warranties and confers no rights.
April 26th, 2011 3:20pm

Yes, exactly... if the batch file is at the root of the flash drive. It does not change the letter on the batch file itself... it's a windows variable. It's pertenant to the OS and the batch file being executed. Windows interprets %~dp0 as "batch file location" Also... note that with this batch file, you can also use %cd%... but this is inefficient if you use any CD lines to change the directory. You can use CD to change the directory and still use %~dp0 to reference the directory of the batch file itself. Example - Copying Drive:\Folder1\file.txt to the root with a batch file on the root of the drive called copyfile.bat. -- START FILE -- (Example Batch File) @echo offtitle filecopy.bat:PrepareSamplemkdir folder1echo Hello! > Folder1\file.txtif exist %~dp0\file.txt (goto Exists) else (goto NotExist) :Existsmsg %currentuser% This file already exists!echo. Press any key to finish this script!pause>nulexit :NotExistcd Folder1copy /y file.txt %~dp0\file.txtstart explorer.exe %~dp0msg %USERNAME% This file has been copied to %~dp0! Have a nice day!EXIT --END FILE-- Best Regards, Steve Kline Microsoft Certified IT Professional: Server Administrator Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7 Microsoft Certified Product Specialist & Network Product Specialist Red Hat Certified System Administrator This posting is "as is" without warranties and confers no rights.
Free Windows Admin Tool Kit Click here and download it now
April 26th, 2011 3:20pm

Did you have other concerns about batch file variables, Ed? Best Regards,Steve Kline Microsoft Certified IT Professional: Server Administrator Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7 Microsoft Certified Product Specialist & Network Product Specialist Red Hat Certified System Administrator This posting is "as is" without warranties and confers no rights.
April 27th, 2011 12:37pm

Hi steve, Thank for you all your help and for getting back to me.. :) been very busy at school for the last days.. :) yes, i still have concerns. still, im confused with your example.. : as what I have mentioned from my previous post, can I use this copy tds.as\kundeftp\tdstouch\*.pdf %~dp0:\td ?? instead of putting the drive letter path, jus put %~dp0:\ ? so sorry.. havent tried this before.. I really appreciate your patience on this.. Again, thank you so much Ed
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2011 6:29am

Hi Customer, %~dp0 output is g:\ , when your usb drive letter is g. So you should use command like "copy tds.as\kundeftp\tdstouch\*.pdf %~dp0td" without ":\". You could add command "echo %~dp0td" in your script to verify. Regards, Rick Tan
May 3rd, 2011 10:48pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics