Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts

    getting folder name to the files inside that folder

    Hello Everyone!

    I need to get the folder name for the files inside that folder. like this i have 400 folders. for example,

    folder 1 - maize d1-22220000eeee - this folder contains many file like area, factor, plantheight, 3ddata.
    folder 2 - maize d2-22220000fffffff - again this folder contains many file like area, factor, plantheight, 3ddata with the same name what i have in the previous folder.
    goes on till 400 folders...........

    now if i combine all these files from these different folders it asks me to replace since all the file names are same.Thus i need to include the folder name to the files inside the folder so that i can easily combine those files.

    required output:
    folder 1 - maize d1-22220000eeee - this folder contains many file like area_maize d1-22220000eeee, factor_maize d1-22220000eeee, plantheight_maize d1-22220000eeee, 3ddata_maize d1-22220000eeee.
    folder 2 - maize d2-22220000fffffff - again this folder contains many file like area_maize d2-22220000fffffff, factor_maize d2-22220000fffffff, plantheight_maize d2-22220000fffffff, 3ddata_maize d2-22220000fffffff.

    it would be really great if anyone suggest me how to do this using some dos cmd or in any other way...

    Thank you..

    Regards,
    Dharani.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,042
    Thanks
    195
    Thanked 755 Times in 691 Posts
    Dharani,

    Welcome to the Lounge as a new poster!

    This can be accomplished very easily with PowerShell.

    Are all 400 directories in the same tree? That is, are they like d:\Master\Folder1, d:\Master\Folder2, d\Master\Folder....?
    Do any of the numbered folders have sub folders?
    Also is the destination folder in d:\Master or is it located elsewhere?

    Exact source/destination folder paths would be very helpful in solving the problem. HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  4. The Following User Says Thank You to RetiredGeek For This Useful Post:

    dharani suresh (2014-04-24)

  5. #3
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by RetiredGeek View Post
    Dharani,

    Welcome to the Lounge as a new poster!

    This can be accomplished very easily with PowerShell.

    Exact source/destination folder paths would be very helpful in solving the problem. HTH
    Hello Sir,
    Very nice to hear from you like this... Thank you so much for your wonderful greeting... Thank you for spending your valuable time in helping me...

    Yes sir, as you told, the exact path of my folders is,
    ""C:\Users\dsuresh\Desktop\Maize_20140410_120440+0 530\Maize_D-1_1_Maize_924bcacc-b446-11e3-95ce-002590c40d8e\area3d""
    ""C:\Users\dsuresh\Desktop\Maize_20140410_120440+0 530\Maize_D-1_2_Maize_924c91a0-b446-11e3-95ce-002590c40d8e\area3d""
    my main folder will be ""Maize_20140410_120440+0530"" and inside of this folder i have many sub folders of 400 like ""Maize_D-1_1_Maize_924bcacc-b446-11e3-95ce-002590c40d8e, , Maize_D-1_2_Maize_924c91a0-b446-11e3-95ce-002590c40d8e"" inside which i have excel files with the same name.
    when i am trying to copy and paste all these excel files in one folder, it will ask me to replace that's y i need these excel files to get its sub folders name to differentiate.

    it would be really great, if you can help me in solving this problem... and i have heard about power shell sir.. it is a command line program right? something like DOS commands....

    looking forward for a positive reply..

    Thank you..

    Regards,
    Dharani.
    Last edited by dharani suresh; 2014-04-22 at 22:44.

  6. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,820
    Thanks
    19
    Thanked 108 Times in 102 Posts
    Here's what I have done and tested as a BATch file - I hope you are familiar with these?
    I'm afraid I couldn't cope with the complexity of your folder names and your file names, so I have simplified them hugely for my test.

    The top level folder I have called D:\JG\$top
    and underneath this I have folders $low0, $low1, $low2...$low9 (the BATch file finds these names out from the first DIR command)
    In each sub-folder I have files called ExcelFileA.xls, ExcelFileB.xls...ExcelFileZ.xls (the BATch file finds these names from the second DIR command)

    The combined folder I have called D:\JG\$combined and it will contain Excel files
    $low0ExcelFileA.xls
    $low0ExcelFileB.xls
    ...
    $low0ExcelFileZ.xls
    $low1ExcelFileA.xls
    $low1ExcelFileB.xls
    ...
    $low1ExcelFileZ.xls
    $low2ExcelFileA.xls
    and so on, after the BATch file has been run.

    Code:
    @echo off
    setlocal
    :: assume top level folder is D:\JG\$top 
    ::   with multiple folders beneath, all of which contain Excel spreadsheets
    
    :: further assume that all the files are to have their folder name prefixed, 
    ::   and are to be copied in that form to D:\JG\$combined 
    
    set combined=D:\JG\$combined
    set topfolder=d:\JG\$top
    :: *** change the two folder paths above to match your requirements ***
    
    for /f %%a in ('dir /ad /on /b "%topfolder%"') do call :processfolder "%%a"
    dir %combined% | more
    endlocal
    goto :eof
    
    ::----------
    :processfolder
    
    :: remove the double-quotes
    set folder=%~1
    echo Processing folder %folder%
    
    :: copy all the Excel files in %folder% to %combined%
    for /f %%a in ('dir /on /b "%topfolder%\%folder%\*.xls" ') do (
     copy "%topfolder%\%folder%\%%a" "%combined%\%folder%%%a"
    )
    
    goto :eof
    If you want the lists of files in all the folders I can provide them, but they are l o n g!
    Last edited by BATcher; 2014-04-23 at 04:39.
    BATcher

    If it wasn't for the weather, Great Britain would be a silent nation.

  7. The Following User Says Thank You to BATcher For This Useful Post:

    dharani suresh (2014-04-24)

  8. #5
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BATcher View Post
    Here's what I have done and tested as a BATch file - I hope you are familiar with these?
    I'm afraid I couldn't cope with the complexity of your folder names and your file names, so I have simplified them hugely for my test.

    The top level folder I have called D:\JG\$top
    and underneath this I have folders $low0, $low1, $low2...$low9 (the BATch file finds these names out from the first DIR command)
    In each sub-folder I have files called ExcelFileA.xls, ExcelFileB.xls...ExcelFileZ.xls (the BATch file finds these names from the second DIR command)

    The combined folder I have called D:\JG\$combined and it will contain Excel files
    $low0ExcelFileA.xls
    $low0ExcelFileB.xls
    .
    and so on, after the BATch file has been run.

    Code:
    @echo off
    setlocal
    goto :eof
    If you want the lists of files in all the folders I can provide them, but they are l o n g!
    Hello BATcher,
    Thank you so much for this wonderful cmds... Thank you for spending your valuable time in helping me...

    As you instructed me, i changed the path's name in the cmds in the place of set top folder and set combined. but still its telling that files not found.My top folder will containd sub folders along with csv files and my combined folder will be empty right....
    1) should i change the root directory also, which means should i change from c to d before pasting this code?
    2) should i change anywhere else the paths name?? ( i hope no)

    i opened the cmd prompt and i pasted these cmds by changing the paths name in two places... this is what exactly i did.. should i do anything else???

    Please guide me..

    Thank you...

    With regards,
    Dharani.

    PS.,
    Sorry these are all CSV files not exactly excel files.....

  9. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,042
    Thanks
    195
    Thanked 755 Times in 691 Posts
    dharani ,

    Sorry I took so long getting back to you.

    Here's the powershell version:
    Code:
    $DriveToCSVs = "G:\BEKDocs"  #Replace w/your source drive\path
    $DestDrivePath = "G:\Test"       #Replace w/your destination drive\path
    $BaseLen = $DriveToCSVs.length + 1
    $FileCnt = 0
    
    Get-ChildItem "$DriveToCSVs\*.csv" -Recurse | `
      Foreach-Object {
          $FileCnt += 1
          $NewFN = $_.FullName.Substring($BaseLen)
          $NewFN = $NewFN -replace '\\','_'
          Copy-Item  $_.FullName "$DestDrivePath\$NewFN"
      }
    
      Write-Host "$FileCnt Files Processed"
    To make this work you can do the following:
    Open the PowerShell ISE by clicking the ORB and typing Power into the search box and selecting Windows PowerShell ISE from the results.
    Copy & paste the code above into the Untitled.ps1 tab.
    Make the indicated changes for your drive/directories as indicated in the code.
    In the BLUE window type this command: Set-ExecutionPolicy RemoteSigned {enter key}
    Click the Yes button in the dialog box.
    Check your command by typing Get-ExecutionPolicy {enter key} it should return RemoteSigned.
    Click on the Green Arrow (circled in red below) to run the program.
    PSInst.JPG
    If you get any red lettering in the left hand window it means you made an error typing your drive\directory information.
    You can save the program for later use when you are done. Note the last line of output tells you how many files were processed.

    Note: Your destination folder must EXIST the program will NOT create it! Also the \ in the path names appended to the file names are replaced with underscores (_).

    HTH
    Last edited by RetiredGeek; 2014-04-23 at 14:01.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  10. The Following User Says Thank You to RetiredGeek For This Useful Post:

    dharani suresh (2014-04-24)

  11. #7
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by RetiredGeek View Post
    dharani ,

    Sorry I took so long getting back to you.

    Here's the powershell version:
    If you get any red lettering in the left hand window it means you made an error typing your drive\directory information.
    You can save the program for later use when you are done. Note the last line of output tells you how many files were processed.

    Note: Your destination folder must EXIST the program will NOT create it! Also the \ in the path names appended to the file names are replaced with underscores (_).

    HTH
    Hello Sir,
    Thanks a lot for your wonderful code in power shell. i can able to understand the instructions that you told me to do. i did that, I didn't get any error. thank u so much.. and no need of asking sorry and all, in spite of your busy schedules, you are managing to help me, for that itself i have to thank you a lot... than you once again..

    And Sir here i have little modifications in the code,
    I told that the main folder consists of lots of sub folders and i need the name of sub folders in the names of CSV files which are inside to that sub folders.. but in this code if i give the sub folders name only it is processing the files, it is not processing from main folder. more over, even in this output also, i am not getting the sub folders name in the csv file name. it is not giving ,my required output. here i am attaching the screen shots along with my output folder for your kind reference and easy understanding if what i want.

    can you please modify the code and give me... it would be highly helpful for me...
    looking forward for the positive reply...

    Thank you so much sir..
    With Regards,
    Dharani.
    Attached Files Attached Files
    Last edited by dharani suresh; 2014-04-24 at 06:47.

  12. #8
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,820
    Thanks
    19
    Thanked 108 Times in 102 Posts
    If the files are CSV files not XLS files, then you would have to change the line which includes
    *.XLS
    to
    *.CSV

    The two path name variables, which I have as
    set combined=D:\JG\$combined
    set topfolder=d:\JG\$top

    should be changed to something like
    set combined=<wherever you want but preferable NOT the same as topfolder>
    set topfolder=C:\Users\dsuresh\Desktop\Maize_20140410_ 120440+0530

    You can always move all the files in the "combined" folder wherever you want afterwards, using Windows Explorer (or whatever), since is far safer to do this rather than running any risk of recursion, where files get copied multiple times and unhappiness occurs.

    If you want to view what is happening while running the BATch file
    change the first line to read (for example)
    @echo .off
    and insert one or more PAUSE statements in the code at likely places.

    Remote debugging of BATch files is not easy...
    Last edited by BATcher; 2014-04-24 at 03:49.
    BATcher

    If it wasn't for the weather, Great Britain would be a silent nation.

  13. The Following User Says Thank You to BATcher For This Useful Post:

    dharani suresh (2014-04-24)

  14. #9
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BATcher View Post
    If the files are CSV files not XLS files, then you would have to change the line which includes
    *.XLS
    to
    *.CSV


    Remote debugging of BATch files is not easy...
    Dear BATcher,

    I was doing the following steps....
    1) First in the code, I changed the xls to csv.
    2) I created two folders separately for source and destination.
    3) In the code, I changed my path in the two lines namely set combined path and set top folder path.
    4) Then I just copied and pasted your code in cmd prompt and run it. but it is saying that the system cannot find the path specified.

    Can you please tell me what shall I do if I want my required output in the combined folder???

    I have also attached the doc file for your kind reference and easy understanding of what i did exactly.please find the attachment. i hope i am clear and i am not confusing you...

    Thank you…

    With Kind Regards,
    Dharani.
    Attached Files Attached Files

  15. #10
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,820
    Thanks
    19
    Thanked 108 Times in 102 Posts
    From your output, it seems to me that the topfolder name does not exist!

    Just in front of the first FOR command, insert
    DIR "%topfolder%" & PAUSE
    and that should show if you've got it right.

    The other possibility is that you have not saved the posted code as a BATch file, such as COPIER.BAT. You should be running COPIER.BAT (or whatever you've called it...).
    Last edited by BATcher; 2014-04-24 at 10:37.
    BATcher

    If it wasn't for the weather, Great Britain would be a silent nation.

  16. The Following User Says Thank You to BATcher For This Useful Post:

    dharani suresh (2014-04-24)

  17. #11
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,042
    Thanks
    195
    Thanked 755 Times in 691 Posts
    Dharani,

    The code is acting correctly in the second example you provided because it is designed to strip the Base drive/folder information from the process which in that case would only leave the file name.

    As for the first case I can't see why it didn't work for you as it worked perfectly in my tests.
    psoutput.JPG

    It appears from your posting that you ran the code by typing it into the PS Command Line interface. Try using the PS ISE environment per my instructions and copy the code in as this will help eliminate hard to find typing errors and also make it easier to rerun the code for testing. HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  18. The Following User Says Thank You to RetiredGeek For This Useful Post:

    dharani suresh (2014-04-24)

  19. #12
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by RetiredGeek View Post
    Dharani,

    It appears from your posting that you ran the code by typing it into the PS Command Line interface. Try using the PS ISE environment per my instructions and copy the code in as this will help eliminate hard to find typing errors and also make it easier to rerun the code for testing. HTH
    Hello Sir,
    First of all, I would like to thank you so much sir for putting your continuous effort in helping me to sort out my problem.. my heartfelt thanks to you sir...

    As you instructed me, i tried in Powershell ISE, even then its not working, i am getting same result, saying that 0 files copied. i have attached the screen shot of what i did and my folders as well in the word document for your kind reference.
    please find the attachment..

    Sorry sir for keep troubling you like this...
    looking forward for the positive reply and solution...

    Thank you,

    With Kind Regards,
    Dharani.
    Attached Files Attached Files

  20. #13
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by BATcher View Post
    From your output, it seems to me that the topfolder name does not exist!

    Just in front of the first FOR command, insert
    DIR "%topfolder%" & PAUSE
    and that should show if you've got it right.

    The other possibility is that you have not saved the posted code as a BATch file, such as COPIER.BAT. You should be running COPIER.BAT (or whatever you've called it...).
    Hello BATcher,
    Hurraahhh!!!!Thank you so much.... finally it works... The code is awesome... as you instructed me, i saved the code as .bat file and then i run in cmd prompt, finally i got the required output. Thanks a lot for making continuous effort in helping me... i was really wondering that how can you do this... the code is really surprising.... you are great...

    BATcher, can i ask one more small help from you, i hope this will not be a big thing for you, as you told me earlier my sub folders name are all too big and complicated. yes its true.
    can you suggest me a way how to cut it down into half?
    for example, if my sub folders names are, Maize_D-1_9_Maize_924ee43c-b446-11e3-95ce-002590c40d8e, cowpea_D-1_10_Maize_924f36c6-b446-11e3-95ce-002590c40d8e, i need it to be cut down to Maize_D-1_9 and cowpea_D-1_10. Is there is any way to do this please suggest me????
    Because, i need only crop name and its D19 AND D110 NUMBER from which we will identify from which sector output has been generated.

    For further help, definitely i will come back to you... if you don't mind, can i have your mail id please... ?????
    please don't think that i am asking too many helps from you.. please...

    With Kind Regards,
    Dharani.
    Last edited by dharani suresh; 2014-04-25 at 02:43.

  21. #14
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,820
    Thanks
    19
    Thanked 108 Times in 102 Posts
    RenameFolders.bat appears below

    Please note that I am assuming that ALL the lower-level folders are of the form
    Token1_Token2_Token3_.....
    example Maize_D-1_9_.....

    Also note that I have put an ECHO command at the front of the ren command right at the bottom of the file.
    Run the BATch file once, and check that what each folders is being renamed TO matches what you expect.
    Then remove the ECHO word, leaving the ren command, save the file, then run the BATch file again.

    Code:
    @echo off
    
    :: save this file as something like RenameFolders.bat
    
    setlocal
    :: assume "top" folder is D:\JG\$top
    ::   with multiple folders beneath, all of which contain CSV Excel spreadsheets
    
    set top=D:\JG\$top
    :: note that you have to change the previous line to match your 'top' folder!
    
    :: process each sub-folder name in turn
    for /f %%a in ('dir /ad /on /b "%top%"') do call :processfolder "%%a"
    
    endlocal
    goto :eof
    
    ::----------
    :processfolder
    
    :: remove the double-quotes to give the original folder name
    set original=%~1
    
    :: extract the first three 'tokens' from the folder name 
    for /f "tokens=1-3 delims=_" %%a in ('echo %original%') do (
     set truncated=%%a_%%b_%%c
    )
    :: rename the folder to the truncated name
    ECHO  ren   %top%\%original%   %truncated%
    :: remove the ECHO word once you're happy the right results will happen
    
    goto :eof
    BATcher

    If it wasn't for the weather, Great Britain would be a silent nation.

  22. The Following User Says Thank You to BATcher For This Useful Post:

    dharani suresh (2014-04-25)

  23. #15
    Lounger
    Join Date
    Apr 2014
    Posts
    25
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BATcher View Post
    RenameFolders.bat appears below

    Please note that I am assuming that ALL the lower-level folders are of the form
    Token1_Token2_Token3_.....
    example Maize_D-1_9_.....

    Also note that I have put an ECHO command at the front of the ren command right at the bottom of the file.
    Run the BATch file once, and check that what each folders is being renamed TO matches what you expect.
    Then remove the ECHO word, leaving the ren command, save the file, then run the BATch file again.

    Code:
    @echo off
    
    :: save this file as something like RenameFolders.bat
    
    goto :eof

    OH MY GOD!!!!! absolutely stunning... how you are able to do these magics... i am really impressed... THANK YOU SO MUCH... THANKS A LOT... I GOT EXACTLY WHAT I NEED.... if i run with the echo command, i am not getting my required output, if i remove that and run the cmd, i am getting my exact output.. i hope these two batch files will work dynamically for all my similar kind of files...

    BATcher,
    now really your magics triggred me to learn these cmds... i just know the basics of DOS cmds...
    If you have time and in case if it is possible for you, can you please explain me only the important parts of code and also can you please tell me what is the use of creating the *.bat file and why cmd prompt didn't accept the commands while i am directly copying and pasting the cmds in it???

    For further help, definitely i will come back to you... if you don't mind, can i have your mail id please... ?????

    My heartfelt thanks to you....
    Thanks a lot!!!!!!!

    With Kind Regards,
    Dharani.
    Last edited by dharani suresh; 2014-04-25 at 08:08.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •