topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday December 11, 2024, 8:52 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: I need help creating a new Directory structure including files from a text file.  (Read 4006 times)

sheriff28

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 1
    • View Profile
    • Donate to Member
I need help in creating a batch file or similar that will make a Folder/Sub Folder structure and add the relative files to these new locations.

Below is a text file representing a small part of the structure that I wish to create.
The first comma delimiter represents the Folder and Sub Folders that I wish to create under a top level Folder called DATABASE DATA.
The second comma delimiter represents the name of the actual File.
The third comma delimiter represents the location of the files to import into the new Folder structure.

"File Samples\Type\Multi Page Scans,""milk cart.jpg"",""c:\users\fss\desktop\filecenter data\file samples\type\multi page scans\milk cart.jpg"""
"File Samples\Type\Multi Page Scans,""15_1.TIF"",""c:\users\fss\desktop\filecenter data\file samples\type\multi page scans\15_1.tif\15_1.tif"""
"File Samples\Type\Colour Scans,""montage.bmp"",""c:\users\fss\desktop\filecenter data\file samples\type\colour scans\montage.bmp"""
"File Samples\Type\Colour Scans,""NIAGRA.jpeg"",""c:\users\fss\desktop\filecenter data\file samples\type\colour scans\niagra.jpg"""
"File Samples\Type\Colour Scans,""Welcome Scan.jpg"",""c:\users\fss\desktop\filecenter data\file samples\type\colour scans\welcome scan.jpg"""
"File Samples\Type\Colour Scans,""SHOOTAA.TIF"",""c:\users\fss\desktop\filecenter data\file samples\type\colour scans\shootaa.tif"""
"File Samples\Type\Colour Scans,""jamesc.bmp"",""c:\users\fss\desktop\filecenter data\file samples\type\colour scans\jamesc.bmp"""
"File Samples\Type\Colour Scans,""teeth.jpg"",""c:\users\fss\desktop\filecenter data\file samples\type\colour scans\teeth.jpg"""
"File Samples\Type\MS Office Documents,""KUNDI  DAN 1.docx"",""c:\users\fss\desktop\filecenter data\file samples\type\ms office documents\kundi  dan 1.docx"""
"File Samples\Type\MS Office Documents,""Save & Invest.doc"",""c:\users\fss\desktop\filecenter data\file samples\type\ms office documents\save & invest.rtf"""
"File Samples\Type\MS Office Documents,""2003.05.01 Scanning Index.csv"",""c:\users\fss\desktop\filecenter data\file samples\type\ms office documents\2003.05.01 scanning index.csv"""
"File Samples\Type\MS Office Documents,""ec customer breakdown.xls"",""c:\users\fss\desktop\filecenter data\file samples\type\ms office documents\ec customer breakdown.xlsx"""
« Last Edit: July 18, 2016, 12:49 AM by sheriff28, Reason: Missing file details »

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,939
    • View Profile
    • Donate to Member
Besides a missing attachment, this request does sounds like homework to me...anyway, the following links show you examples and ideas about the use of strings/arrays in Windows batch scripts:
Create Lists or arrays in Windows batch (SuperUser website (StackOverflow))
How to use array in Windows batch programming
Using arrays in batch files (Rob van der Woude website, a good resource for batch script creation)
Creating array into batch (example script)
Arrays, structures and linked lists in batch files (dostips website, another good resource for batch script creation)
Arrays, linked lists and other data structures in cmd.exe (another StackOverflow page explaining do's and don'ts)

These links should get you going :)

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
and have a look in the coding snacks, things like this have been done several times already

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
I think it would be a lot easier to do it using Powershell, look at the commandlets:
Import-Csv  (read the CSV file)
New-Item     (create folder structure)
Copy-Item    (copy file to destination or use Move-Item)

This Stack Overflow post pretty much answers how to do it in Powershell:
http://stackoverflow...v-file-in-powershell

To get you started, this will create a folder structure using the info in the CSV:
Code: PowerShell [Select]
  1. $path = "k:\test.csv"
  2. Import-Csv -path $path | ForEach-Object {
  3.   foreach($property in $_.PSObject.Properties) {
  4.     $stuff = ($property.Value).Split(",")
  5.     New-Item -Path ("K:\DATABASE DATA\" + $stuff[0]) -ItemType Directory | Out-Null
  6.   }
  7. }

2016-07-19 11_15_25.pngI need help creating a new Directory structure including files from a text file.

No error checking or pre-existing path checking.

The second item in the CSV isn't needed, you only need to copy/move the original file (third item) to the new structure.
« Last Edit: July 18, 2016, 08:14 PM by 4wd »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
4wd's Power Shell script looks like it has the problem covered.  For general purpose directory creation with a long parent directory chain I have a utility on my page CreateDir

It has a /q switch; quiet mode for use in batch files.  It is written in AutoIt3 that has a DirCreate() function that creates any missing folders in the chain when creating the target folder, without any programmer intervention needed.  See the included Readme.txt file for examples.