Sync from a list file with

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AOK
Posts: 4
Joined: 14 Jan 2020 12:57

Sync from a list file with

#1 Post by AOK » 14 Jan 2020 13:38

My sys info from the bat is in the end. Sooo there was a similar topic, but not quite.

My task is:

I have a single %SourceDir% with many if not hundreds of files with unique names. All in the same folder.
I have the %DestDir%/%SubFolder1%/%SubFolder2%/%SubFolder3%/%SubFolder4%/(up to 4 subfolders deep)

I have to write a batch file that would read from the file_list.txt (which basically would be dir/b/s output from another version of that same %DestDir% and create the corresponding paths and copy the corresponding files within.

PLS advice.

Long ago I made something much simpler, which back then worked, but is nothing with the current task at hand :-)

Code: Select all


@echo off
set src_folder=T:\TEST\source
set dst_folder=T:\TEST\destination
set file_list=T:\TEST\file_list.txt
//some way to read the path when the loop begins and before copying the line with the file and create it "if not exist"
//set pathfromlist=%DestDir%/%SubFolder1%/%SubFolder2%/%SubFolder3%/
//PLS advice
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
    xcopy "%src_folder%\%%f" "%dst_folder%\"
)


Thanks in advance. :-)

Here's my info,even though imho @ the moment is irrelevant.

Code: Select all

 INFO.BAT version 1.5
--------------------------------------------------------------------------------
Windows version        :  Microsoft Windows [Version 10.0.18362.535]
Product name           :  Windows 10 Pro Education, 64 bit
Performance indicators :  Processor Cores: 8      Visible RAM: 67053976 kilobytes

Date/Time format       :  (mm/dd/yy)  Tue 01/14/2020  20:59:41.10
__APPDIR__             :  C:\WINDOWS\System32\
ComSpec                :  C:\WINDOWS\System32\cmd.exe
PathExt                :  .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Extensions             :  system: Enabled   user: Disabled
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  en-US       Code Pages: OEM  866    ANSI 1251
DIR  format            :  01/10/2020  03:54 PM    34,359,738,368 pagefile.sys
Permissions            :  Elevated Admin=No, Admin group=Yes

                          Missing from the tool collection:  debug

Squashman
Expert
Posts: 4197
Joined: 23 Dec 2011 13:59

Re: Sync from a list file with

#2 Post by Squashman » 14 Jan 2020 15:44

The DIR /B /S command would include a full path in the output. In the script you are appending that information to the source folder. Xcopy would fail because the input path is invalid.

Eureka!
Posts: 102
Joined: 25 Jul 2019 18:25

Re: Sync from a list file with

#3 Post by Eureka! » 14 Jan 2020 16:13

Does this work for you?
(I'm not entirely sure what you want as you left important information out, like the exact structure of file_list.txt )

Replace the xcopy line with:

Code: Select all

    if exist "%src_folder%\%%~nxf" echo xcopy "%src_folder%\%%~nxf" "%%f"
(remove the "echo" when everything is OK)

AOK
Posts: 4
Joined: 14 Jan 2020 12:57

Re: Sync from a list file with

#4 Post by AOK » 20 Jan 2020 09:34

Thanks for the response, guys.

Maybe I have to try to explain it again. :-)

I want everything within a specific folder with subfolders with the files %TEMPLATEFolder% to be scanned as a list.txt. And later to be able to reproduce the same folder structure in a new place %DestFolder% as well. This time taking all the files from a single folder %SrcFolder% lacking the folder structure of the TEMPLATEFolder, but having all the files with their original names from it.

So my current starting point was to dir/b/s an old version of the %TEMPLATEFolder% in order later when I have all the files but in the same folder (%SrcFolder% - no subfolders!) to make the computer to arrange them according to the dir/b/s list.txt each file in its appropriate subfolder of the %TEMPLATEFolder%.


TEMPLATEFolder/Folder-1/File-01.txt
TEMPLATEFolder/Folder-7/File-02.jpg
TEMPLATEFolder/Folder-15/File-01.doc

%SrcFolder%/File-01.txt
%SrcFolder%/File-02.jpg
%SrcFolder%/File-01.doc

GOAL:
===
%DestFolder%/Folder-1/File-01.txt
%DestFolder%/Folder-7/File-02.jpg
%DestFolder%/Folder-15/File-01.doc

Is it clearer now? As I understand the %%~nxf would only strip the filenames which is to no help to me. I have all the files updated and changed. Way too many. I have them in a single folder. Need them correctly distributed in the original folders ;-)

AOK
Posts: 4
Joined: 14 Jan 2020 12:57

Re: Sync from a list file with

#5 Post by AOK » 20 Jan 2020 10:12

Eureka! wrote:
14 Jan 2020 16:13
Replace the xcopy line with:

Code: Select all

    if exist "%src_folder%\%%~nxf" echo xcopy "%src_folder%\%%~nxf" "%%f"
(remove the "echo" when everything is OK)
Actually it DOES with the ECHO! because of the piping.

According to this and this in order for XCOPY not to ask me file or folder I have to pipe D|XCOPY.

And in order for it to work I have to echo it. Which I am not sure if it is echo D into Xcopy... or what? But in no other scenario does it work without asking me for each file.

Here is the code:

Code: Select all

@echo off
cls
set src_folder=T:\TEST\source
set dst_folder=T:\TEST\destination
set file_list=T:\TEST\file_list.txt
for /f "delims=" %%f in (%file_list%) do (
if not exist "%dst_folder%" mkdir "%dst_folder%"
	if exist "%src_folder%\%%~nxf" echo D|xcopy "%src_folder%\%%~nxf" "%%f"
)

Eureka!
Posts: 102
Joined: 25 Jul 2019 18:25

Re: Sync from a list file with

#6 Post by Eureka! » 28 Jan 2020 15:42

While cleaning leftovers from my disks, found this.
Maybe it can help you a step further in the right direction.
(the last code you posted is a step in the *wrong* direction, btw)

Code: Select all

@echo off
setlocal enabledelayedexpansion

::-----------------------------------------------------------------
set TEMPLATE_FOLDER=T:\test\template
set TEMPLATE_FILELIST=T:\test\file_list.txt

set SRC_FOLDER=T:\test\source
set DEST_FOLDER=T:\test\dest
::-----------------------------------------------------------------


for /f "usebackq delims=" %%f in ("%TEMPLATE_FILELIST%") do (
  set TEMPLATEFILE=%%f
  set DESTFILE=!TEMPLATEFILE:%SRC_FOLDER%=%DEST_FOLDER%!
  if exist "%SRC_FOLDER%\%%~nxf" echo xcopy "%SRC_FOLDER%\%%~nxf" "!DESTFILE!"
)

AOK
Posts: 4
Joined: 14 Jan 2020 12:57

Re: Sync from a list file with

#7 Post by AOK » 05 Feb 2020 15:07

Eureka! wrote:
28 Jan 2020 15:42
While cleaning leftovers from my disks, found this.
Maybe it can help you a step further in the right direction.
(the last code you posted is a step in the *wrong* direction, btw)
Thanks but:

1/ I tried it - it did nothing. Tested it in a few variations and still can't understand the template part of your code.What's your idea?

2/ I DID found a mistake related to XCOPY switches - D should be F infont of the echo and added another /S /E. Haven't tested without them but with it does the job as desired.

Code: Select all

@echo off
cls
set src_folder=T:\TEST\source
set dst_folder=T:\TEST\destination
set file_list=T:\TEST\file_list.txt
@echo %dst_folder%
@echo ========
@echo %src_folder%
@echo ========
pause
for /f "delims=" %%f in (%file_list%) do (
if not exist "%dst_folder%" mkdir "%dst_folder%"
	if exist "%src_folder%\%%~nxf" echo F|xcopy /S/E "%src_folder%\%%~nxf" "%%f"
)

Post Reply