Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Batch file to move many listed files to one specific folder

    Good Morning,

    I have the below code i am trying to use to search through all folders and subfolders for specifically listed files. At current it does not appear to search through subfolders.

    Any ideas?

    @echo off

    set LISTFOLDER=C:\Source
    set FILESPATH=c:\Test
    set DESTPATH=c:\batch\destination

    for /f "tokens=*" %%i in ('dir /b ^"%LISTFOLDER%\*.txt^"') do (call :COPY_FILES "%LISTFOLDER%\%%i")

    pause
    exit

    :COPY_FILES
    for %%i in (%1) do set DEST=%%~ni
    for /f "usebackq delims==" %%i in (%1 /b /a-d /s) do xcopy /qvs "%FILESPATH%\%%i" "%DESTPATH%\%DEST%\*"

    Regards

  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
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    You need the "/r" switch in your "for" statement.
    You also need a "goto :eof" to allow your batch file to call sub-routines - "exit" isn't the correct method according to Microsoft.
    In this case you can do it all on one line.
    To test it add an "echo" immediately before the "xcopy", see the remark.

    Code:
    @echo off
    
    set LISTFOLDER=C:\Source
    set FILESPATH=c:\Test
    set DESTPATH=c:\batch\destination
    
    REM for /r %LISTFOLDER% %%I in (*.txt) do echo xcopy /qvs "%FILESPATH%\%%~nxI" "%DESTPATH%%%~pnxI"
    for /r %LISTFOLDER% %%I in (*.txt) do xcopy /qvs "%FILESPATH%\%%~nxI" "%DESTPATH%%%~pnxI"
    cheers, Paul

  4. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,802
    Thanks
    18
    Thanked 106 Times in 100 Posts
    The general opinion among BATchers is that FOR /R can be somewhat flakey.

    But if it works for the OP, then you have a good and elegant solution!
    BATcher

    Mr Owl ate my metal worm

  5. #4
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,

    I have tried your coding with the below error presenting itself.

    The following usage of the path operator in batch-parameter substitution is invalid %~nxl" "%DESTPATH%%%~pnxl"

    The code i am using is as you suggest with goto :EOF at the end.

    Cheers

  6. #5
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    I tested it before posting so it may be a transcription error. Did you copy the code I posted or type it in?

    p.s. No need for a "goto :eof" unless you are using sub-routines in your batch files, or you want to jump out before the end.

    cheers, Paul

  7. #6
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried both ways, copy and paste, then manually typed it, same error both times, I'm not in work now, so ill try again tomorrow, maybe my fingers went dyslexic or something

    Cheers

  8. #7
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still no joy i have copy and pasted, plus manually typed it and i still receive the above mentioned error

    Cheers

  9. #8
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    What OS are you using? I'll try and test it in the same environment.

    Are you using Word or similar as the editor? You must use Notepad, or my favourite, Notepad++.

    cheers, Paul

  10. #9
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Using notepad to made all edits, I believe we are using windows 7 if that helps

  11. #10
    Bronze Lounger DrWho's Avatar
    Join Date
    Dec 2009
    Location
    Central Florida
    Posts
    1,209
    Thanks
    15
    Thanked 159 Times in 124 Posts
    Excuse a dumb question, Please.
    I've read this entire thread and I still don't understand exactly what it is that the OP is trying to accomplish.
    I back up all my data to a 32gig flash drive using a batch file and XCOPY.
    After the initial backup, it copies only those files that have either been created or changed since the last backup.
    But I guess that's not what the OP is trying to do.
    For myself and anyone else who just doesn't get it, could we have a better explanation of what's trying to be done?
    Thanks lots
    The Doctor
    Experience is truly the best teacher.

    Backup! Backup! Backup! GHOST Rocks!

  12. #11
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, here goes, apologies if this still doesn't make sense upon completion.

    I have a text file with a list of files (changes weekly)

    I need a batch file to use the text file to search through a root folder and all subfolders and copy all the files from the list and paste them into a seperate single folder.

    Does that make sense?

  13. #12
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    I don't have the error running in Win 7 home 32bit. It works as expected. Try removing the quotes and test.

    The code I posted doesn't actually do what you requested in your last post, so I'll have another look at it later - unless someone beats me to it.

    cheers, Paul

  14. #13
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    Does your list of files include directories or is it just file names? If it's file names this code should be OK.
    Code:
    set FIILELIST=C:\List\files.txt
    set FILESPATH=C:\Source
    set DESTPATH=C:\Destination
    
    for /f %%X in (%FIILELIST%) do call :COPY_FILES "%%X"
    goto :eof
    
    :COPY_FILES
    for /r %FILESPATH% %%I in (%~1) do echo xcopy /qvs "%%I" "%DESTPATH%%%~pnxI"
    cheers, Paul

  15. #14
    New Lounger
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ignore previous post i got it working, now i have a couple of other issues, during it running through every folder it questions the filename asking for confirmation of file or directory, obviously if i am searching for 1000 files i don't want to have to keep pressing "f", in addition it doesn't place it into the specified folder it creates a brand new folder inclusive of the folder structure where it found the file.

    Hope that makes sense.

  16. #15
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    Can you please post the code you are using?

    cheers, Paul

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
  •