topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday November 8, 2024, 8:09 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Batch file to move files (less than total size) from A to B  (Read 11092 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
So excited with the last request that I'm making another sort-of-similar request.

To fill up a USB thumb drive with various YouTube downloaded videos, I use Explorer, detail view, select the first....

...page down...

...page down...

...and Shift-select the last.  So as to get the total size I want to send to the thumb drive.

Then I drag these files over to the thumb drive.

I'm wondering if there could be a batch file that can help out with the process.

Type into the batch file:

-- Source
-- Destination
-- Total 'upper limit' amount of files desired to be transferred (will end up approximate, of course)

'Order' of files moved, which first, should be by 'filename' order.  (I think that's Windows' default, but not sure.)

Double-click.  Presto.

Example:
Source:  d:\video\
Destination:  h:\
Amount of files to transfer:  50 Gb.

Move the files.
Order of move by filename.

Wondering if this is readily doable through batch file (or powershell).

(My hunch is there would have to be a running sum of sizes sent to some cache.  Keep going until....  A batch file to simply move files is easy enough.  The trick here is the total size wanted.)

Thoughts appreciated.

Nicholas Kormanik


highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #1 on: April 02, 2019, 04:51 AM »
Use the last batch file I wrote and count up their size instead of count?

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #2 on: April 02, 2019, 06:22 AM »
Exactly, highend01.  Just count the total size, if possible, and stop the process before reaching the user-inputed limit.

Don't mean to put the burden on you.  But you certainly aced the last similar request.

I've come across programs for filling up DVDs and CDs with music files.  As I recall, the programs will pick and choose from a set of files, and put as much as possible onto the disk.  Summing total size.

In the present case I just want to start from -- actually, 0001_xyz.mp4.  Then 0002_xyz.mp4.  And on and on until the set total size.  I append the numbers -- serialize -- to all the .mp4 files, to make playing easier (Amazon firestick, using MX Player on the tv, plays in the order of filename, thus the numbers).


Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,938
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #3 on: April 02, 2019, 11:57 AM »
A 3rd party file manager like Directory Opus shows you the size of the files you selected.

Don't take this the wrong way, but here's a taught. With the amount of file management you need to do, it might be useful to start looking into one of those. Spend the cash and automatize what you can within that software. You have a GUI to work with, In case of Directory Opus, also an extensive manual, the batch scripts you already have can be implemented as a new button in DO and while your automation is busy, you'll have a progress bar to see how far along you are.

Windows Explorer (in any version of Windows) is not a good fit when you need to manage lots of files. It works more or less for tiny to small file-sets. But it is a time sink and RSI inducer when you try to use it with big file-sets. So falling back to scripting appears the next best thing to do. And it is.

However, you seem to be depending with regards to scripting batch files, so it might be handier for you to start considering 3rd party file managers, like: Directory Opus, Total Commander, XYplorer, xplorer2, Double Commander, Free Commander, Altap Salamander, etc. (this is a list of managers I tried and is in order of my preference).

Look here for a list of 26 3rd party file managers. The fact that there are so many, indicates that Windows Explorer is flawed (deeply).

While scripting is an excellent idea for menial file management tasks, you must either learn (and practice) scripting or get a tool for the job. That way, it is much easier to execute your ideas on how you want your files to be managed the moment you have a new idea about how to do that.

The time being lost by writing down your idea, making the request here at DC, waiting for a response and then testing the response, could have been spend on learning how to script or doing the job with a 3rd party file manager.

On a side note: I was not aware of the existence from Multi Commander. I am taking a look at it and am pleased. The fact that is portable, scores high with me, the Directory Opus 4 "look" (Amiga home computers owners know what I'm about) scores high too and you can edit these buttons extensively, but also the main menu and context menu as well. I start to think it should take the place xplorer2 in my list...maybe even one higher.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #4 on: April 02, 2019, 12:02 PM »
Highly recommend what Shades said above, invest in a decent file manager.

Doing it a little differently:

2019-04-02 17_46_04.pngBatch file to move files (less than total size) from A to B

Put the shortcut in C:\Users\<user>\AppData\Roaming\Microsoft\Windows\SendTo, edit its Properties to point to wherever you've put the script.

Edit the path to the script:
Code: Text [Select]
  1. C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\PoSh\MoveMovies.ps1"


  • Right-click on the folder with the movies (MP4, MKV, FLV, AVI) -> Send To -> MoveMovies
  • Select the destination folder
  • Enter the gigabyte value of files to move

Hitting Cancel at any requester will exit, as will running out of space, or entering a GB value out of range.

It only checks the available space on the destination once before moving files so if you happen to copy other things to the destination that reduce the expected space then the Move-Item will fail and an error will popup, (in theory).

There's also a popup when it's finished and if you enter a number out of range.

That's about it for error checking, (hey, it's a 100% improvement over my usual error checking routines).

NOTE: Windows doesn't allow Send To on root directories so it won't work for them.  Could always add a couple of lines to call the function to ask for a Source directory instead of having it passed via Send To ... an exercise for the user.

Code: PowerShell [Select]
  1. <#
  2.   MoveMovies.ps1
  3. #>
  4. param (
  5.   [string]$source
  6. )
  7.  
  8. Add-Type -AssemblyName System.Windows.Forms
  9.  
  10. Function Get-Folder {
  11.   $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  12.   $objForm.Description = "Select destination folder"
  13.   $objForm.SelectedPath = [System.Environment+SpecialFolder]'MyComputer'
  14.   $objForm.ShowNewFolderButton = $false
  15.   $result = $objForm.ShowDialog()
  16.   if ($result -eq "OK") {
  17.     return $objForm.SelectedPath
  18.   } else {
  19.     return $null
  20.   }
  21. }
  22.  
  23. Function Get-MoveSize {
  24.   param (
  25.     [int]$max
  26.   )
  27.   [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
  28.  
  29.   $title = 'Move Movies'
  30.   $msg = "Total GBs of files to move [1-$($max)]: "
  31.  
  32.   $text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
  33.   return (($text/1) * 1Gb)
  34. }
  35.  
  36. Function Send-Notification {
  37. # https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/x83z1d9f(v=vs.84)
  38.   param (
  39.     [string]$msg,
  40.     [int]$type
  41.   )
  42.   $wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
  43.   $wshell.Popup($msg, 0, "MoveMovies", $type) | Out-Null
  44. }
  45.  
  46.  
  47. $dest = (Get-Folder)
  48. if ($dest -ne $null) {
  49.   # Get disk volume label
  50.   $dDrive = ($dest | Split-Path -Qualifier)
  51.   # Get disk free space, round down to nearest whole number
  52.   $dFree = [math]::Round((gwmi Win32_LogicalDisk -Filter "DeviceID='$($dDrive)'").FreeSpace / 1Gb)
  53. } else {
  54.   break
  55. }
  56.  
  57. $moveSize = (Get-MoveSize $dFree)
  58. if (($moveSize -lt 1) -or ($moveSize -gt ($dFree * 1Gb))) {
  59.   Send-Notification "Input outside acceptable range!" 16
  60.   break
  61. }
  62.  
  63. Write-Host $dest $moveSize $dDrive ($dFree * 1Gb)
  64.  
  65. # Get list of videos in source directory
  66. $videos = (Get-ChildItem "$($source)\*" -Include *.mp4,*.mkv,*.flv,*.avi | Sort Name)
  67. for ($i = 0; $i -lt $videos.Count; $i++) {
  68.   if ($videos[$i].Length -lt $moveSize) {
  69.     Move-Item -LiteralPath "$($videos[$i].FullName)" -Destination "$($dest)"
  70.     if ($?) {
  71.       $moveSize -= $videos[$i].Length
  72.     } else {
  73.       Send-Notification "Moving file failed!" 16
  74.       break
  75.     }
  76.   } else {
  77.     Send-Notification "Maximum number of files moved into space available/allowed" 64
  78.     break
  79.   }
  80. }

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #5 on: April 04, 2019, 12:47 AM »
@highend01
@4wd
@Shades

Agree that native Explorer begs for enhancements.  I've tried to do that with whatever third-party upgrades I could find.

Another alternative file manager, though, is definitely something to consider.

Regarding the present challenge, one double-click seems super appealing, if highend01 could pull off the wizardry.  Like, wow!

I'll try 4wd's offering.  Looks promising.


nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #6 on: April 04, 2019, 01:27 AM »
@highend01
@4wd
@Shades

4wd, works as advertised.  Very slick.  Thank you!

Let's see if highend01 can code it into a batch file, for single double-click.

Still wondering if that's possible.


highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #7 on: April 04, 2019, 05:52 AM »
You are kidding me, right?

That's the exact same thing, only in green...

@SETLOCAL EnableDelayedExpansion
@ECHO OFF

REM 1 MB = 1048576 bytes
REM 157286400 = 150 MB
SET Max=157286400
SET Src=R:\tst_src
SET Dst=R:\tst_dst

IF EXIST "%Src%" (
    IF EXIST "%Dst%" (
        SET /a Size = 0
        FOR %%I IN ("%Src%\*") DO (
            SET /a Size += %%~zI
            IF !Size! GTR %Max% GOTO :EOF
            MOVE /Y "%%I" "%Dst%" >NUL 2>NUL
        )
    )
)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #8 on: April 05, 2019, 04:02 AM »
Well, @highend01, that's some amazing stuff.  Thank you!

Basically works as asked for, as a one double-click trick.  Files up to a certain total-size limit get moved to the new location.

One issue did come up, though.  Via this approach there appears to be an inherent upper limit.  At 2 GB.  No matter how high the user inputs their own upper limit, Windows stops short.

Hmmm.  Any idea why?

I suppose user (me) can come back for seconds, and thirds.  I.e., run the batch file multiple times.


highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #9 on: April 05, 2019, 04:29 AM »
Google stopped working (again)?

https://stackoverflo...32-bits-of-precision

Includes ways how to circumvent this restriction. Have fun learning a minimal amount of batch scripting!

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #10 on: April 05, 2019, 05:04 AM »
So much fun that we all should do it.

What say, gang??

(grumble, grumble, grumble....)


4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #11 on: April 05, 2019, 03:02 PM »
So much fun that we all should do it.

What say, gang??

(grumble, grumble, grumble....)

Or learn _any_ scripting language at all ...

... meanwhile ... 30 seconds later ...

Code: PowerShell [Select]
  1. <#
  2.   MoveMovies.ps1
  3. #>
  4. Add-Type -AssemblyName System.Windows.Forms
  5.  
  6. Function Send-Notification {
  7. # https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/x83z1d9f(v=vs.84)
  8.   param (
  9.     [string]$msg,
  10.     [int]$type
  11.   )
  12.   $wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
  13.   $wshell.Popup($msg, 0, "MoveMovies", $type) | Out-Null
  14. }
  15.  
  16.  
  17. $source = "D:\Movies"
  18. $dest = "D:\temp"
  19. $moveSize = 2000 # In gigabytes
  20. $moveSize *= 1Gb
  21.  
  22. # Get list of videos in source directory
  23. $videos = (Get-ChildItem "$($source)\*" -Include *.mp4,*.mkv,*.flv,*.avi | Sort Name)
  24. for ($i = 0; $i -lt $videos.Count; $i++) {
  25.   if ($videos[$i].Length -lt $moveSize) {
  26.     Move-Item -LiteralPath "$($videos[$i].FullName)" -Destination "$($dest)"
  27.     if ($?) {
  28.       $moveSize -= $videos[$i].Length
  29.     } else {
  30.       Send-Notification "Moving file failed!" 16
  31.       break
  32.     }
  33.   } else {
  34.     Send-Notification "Maximum number of files moved into space available/allowed" 64
  35.     break
  36.   }
  37. }

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #12 on: April 06, 2019, 01:32 AM »
@highend01
@4wd
@Shades
@everyone else

Okay, here is where we now stand:  As pointed out in the url above, advice #2 is to use "another language."

That seems clear enough (especially bolded).

Who else would like to take the next step?


4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #13 on: April 06, 2019, 03:58 AM »
Why?

PowerShell doesn't suffer from the 32bit limitation ... as shown by me setting the script above to 2TB.

The numbers must all be within the range of 32 bit signed integer numbers (-2,147,483,648 through 2,147,483,647) to handle larger numbers use PowerShell or VBScript.
« Last Edit: April 06, 2019, 06:45 AM by 4wd »

highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #14 on: April 06, 2019, 04:33 AM »
Who else would like to take the next step?
Maybe... You?

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #15 on: April 07, 2019, 02:35 AM »
Me?

I just thought of another question....

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #16 on: April 09, 2019, 07:51 PM »
@highend01
@4wd
@Shades
@everyone else

Guys and gals, let's wrap this one up.  Anything more we can do?

If limited to less than 2 GB to be moved, probably best to use highend01 method, one-double-click.  Easy peasy.

If more than 2 GB, 4wd has a terrific approach, just requiring a few more steps.


highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #17 on: April 09, 2019, 09:17 PM »
Sure, add the stuff from the stackoverflow link. Done.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Batch file to move files (less than total size) from A to B
« Reply #18 on: April 09, 2019, 10:24 PM »
If more than 2 GB, 4wd has a terrific approach, just requiring a few more steps.

By "few more steps" you mean:
1. Move shortcut from Send To to Desktop
2. Double-Click to run

Technically, that's only a couple more steps and only applies the first time.

There were simple changes you could have easily made yourself.

Anything more we can do?

Learn?
« Last Edit: April 09, 2019, 10:37 PM by 4wd »