looking inside multiple subdirectories and copying files

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
joe.loduca
Newbie
Posts: 2
Joined: Wed Aug 08, 2018 9:57 am

looking inside multiple subdirectories and copying files

Post by joe.loduca » Wed Aug 08, 2018 11:08 am

Trying to set up a script to go within a most recent subdirectory then within that latest directory, I would find multiple directories each containing a bunch of csv files that I ultimately want to copy over to 1 directory. Having trouble scripting the process of managing all these multiple subdirectories. I'm thinking I need to set up an array of subdirectories, choose one of interest. Go there and then get a new list of directories within that latest subdirectory. The look inside each of these directories and copy any csv files found there to one
centralized subdirectory. I have a network app that creates network data within monthly subdirectories based on the date. For example, I might see the following directories:

C:\reports\20180601 (for june as there is a 06 in directory name for 6th month)
C:\reports\20180701 (july)
C:\reports\20180801 (august)

so being that this is the month of august, I want a script to choose the latest "C:\reports\20180801" subdirectory and look inside it. Inside this directory should be about 40 to 80 subdirectories. The names of these directories are named from the network devices I have. Each of these directories might contain anywhere from 1 to 4 csv files. I want to get ALL csv files in all sub directories within C:\reports\20180801\<multiple subdirectories> to be copied to 1 centralized directory - maybe call it C:\myCSVfiles or something like that.

so I have:

C:\reports\20180801
|_ Network device 1
|_ Dev1report1.csv
|_ Dev1report2.csv
|_ Network device 2
|_ Dev2report1.csv
|_ Network device 3
|_ Dev3report1.csv
|_ Dev3report2.csv
|_ Dev3report3.csv
|_ Dev3report4.csv

in the above example, I would want all the csv files to get copied over to C:\myCSVfiles.. should see in that directory:

Dev1report1.csv
Dev1report2.csv
Dev2report1.csv
Dev3report1.csv
Dev3report2.csv
Dev3report3.csv
Dev3report4.csv

User avatar
PepsiHog
Automation Wizard
Posts: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: looking inside multiple subdirectories and copying files

Post by PepsiHog » Thu Aug 09, 2018 1:54 pm

Hello,

I've done this before. I mean managing folders in folders. What I did is get a list of folders each time you change into a folder. Then use the While command and loop through the list of folders.

I will write some code and post it for you. But for now maybe this gives you the idea.

I didn't read what you wrote thoroughly, I see you said the same thing. Well this is what I have written. It is a little rough, but it may be helpful.

Code: Select all

OnEvent>Key_Down,VK113,0,DoExit

let>StartFolder=C:\Program Files (x86)
let>Ext=txt

// Get list of csv files.
//GetFileList>%StartFolder%\*.%Ext%,FL



Dialog>Dialog1
object Dialog1: TForm
  Left = 587
  Top = 237
  Width = 492
  Height = 746
  HelpContext = 5000
  AutoScroll = True
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object MSMemo1: tMSMemo
    Left = 0
    Top = 0
    Width = 476
    Height = 708
    AutoSize = False
    AlignBottom = True
    ScrollBars = ssBoth
    Align = alClient
    TabOrder = 0
  end
end
EndDialog>Dialog1


Show>Dialog1


// Get list of folders.
let>GFL_Type=1
GetFileList>%StartFolder%\*.*,FolderList
Separate>FolderList,;,Folder

StringReplace>FolderList,;,crlf,FLDisplay
SetDialogProperty>Dialog1,MSMemo1,Text,%FLDisplay%

let>asd=0

While>asd<%Folder_count%
let>asd=%asd%+1
let>Temp=Folder_%asd%
// Get list of all folders in current folder.
GetFileList>%Temp%\*.*,FL
// Add list of folders to FolderList.
if>FL>{""}
ConCat>FolderList,FL
ConCat>FolderList,{";"}
endif
// Get count of all folders.
Separate>FolderList,;,Folder
StringReplace>FolderList,;,crlf,FLDisplay
SetDialogProperty>Dialog1,MSMemo1,Text,%FLDisplay%
if>asd=10000
mdl>Ready.
   wait>120
   exit
endif
// Create the slightest hesitation 
// so macro can be easily stopped.
wait>.00000000000000000000000000001
EndWhile

mdl>%FolderList%

exit

ConCat>FileList,FolderList
ConCat>FileList,{";"}


srt>DoExit
  exit
END>DoExit
exit
Last edited by PepsiHog on Thu Aug 09, 2018 4:39 pm, edited 1 time in total.
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

joe.loduca
Newbie
Posts: 2
Joined: Wed Aug 08, 2018 9:57 am

Re: looking inside multiple subdirectories and copying files

Post by joe.loduca » Thu Aug 09, 2018 2:50 pm

Thanks Pepsihog,

I have pulled down some sample scripts that appear to come pretty close to what I need, but I was getting errors on just the samples with no editing what-so-ever..

I know a few programming languages too, in fact I have a liberty basic program (working) that does all the file management correctly. it works the latest files according to the present date. its perfect.. The only problem I have is liberty basic will no work on the servers because of the operating system used there.. I wrote everything on a win7 device, texted it, and tried to push it up to the servers.. The servers dont like me because of that.

User avatar
PepsiHog
Automation Wizard
Posts: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: looking inside multiple subdirectories and copying files

Post by PepsiHog » Thu Aug 09, 2018 4:47 pm

Ok. Well I posted my code so far. It needs work. But it works well enough that you can see the idea. At the top of the script you set the folder location to start at, and the extension you are looking for.

I have to go do stuffs, but if you need any more help, let me know.
(If you tried pushing stuff up me, I wouldn't like you either!) :D

Good Luck.
Last edited by PepsiHog on Wed Jan 23, 2019 5:22 pm, edited 1 time in total.
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

nodochau
Pro Scripter
Posts: 131
Joined: Wed Jan 16, 2019 12:59 pm

Re: looking inside multiple subdirectories and copying files

Post by nodochau » Mon Jan 21, 2019 7:38 pm

Hi PepsiHog,
I come across your code and it is what I am looking for.
I edited the folder and the file extension as I want to look for but the scripts seem like run never end.
My problem is how to get the file in a folder within sub folders by input the file name only.
Thanks

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: looking inside multiple subdirectories and copying files

Post by Grovkillen » Mon Jan 21, 2019 8:59 pm

Try debugging before running it by it self...
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
PepsiHog
Automation Wizard
Posts: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: looking inside multiple subdirectories and copying files

Post by PepsiHog » Wed Jan 23, 2019 5:14 pm

Hi,

You will need to add an if statement for what file you are looking for.

Insert this directly above ==> // Create the slightest hesitation

Code: Select all

let>GFL_Type=0
GetFileList>%StartFolder%\*.*,FileList
position>namelookingforhere,FileList,1,res
if>res>0
    // Create a subroutine with what to do when file is found.
     gosub>DoExit
    // GoSuB>whatyounamedyoursubroutine
endif
You need to replace "namelookingforhere" with the filename you are searching for.

There are users on this forum that will write a macro for you at a cost.

I do not mind helping, but you need to do some of the programming.

Good Luck,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts