Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Simple BAT file to copy files to specific folder

  • 12-11-2024 11:29AM
    #1
    Registered Users, Registered Users 2 Posts: 959 ✭✭✭


    Hi All. I need to do a cleanup on a computer and basically copy certain file formats into I would assume one folder.

    I was going to use something like this as a simple starting point:

    @ echo off
    COPY "Source folder path*.xlsx*" "destination path"
    PAUSE

    A few questions:

    1. What is the seperator for file format? For example if I want to copy excel and powerpoint files (In my case it will be different photo types so jpeg, png, mov, all those type extensions) is it the comma or semi-colon seperator between extension types?

    2. If I want to include sub folders in the source folder path (Eg. desktop has a few different folders and sub folders, want to extract all files of specified format to one new destination location)

    3. Thirdly and this is probably strange… If I want to copy the files and sub folder they are located in. So for example if I have desktop1 and desktop 2 folders, and both contain excel files.. in the destination folder I would like these sub folders to be included and then the file inside this folder, so within destination folder desktop 1 and desktop 2 exist with the relevant files within them…

    The reason for this is there are probably duplicated files and folders, in fact I know there is because the person is unorganised so there will be multiple duplicated files and folders. By copying the sub folder that the file is contained within I can then run the script again with a specified source folder (desktop1 folder may exist in a few different locations on the C drive but using this I can bring them to one location then run script again with specified source folder path), and then bring the files back into one folder The only issue I can see with this is it won't copy the same folder name into a destination, if it already exists so is there any work around, or different approach or ignore duplicates of the destination file itself of same name. I don't really want to over engineer it to be honest although this point 3 sounds complicated!!

    4. If I do proceed with point 3 above, there will be a lot of files and stuff going on! Is there anyway to add a time out or pause or break because the speed the copying takes will lag significantly vs the speed the script rune at and will this cause a crash or fatal exception or issues in the process? Is there a way to add a break after every say 100 files press enter to continue or something like that?



Comments

  • Moderators, Regional Midwest Moderators, Paid Member Posts: 11,353 Mod ✭✭✭✭MarkR


    I asked copilot about this, so give it a good review and test.

    @echo off
    setlocal

    REM Set the source and destination directories
    set "source=C:\path\to\source\directory"
    set "destination=C:\path\to\destination\directory"

    REM Create the destination directory if it doesn't exist
    if not exist "%destination%" mkdir "%destination%"

    REM Copy the files with the specified extensions
    xcopy "%source%\*.jpeg" "%destination%" /s /i /y
    xcopy "%source%\*.png" "%destination%" /s /i /y
    xcopy "%source%\*.ppt" "%destination%" /s /i /y
    xcopy "%source%\*.mov" "%destination%" /s /i /y

    echo Files copied successfully!
    pause
    endlocal


  • Moderators, Regional Midwest Moderators, Paid Member Posts: 11,353 Mod ✭✭✭✭MarkR


    Sorry, I didn't read the full instructions, and I don't want to dive that deep. :-D



  • Registered Users, Registered Users 2 Posts: 959 ✭✭✭JIdontknow


    This works fine, thanks! I never even thought to use copilot..



  • Moderators, Regional Midwest Moderators, Paid Member Posts: 11,353 Mod ✭✭✭✭MarkR


    It's pretty good at getting you pointed in the right direction for lots of little coding tasks.



  • Registered Users, Registered Users 2, Paid Member Posts: 9,703 ✭✭✭10-10-20


    "rsync -av —progress "%source%\*.jpeg" (destination)" should verify the copies. I prefer that over xcopy.



  • Advertisement
  • Registered Users, Registered Users 2 Posts: 24,465 ✭✭✭✭Esel
    Not Your Ornery Onager


    Not your ornery onager



  • Registered Users, Registered Users 2, Paid Member Posts: 9,703 ✭✭✭10-10-20


    Not natively but has been a third party download now for years. I used to use it for backups but I just realised that it's dependent on ssh… (I was backing up to Linux) so isn't a good candidate for Windows to Windows! Whoops! 😀



Advertisement