Extracting subdirectories and files from parent directory, using these to create dirs /copy files.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Robba078
Posts: 2
Joined: 24 Jun 2018 07:33

Extracting subdirectories and files from parent directory, using these to create dirs /copy files.

#1 Post by Robba078 » 24 Jun 2018 07:39

am building a small automated deployment pipeline for an application. I am very new to batch files, created a few ones so far with success, but this particular one is driving me nuts.

What I want:

in an inputpath, there can be files and/or subdirectories with files. If these exist in the targetpath, then the the files in the targetpath need to be be renamed to file_OLD.extension.

When this is done the files from inputpath need to be copied there.

If the file, or sub directory with file not exists, the sub directory needs to be created and the file needs to be placed there.

What I have so far:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set inputpath=P:\Deployment\1.DeployToTST\Template
set targetpath=\\<servername>\Template

for /f %%a in ('dir "%inputpath%"/a-d/b/-p/s^|find /v ""') do (

if exist %targetpath%\%%a (
xcopy "%targetpath%\%%a" "%targetpath%/%%a_OLD" /Y
xcopy /S "%%a" "%targetpath%\%%a" /Y /I /K

) ELSE  (

xcopy /S "%%a" %targetpath\%%a%" /Y /I /K)

)
I feel that the logic is right (it is not rocket science after all), however I cant seem to get it working. as %%a seems to be taking the entire full file path:

Code: Select all

P:\Deployment\1.DeployToTST\Template\template1.ctx
P:\Deployment\1.DeployToTST\Template\template1\template1.ctx
I actually need for %%a to be:

Code: Select all

 template1.ctx
 template1\template1.ctx
file extensions differ, so do the subdirectories in my inputpath.

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

Re: Extracting subdirectories and files from parent directory, using these to create dirs /copy files.

#2 Post by aGerman » 24 Jun 2018 10:29

You need to replace inputpath with targetpath in the output of DIR /S in order to check the existence and do the renaming. The rest can be done with a subsequent ROBOCOPY call.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "inputpath=P:\Deployment\1.DeployToTST\Template"
set "targetpath=\\<servername>\Template"

for /f "delims=" %%i in ('dir "%inputpath%" /a-d /b /s') do (
  set "inpf=%%i"
  set "inpn=%%~dpni"
  setlocal EnableDelayedExpansion
  if exist "!inpf:%inputpath%=%targetpath%!" move /y "!inpf:%inputpath%=%targetpath%!" "!inpn:%inputpath%=%targetpath%!_OLD%%~xi"
  endlocal
)
robocopy "%inputpath%" "%targetpath%" /s /e /r:1 /w:1
Untested though. Not sure if IF EXIST and MOVE can handle network paths.

Steffen

Robba078
Posts: 2
Joined: 24 Jun 2018 07:33

Re: Extracting subdirectories and files from parent directory, using these to create dirs /copy files.

#3 Post by Robba078 » 25 Jun 2018 09:17

Hi aGerman,

Damm, that did it completely, thanks a lot!!!

I am not fully understanding what you did, but will look at documentation, as I also want to learn to do this completely by myself.

Thanks again, cheers!

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

Re: Extracting subdirectories and files from parent directory, using these to create dirs /copy files.

#4 Post by aGerman » 25 Jun 2018 09:26

If you want to read the documentation then I suggest to read it for SET in order to understand how the string replacement works and for FOR in order to learn more about modifiers beginning with a ~ sign. ROBOCOPY just creates folders and files that were not found in the target path.
If you still have questions then just ask.

Steffen

Post Reply