copy data and dependencies to a target directory using a control file

Hi@all

i have some problems to find a good way to handle this, maby someone have

Short and simple description:

I have 2 folders. A destination folder and a source folder.

The main task is to copy interactively selectable files (via out-GridView) from one folder to another, but with some dependencies.
There is a source folder structure and a target folder structure which are structured in the same way.

With a configuration file, folders and files are made dependent. There is always a leading folder and dependent folders for a folder.

There are one or more leading folders (source) from which data can be selected via Out-GridView. There are more dependent folders in the source per leading folder from which the user cannot select anything.

All folders in the source that are dependent on a leading folder contain files with the same name but different extensions.

If the user has selected files from the leading folder using Out-GridView, all files with the same name but different extensions should also be copied from the source to the target folders .
The structure of the source folder is the same as that of the destination folder. If a folder does not exist, it will be created automatically.

In order to make only certain files available for selection, a control file is used for this task, in which the following parameters are stored per line. A Configuration file can have leading folders with dependencies but leading folder / dependencies are stored allwas per line. New line means different folder wit other dependancies

Struktur/Pattern:

<Main Folder name\File extension> <second dependent folder File extension with condition MUST or Can be copied> <further dependent folder File extension with condition MUST or Can be copied>

Folders Source (Example):

Under c:\test

  • SRC (Leading)
    • abc.c
    • efc.c
    • xxx.c
    • 123.txt
    • ...
  • DLL (depending on SRC)
    • abc.dll
    • efc.dll
    • xxx.dll
    • 456.dat
    • ...
  • JPG (depending on SRC)
    • abc.jpg
    • efc.jpg
    • xxx.jpg
    • 891.exe
  • WRD (Leading)
    • 111.docx
    • 222.docx
    • 333.docx
    • 888.txt
    • ...
  • EXC (depending on WRD)
    • 111.exe
    • 222.exe
    • 333.exe
    • test.txt
    • ...
  • VISIO (depending on WRD
    • 111.vsdx
    • 222.vsdx
    • 333.vsdx
    • 777.c
    • ...
 

Folders Target(Example):

Under E:\test

  • SRC
    • abc.c
    • efc.c
    • xxx.c
  • DLL
    • abc.dll
    • efc.dll
    • xxx.dll
  • JPG
    • abc.jpg
    • efc.jpg
    • xxx.jpg
  • WRD
    • 111.docx
    • 222.docx
    • 333.docx
  • EXC
    • 111.exe
    • 222.exe
    • 333.exe
  • VISIO
    • 111.vsdx
    • 222.vsdx
    • 333.vsdx
Example configfile:
Leading Foldername\Extension Dependent Folder 1 Musst or Can be copied Dependent Folder X Musst or can be copied
src\*.c dll\.dll M jpg\jpg K
wrd\*.docx exc\.xslx K visio\.vsdx M
.......many more entries per line

Only the files from the leading folder are selectable. All dependent files are copied to the target folder in the background automatically

Script:

First line of the config file processing

The script opens now the path to the first entry “Src” and get all items from this folder with the *.c file extension. Out-Gridview lets the user select some files if files were found with the extension (from src with *.c). The user select file abc.c efg.c and xxx.c and klick on ok.

The scrip now search for all files in the folders “dll” AND “jpg” and get all items from this folder with the same names of the pre selected files (abc efg and xxx ) but now with the .dll and .jpg extension (abc.dll efg.dll, xxx.dll,abc.jpg, efg.jpg, xxx.jpg)and copied this files and the leading files (abc.c efg.c and xxx.c ) directly to the target folder (src and dll and jpg folders) without further interaction. The user can only select files from the leading Folder.

Must or can means only if the copy job failes and the config file have for this file type a “M” then the script should stop, if “C” then the issue sould be logt but the script should continue running.

The script works through the configuration file lines.
Per line the above described procedure is repeated.

I am not sure how to structure the config file and also i have right now not realy a idee how to code the script .

So, the main problem is how to define the config file (json? xml? or with delimiter? working with arrays?)

Thanks for any idea

 

For the config I’d go probably go with csv since it appears to be flat objects (data is not hierarchical) and can be easily imported with Import-Csv. Do you have any code? I think this would be basic psuedocode to start:

#get config and assign to variable
$configpath = "C:\yourconfig.csv"
$config = Import-Csv -Path $configpath

#display grid and get selection
$defaultpath = "."
$selection = Get-ChildItem -Path $defaultpath -File | 
                Out-GridView -OutputMode Multiple

#iterate over each file selected
foreach ($item in $selection) 
{
    #search for corresponding item in config and take appropriate actions

} #foreach

 

{
  "1": {
    "Folder": "SRC",
    "Pattern": "*.c",
    "Dependencies": [
        {"Folder": "DLL","Extension": ".dll","Required": "M"},
        {"Folder": "JPG","Extension": ".jpg","Required": "C"}
      ]
    },
    "2": {
    "Folder": "WRD",
    "Pattern": "*.DOCX",
    "Dependencies": [
        {"Folder": "EXC","Extension": ".xslx","Required": "M"},
        {"Folder": "VISIO","Extension": ".vsdx","Required": "C"}
      ]
    }
   }

Maby in this way (Json)?

I think with a CSV it is a bit to difficult to handly the dependencies.

I need to get at first the content of the folder from the first layer of the json, for example:

Pseudo:

get-Childitem “Folder”: “SRC” / “Pattern”: “*.c” |out-gridview -passthrough

user selects this files via Out-GridView

  • abc.c
  • efc.c
  • xxx.c
Now....the script musst find all files in the depending folder DLL and JPG with the same names as the user selected files...abc.dll, efc.dll,xxx.dll,abc.jpg,efs.jpg,xxx.jpg and copy it to the destination folder

sounds simple…but isn’t :slight_smile:

 

 

JSON would be fine.

#get config and assign to variable
$configpath = "C:\yourconfig.json"
$config = Get-Content -Path $configpath -Raw | ConvertFrom-Json