I need help making a bat file for copying multiple files from multiple folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Lioric
Posts: 2
Joined: 24 Dec 2017 12:56

I need help making a bat file for copying multiple files from multiple folders

#1 Post by Lioric » 24 Dec 2017 13:05

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I need help making a bat file for copying multiple files from multiple folders

#2 Post by aGerman » 24 Dec 2017 15:28

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.

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);
Steffen

Lioric
Posts: 2
Joined: 24 Dec 2017 12:56

Re: I need help making a bat file for copying multiple files from multiple folders

#3 Post by Lioric » 24 Dec 2017 18:45

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I need help making a bat file for copying multiple files from multiple folders

#4 Post by aGerman » 24 Dec 2017 20:15

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 need help for a certain command then execute it with /? appended. E.g. FOR /?
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

Post Reply