Batch file to move certain file to other disk creating links to original files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSuser2018
Posts: 1
Joined: 28 Sep 2018 06:19

Batch file to move certain file to other disk creating links to original files

#1 Post by DOSuser2018 » 28 Sep 2018 06:51

Hi to all,
I would like to create a .bat file that moves certain files (based on their extension) from a folder (with subfolders) to another disk preserving original tree structure while creating a .lnk in the original location to the moved files.

For example the .bat would behave like this:

File extension: mkv
Source path: d:\movies
Destination path e:\movies

The batch scans folder and subfolders and moves all the .mkv files to the new path, while creating a .lnk in the original source folder for each file that has been moved.

I found some code that I adapted to my purpose:

Code: Select all

@echo off
cls

setlocal enabledelayedexpansion

set SOURCE_DIR=K:\source
set DEST_DIR=K:\dest

set FILENAMES_TO_COPY=*.txt


for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do (
    if exist "%%F" (

	echo source: %%F

        set FILE_DIR=%%~dpF
        set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=!
	set FILE_NAME_EXT=%%~nxF

	set FILE_DIR
	set FILE_INTERMEDIATE_DIR
	set FILE_NAME_EXT

        xcopy /S /I /Y /V "%%F" "%DEST_DIR%!FILE_INTERMEDIATE_DIR!"

	echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR%FILE_NAME_EXT!"

pause

    )
)
In this first attempt, I just copy the files, and it works.

I want to check the destination (which I'll use as parameter for mklink) but the last echo

echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR%FILE_NAME_EXT!"

This one gives me
destination "K:\dest"

I also tried

echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR!FILE_NAME_EXT!"
This one give me
destination "K:\dest\FILE_NAME_EXT"

Not what I would like to have, ie the full destination path\filename.extension
I guess I'm just missing something trivial...

Any ideas would be highly appreciated.

Thank you very much,

Matt

kwsiebert
Posts: 43
Joined: 20 Jan 2016 15:46

Re: Batch file to move certain file to other disk creating links to original files

#2 Post by kwsiebert » 01 Oct 2018 10:00

You have an unpaired % in the echo statement, which should be an ! to begin with, like so:

Code: Select all

echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR!!FILE_NAME_EXT!"

Post Reply