Let's say i have 10 shortcuts in my d drive which leads to different locations of the PC
All these shortcuts are in one folder
I need a bat file to copy the contents inside each shortcut file to be copied to E drive within a real folder ( not shortcut)
In short i need to backup the files in the shortcuts but i want them in real folders just like the 10 shortcuts on d drive
Thanks
I need help making a bat file for copying multiple files from multiple folders
Moderator: DosItHelp
Re: I need help making a bat file for copying multiple files from multiple folders
You can't read the target out of a shortcut using pure batch. You could use a hybrid script though.
Save with extension .bat and customize variables lnk_path and dest_path.
Steffen
Save with extension .bat and customize variables lnk_path and dest_path.
Code: Select all
@if (@a)==(@b) @end /*
:: Batch
@echo off &setlocal
set "lnk_path=D:\folder\with\your\shortcuts"
set "dest_path=E:\destination\for\your\copied\contents"
for %%i in ("%lnk_path%\*.lnk") do (
for /f "delims=" %%j in ('cscript //nologo //e:jscript "%~fs0" "%%~fsi"') do (
echo Target of "%%~ni" is "%%~j".
if "%%~aj" geq "d" (
>nul robocopy "%%~j" "%dest_path%\%%~nxj" /e /r:1 /w:1
) else if "%%~aj" geq "-" (
>nul copy "%%~j" "%dest_path%\"
)
)
)
pause
:: JScript
goto :eof */ WScript.StdOut.WriteLine(WScript.CreateObject('WScript.Shell').CreateShortcut(WScript.Arguments(0)).TargetPath);
Re: I need help making a bat file for copying multiple files from multiple folders
Thank you very much
But may I ask you all to interpret what you have written here?
I do understand the set lnk path and dest path part
The rest, I'll need a bit of help
Thank you again
But may I ask you all to interpret what you have written here?
I do understand the set lnk path and dest path part
The rest, I'll need a bit of help
Thank you again
Re: I need help making a bat file for copying multiple files from multiple folders
Code: Select all
@if (@a)==(@b) @end /* REM Valid line in both Batch and JScript, begins a JScript comment block using slash and asterisk
:: Batch
@echo off &setlocal
set "lnk_path=D:\folder\with\your\shortcuts"
set "dest_path=E:\destination\for\your\copied\contents"
REM Find all files with extension .lnk (shortcut) in the specified folder and assign them one by one to %%i.
for %%i in ("%lnk_path%\*.lnk") do (
REM Run this script again in cscript.exe as JScript for every found .lnk file and pass the .lnk file as argument.
REM The JScript writes the found target path to the output stream that is processed in the FOR /F loop and assigned to %%j.
for /f "delims=" %%j in ('cscript //nologo //e:jscript "%~fs0" "%%~fsi"') do (
REM Just to display what was found...
echo Target of "%%~ni" is "%%~j".
REM If the target is a directory ...
if "%%~aj" geq "d" (
REM ... use ROBOCOPY to copy it.
>nul robocopy "%%~j" "%dest_path%\%%~nxj" /e /r:1 /w:1
REM Else, if the target is a file ...
) else if "%%~aj" geq "-" (
REM ... use COPY to copy it.
>nul copy "%%~j" "%dest_path%\"
)
)
)
pause
:: JScript
REM GOTO :EOF is still Batch and ends the script before it reaches the JScript portion.
REM The asterisk and slash ends the JScript comment block and thus, the rest of the line is processed if the script
REM is executed in cscript.exe.
REM The JScript uses objects, methods and properties provided in the WSH to determine the target path of the passed
REM shortcut file and writes it to the standard output stream.
goto :eof */ WScript.StdOut.WriteLine(WScript.CreateObject('WScript.Shell').CreateShortcut(WScript.Arguments(0)).TargetPath);
If you are intersted in how the JScript computes the target path see https://msdn.microsoft.com/en-us/subscr ... s.84).aspx and linked WshShortcut Object.
Steffen