Example :
I will have the following :
Folder that contains :
- The CMD or BAT file
- 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
"
afterlnk
in thefor
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