Insert a group of 5 files and one folder into every subfolder

Page 1 of 2 12 LastLast

  1. Posts : 232
    Win 10 Ver 1903
       #1

    Insert a group of 5 files and one folder into every subfolder


    For testing purposes, I need to be able to copy 5 files and 1 folder into every level of another folders-group. For example:

    The main folder (Call it Subject) contains several subfolders each of which could more subfolders down to the 3rd or 4th level
    I need a script that can recursively walk through the folder structure of "Subject:" and copy the contents of a folder named "Test" (which has 5 files and one subfolder) into each level of the folders in "Subject"

    I only need this for testing purposes to see if the added data will solve a problem.
    Basically xcopy test\*.* /s into each subfolder in Subject.

    Since the the "Test" folder contains a single subfolder of its own the whole thing needs to be done in the first pass so the process will not try to also add them to that folder as well.

    I know this is something that if I can just get 10 minutes to think i can probably work it out but if someone has the proper script to do this I would appreciate it.
      My Computer


  2. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    This becomes simple if you either
    1 Use a specific named list of target subfolders & run a separate xcopy command for each one
    or
    2 Move your Test folder to a different drive [temporarily]

    Denis
      My Computer


  3. Posts : 232
    Win 10 Ver 1903
    Thread Starter
       #3

    agreed but.
    it is what it is and the steps are just finding the right code for walking through the structure of a folder and copying files each time you go down one level.
    I can't specify the names of the subfolder because they vary
    the Top Level needs to get copies of the 5 files and one subfolder
    then I need to drop down to every level two subfolder and copy those same things to it
    then to subfolder-level 3 of every case where there is one and copy again
    then every case of a subfolder at level 4
    etc etc
    Thank God I only have to do this to maybe 20 examples to find out if the tests works.
    I did a couple by hand but it got tedious Very Quick
    I am not sure of what the statement would be in the script to designate each folder level and as i said, the names are all different.

    PS moving the test folder to a different drive is not a problem. The issues i keep hitting is how to designate each folder in a recursive walk of them so i know when to copy the extra files and folders to that folder
    Last edited by questorfla; 28 Oct 2019 at 15:10.
      My Computer


  4. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    1 Choice of command
    You can get a complete list of all your relevant target folders using a Dir /s command.
    You can copy your xcopy command into a batch file twenty or so times and tailor each line to include each of the target folder paths.

    or

    You could use a RoboCopy command and exclude the test folder using the /XD switch.

    2 Recursive behaviour - amended to allow your use on whole drives rather than my own use on folder paths within drives
    In either case [xcopy, RoboCopy] you would also need to set up a recursive batch file, for which I have attached a safe demo version. RecurseThroughFolders-Demo-ShortForm.bat
    - You need to set the variable called BaseFolder by putting in an appropriate drive letter [or, as I do, a folder path such as D:\SomeFolder\SomeSubfolder] in the line Set BaseFolder=D:
    - I have set a variable called Mode to the value 1 needed to recurse & act on folders [not files & folders] which is appropriate because your xcopy/robocopy command will, in each case, act on the source folder path to copy everything to the target folder path.
    - If this script is used on whole drives, only Mode value 1 can be used.
    - I had never intended to use this script on whole drives, only on specific folder paths. I have amended it so that your intended use can work.
    - As Berton points out below, folder-file paths always need to be checked before action is taken. This demo only takes the action of displaying an echo of the folder-file fullpath and no checks are done. In addition to checking fullpath length, folder-file permissions would also need to be checked but these are not relevant to my own use of the script so no provision has been made for either.
    - You will see an echo at the end of each loop
    Finished in Mode 1 with item# 1 - "S:\SomeFolder"
    Finished in Mode 1 with item# 2 - "S:\SomeFolder\SomeSubFolder"
    {gaps in the numbering sequence indicate that a Hiden or System folder-file had been found and skipped}
    Finished in Mode 1 with item# 4 - "S:\SomeFolder\SomeSubFolder\SomeSubFolder"
    etc

    - Instructions for converting the demo for real use are given within the batch file - remember that only Mode 1 can be used on whole drives.
    RecurseThroughFolders-Demo-ShortForm.bat
    Code:
    ::::: Recurse through folders to run a CommonProcedure sub-routine on each item
    ::::: - provision has been made for the special character & to be used in paths-filenames 
    ::::: - some other special characters can also make it through but I have only tested for &
    ::::: This was not written for use on whole drives but only on folder paths
    ::::: - It picks up Hidden & System folders-files
    ::::: - I have inserted an escape mechanism that detects and skips Hidden & System folders-files
    ::::: - This escape mechanism does not work for non-Hidden, non-System folders-files within Hidden & System folders-files
    ::::: - This escape mechanism limitation means that only Mode 1 can be used for whole drive use i.e. when BaseFolder is a drive not a folder path.  Using it for whole drives using other Mode values would necessitate robust folder-files permissions checking that are not required for my intended use of this batch file.
    ::::: Demo version :::::
    ::::: The demo just displays some commonly-used variable manipulations and takes no action, it's all just for show
    ::::: - The demo is therefore quite innocuous
    ::::: - It pauses at the end of processing each item passed by the For command so each outcome can be inspected
    ::::: - It also pauses at the end of all processing, after :EndRecurse
    ::::: - For real uses, clear the decks by deleting lines
    ::::: - - from the line after :GetOnWithIt
    ::::: - - to [including] the line Pause at end of each CommonProcedure during testing
    ::::: User-chosen initialisation :::::
    :: Choose a windows Title, the variable is then used to ease subsequent renaming
    Set ThisTitle=Batch file recursing demo [short form]
    :: Insert tailored BaseFolder value for each use
    Set BaseFolder=G:
    :: Set Mode to 0 [files only], 1 [folders only], 2 [folders and files] for each use
    :: I have only included setting a variable to manage this so that I never have to alter anything in the program flow of the batch file
    ::Set /a Mode=0
    ::::: Only Mode 1 can be used for whole drive use i.e. when BaseFolder is a drive not a folder path
    Set /a Mode=1
    ::Set /a Mode=2
    ::::: Initialisation :::::
    Prompt $g
    Title %ThisTitle%
    Set /a ItemsCount=0
    ::::: Core recursing command :::::
    :: This is the files-only mode
    If %Mode% EQU 0 For /R "%BaseFolder%" %%F IN (*.*) Do Call :CommonProcedure "%%F"
    :: This is the folders-only mode
    If %Mode% EQU 1 For /R "%BaseFolder%" %%F IN (.) Do Call :CommonProcedure "%%F"
    :: Folders-only mode passes \. at the end of each folder path so the full stop needs to be removed before any other processing
    :: This is the folders and files mode
    If %Mode% EQU 2 For /R "%BaseFolder%" %%F IN (*) Do Call :CommonProcedure "%%F"
    GoTo EndRecurse
    ::::: Subroutine - CommonProcedure :::::
    :CommonProcedure
    :: Increment the item count (For loop count)
    Set /a ItemsCount=%ItemsCount% + 1
    Set "ThisItem=%~1"
    :: Skip Hidden and System folders-files
    Set ThisItemAtts=%~a1
    If Not "%ThisItemAtts:h=%"=="%ThisItemAtts%" GoTo :EOF
    If Not "%ThisItemAtts:s=%"=="%ThisItemAtts%" GoTo :EOF
    :: Include this stage only for Mode 1 Folders-only
    If %Mode% NEQ 1 GoTo GetOnWithIt
    :: Folders-only mode passes \. at the end of each folder path so the full stop needs to be removed before any other processing
    :: To remove trailing character 
    Set "ThisItem=%ThisItem:~0,-1%"
    :GetOnWithIt
    echo Finished in Mode %Mode% with item# %ItemsCount% - "%ThisItem%"
    Pause at end of each CommonProcedure during testing
    GoTo :EOF
    ::::: Closing :::::
    :EndRecurse
    Pause at EndRecurse during testing


    3 Why are you doing this?
    You said that this job is a test. What is it a test of?
    - If you are going to be using the script repeatedly in the future [to, say, create a 'template' set of folders for filing purposes] then another advantage of the RoboCopy command would be that until you start using any of the newly-created folders their modified date would remain that of the source folder and this might prove to be useful in spotting where changes have been made in the subfolders.

    Denis
    Last edited by Try3; 28 Oct 2019 at 16:56.
      My Computer


  5. Posts : 14,022
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #5

    And don't forget about the maximum path length, I've run into it a few times.
    maximum path length at DuckDuckGo
      My Computers


  6. Posts : 1,621
    Windows 10 Home
       #6

    People are just as happy as they make up their minds to be. -- Abraham Lincoln
    Nugget #14 -- No one can walk backwards into the future. -- John Mason's book: You're Born An Original! Don't Die A Copy!
    google is our friend :)
      My Computer


  7. Posts : 232
    Win 10 Ver 1903
    Thread Starter
       #7

    it is a test of the view layout for a web folder. Ultimately i have to place the proper CSS and JS coding into a directory that affects the one folder in need of it. But currently, all i have is a set of files and folders that work as long as they exist in every folder where there are files or subfolders. When a user opens that webpage, oi need them to see the files/folders ins specific organization.
    This java code along with the other files seems to do a great job of organizing things but so far, i have not been able to get them to work form a central location. The website already has a folder for both JS code as well as a styles.css code but this specific index.php file i can't seem to rewrite such that it will use the locations that already exist.
    This is a VERY old website and the added code i found for it is also about the same age.
    Since we are also looking at using a version of SYNC.COM for the same purpose, I don't want to go to too much effort if no one even likes the output, (though i am sure the css code could be tweaked as well)

    This was all so thati cold put together maybe 5 to 10 samples sites such that the people here could see what the web contents MIGHT look like if we kept the original webcode instead of moving to SYNC.

    I have just been "bitten" too many times by "sync-type" products and i am not so sure the people here playing with it realize what CAN happen if the users make mistakes that gets Sync'ed across to every system they are on.

    PS: Thanks for the help. In trying to do what i needed i finally just manually copied the files to create 5 sets of website folders for a "showing". I appreciate the coding and definitely will add it to my accumulation of "ODD THINGS" that i might need again some day.

    I probably would have been better off looking for a web designer to help me adjust that index.php file so that the css and JS could go where it should be in the first place.
      My Computer


  8. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #8

    I have now finished fiddling with my post #4 [above] and its attached batch file.

    [I had ended up with two editing windows open and had made some amendments in one but with others in the other window so the whole thing was an incoherent mess]

    Denis
      My Computer


  9. Posts : 14,022
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #9

    RolandJS said:
    People are just as happy as they make up their minds to be. -- Abraham Lincoln
    Nugget #14 -- No one can walk backwards into the future. -- John Mason's book: You're Born An Original! Don't Die A Copy!
    google is our friend :)
    So are you saying I can't use that signature?
      My Computers


  10. Posts : 232
    Win 10 Ver 1903
    Thread Starter
       #10

    I am not sure there is an answer to this


    DisplayFiles.zipDisplayFiles.zipI tried to attach the set of files but not sure if i did it correct. Named DisplayFiles.zip
    this is the complete set of css and index.pho that has to be used to create the display window for the contents of the web folder. The problem right now being that the directory where i need to do this contains a folder with multiple subfolders and files in all of them.
    It is just one part of a website and is the only part that i need to display in this manner but I need all files and all subfolder levels in that area to display in the way that these files create. As I said, it works great as long as i put a copy of every file as well as the images folder in the root of every folder but I know that isn't supposed to be done hat way,
    i tried copying the JS code to the website JS folder as well the CSS code to the websites main CSS folder and changing index.php such that it referred to those locations when it was callen
    I still put a copy of index.php in the root of every folder in the "documents" website directory. No matter what i tried i could not get it to use the normal website locations for Java and CSS code.
    I am certain it is something I am missing but i can't see what it is. Which is a shame because the layout is great.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 05:18.
Find Us




Windows 10 Forums