Monday, September 29, 2014

cmd.exe - CMD or BAT to copy/replace files from a relative path to a .lnk target location



Example :




I will have the following :




  • Folder that contains :




    1. The CMD or BAT file

    2. the file/files needed to be copied ( Ex: file1.exe and file2.exe )



  • A .lnk located at the desktop ( Ex: C:\Users\Home\Desktop\Example.lnk) which is a shortcut for ( EX: D:\folder\Example.pdf )




I need the CMD/BAT file to copy file1.exe and file2.exe from its current relative location and paste or paste/replace them to the .lnk target location after reading it which is D:\folder\



Edit






I have tried the following to replace gravity.pdf with another version of gravity.pdf located at the same folder of the bat command :




@echo off
setlocal
rem get the .lnk target directory
for /f "tokens=* usebackq" %%i in (`type "C:\Users\Abdo\Desktop\Gravity.lnk ^| find "\" ^| findstr/b "[a-z][:][\\]"`) do (
set _targetdir=%%~dpi
)
rem copy the files
copy /y Gravity.pdf %_target%
endlocal



but an error comes "the syntax of the command is incorrect."



I am tring to understand the code , canot get how %_target% will refere to the target full path of the gravity.lnk which at my case now is D:\Books\



Edit 2







I have removed some inserted lines from the code and now an empty cmd black window opens but nothing changes :



code :



@echo off
setlocal
rem get the .lnk target directory
for /f "tokens=* usebackq" %%i in (`type "C:\Users\Abdo\Desktop\Gravity.lnk ^| find "\" ^| findstr/b "[a-z][:][\\]"`) do (set _targetdir=%%~dpi)
rem copy the files
copy /y Gravity.pdf %_target%

endlocal

Answer



How do I copy/replace files from a relative path to a .lnk target location?



Use the following batch file:



@echo off
setlocal
rem get the .lnk target directory

for /f "tokens=* usebackq" %%i in (`type "C:\Users\Home\Desktop\Example.lnk" ^| find "\" ^| findstr/b "[a-z][:][\\]"`) do (
set _targetdir=%%~dpi
)
rem copy the files
copy /y file1.exe %_targetdir%
copy /y file2.exe %_targetdir%
endlocal






My code gives an error "the syntax of the command is incorrect".




  • You are missing the " after lnk in the for command.


  • %_target% should be %_targetdir% (that was a mistake in my batch file - now fixed).




Here is the corrected version of your batch file:




@echo off
setlocal
rem get the .lnk target directory
for /f "tokens=* usebackq" %%i in (`type "C:\Users\Abdo\Desktop\Gravity.lnk" ^| find "\" ^| findstr/b "[a-z][:][\\]"`) do (
set _targetdir=%%~dpi
)
rem copy the files
copy /y Gravity.pdf %_targetdir%
endlocal






Further Reading




  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.

  • find - Search for a text string in a file & display all the lines where it is found.

  • findstr - Search for strings in files.

  • parameters - A command line argument (or parameter) is any value passed into a batch script.


  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.

  • type - Display the contents of one or more text files.

  • for /f - Loop command against the results of another command.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...