how to copy files to usb-drive in certain order?

New to FreeBASIC? Post your questions here.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

how to copy files to usb-drive in certain order?

Post by srvaldez »

I have some mp3 files and would like to copy them to a USB-drive in certain order, the reason is that my stereo will sort the files in the order that they were written
all the files start with a three digit number then the song name, like 001 first song, 002 second song and so on
the problem is that copying the files using the Windows explorer by select all the files and then draging them to the USB-drive will copy the files out of order
any suggestions?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to copy files to usb-drive in certain order?

Post by MrSwiss »

srvaldez,

I don't clearly understand the problem:
  • Is it related to: "last modified" date/time stamp (aka: programming)?
    Is it related to: configuration of File Explorer?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: how to copy files to usb-drive in certain order?

Post by srvaldez »

the problem is that the stereo sorts the files as it finds them on the USB-drive which is in the order that they were copied
like I said, selecting all the files in Windows explorer and dragging them to the USB-drive will copy the files out of order, that is, unsorted
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to copy files to usb-drive in certain order?

Post by MrSwiss »

srvaldez,

in that case you'll have to program your own "custom copy" routine, that:
  • 1) reads all titles (string array)
    2) sorts array (to your preference)
    3) copies (one by one) to the target ...
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: how to copy files to usb-drive in certain order?

Post by counting_pine »

I had to solve this problem a few years ago. Perhaps you have the same cheap MP3 player?
https://www.amazon.co.uk/gp/aw/cr/rR3AN26S2NR8RXR

You can copy files in alphabetical order through the Windows command line:

Code: Select all

cd /d "path\to\copy\from"
for /f "usebackq delims=" %i in (`dir /b /on`) do copy "%i" "path\to\copy\to"
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: how to copy files to usb-drive in certain order?

Post by dodicat »

Here I have created 10 blank files, sorted their titles and moved them to another folder.
Please use this code inside a temp folder to run it.
Then you will easily adapt something similar to your destination path.

Code: Select all


 #Include "file.bi"
Sub savefile(filename As String,p As String="")
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub


sub bsort(s() as string)
    for n as long=lbound(s) to ubound(s)-1
        for m as long=n+1 to ubound(s)
            if s(m)<s(n) then swap s(m),s(n)
        next
    next
end sub

dim as string g(1 to 10)
for n as long=lbound(g) to ubound(g)
    g(n)=right("000"+str(n),3)+ "Title"+str(n)+".mp3"
    print g(n)
next

bsort(g())

dim as string c=curdir+"\newfiles"
shell "mkdir "+c

for n as long=lbound(g) to ubound(g)
    savefile(g(n))
    shell "copy " + curdir + "\" + g(n) + " " + c
next
sleep

    


 
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: how to copy files to usb-drive in certain order?

Post by srvaldez »

counting_pine and dodicat, thank you
much appreciated :-)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: how to copy files to usb-drive in certain order?

Post by jj2007 »

Tested pseudocode ;-)

Code: Select all

  GetFiles C:\Fotos\Fotos2018\IMG_2018*.jpg	; filenames to string array
  SortFiles name	; sort alphabetically
  For_ each esi in Files$()
	invoke CopyFile, esi, Cat$("J:\Temp\"+Extract$(esi, "\", 0, xsRinstrL)), 0
  Next
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: how to copy files to usb-drive in certain order?

Post by srvaldez »

I found that it was hard to navigate through 600+ songs, so inspired by dodicat's code I stitched together this code
the code creates folders starting with 01 and copies 12 songs into each folder, except for the last folder which will hold 12+remainder songs

Code: Select all

'======================================================
' list files in a folder in a text file in that folder
'  by Richard
'  https://www.freebasic.net/forum/viewtopic.php?p=191571#p191571  
'======================================================
Dim As String folder = "C:\Users\srvaldez\Desktop\MusicToBeCopied"
Dim As String filename, s, q=chr(34)
redim as string g(1 to 1)
dim as long count=1, max, k

filename = Dir( folder & "\*", &h21 )
Do While Len( filename )
    Select Case filename
    Case "."    ' list of file names to ignore
    Case ".."
    Case Else
    	ReDim Preserve g(1 to count)
        g(count) = filename
        count+=1
    End Select
    filename = Dir( )
Loop
count-=1
max=count\12
k=1
if max>=0 then
	shell "mkdir H:\MyMusic"
	for i as long=1 to max
		s=trim(str(i))
		if len(s)<2 then s="0"+s
		s="H:\MyMusic\"+s
		shell "mkdir "+s
		for j as long=k to k+11
			shell "copy "+q+folder+"\"+g(j)+q+" "+s
			k+=1
		next
		if max*12<count andalso i=max then
			for last as long=k to count
				shell "copy "+q+folder+"\"+g(last)+q+" "+s
			next
		end if
	next
end if
the same could be accomplished with a batch file
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: how to copy files to usb-drive in certain order?

Post by dodicat »

You could get them all by putting this into your original music folder
(nothing done, only shown)

Code: Select all


 #Include "file.bi"
 
 Sub string_split(byval s As String,chars As String,result() As String)
    redim result(0)
    Dim As String var1,var2
Dim As long pst,LC=len(chars)
      #macro split(stri)
    pst=Instr(stri,chars)
    var1="":var2=""
    If pst<>0 Then
    var1=Mid(stri,1,pst-1)
    var2=Mid(stri,pst+LC)
    Else
    var1=stri
End if
    if len(var1) then 
    redim preserve result(1 to ubound(result)+1)
    result(ubound(result))=var1
    end if
    #endmacro
   Do
   split(s):s=var2
Loop Until var2=""
End Sub
#Include "file.bi"

Function loadfile(file as string) as String
	If FileExists(file)=0 Then Print file;" not found":Sleep:end
   var  f=freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
end Function

Sub savefile(filename As String,p As String="")
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub


sub bsort(s() as string)
    for n as long=lbound(s) to ubound(s)-1
        for m as long=n+1 to ubound(s)
            if s(m)<s(n) then swap s(m),s(n)
        next
    next
end sub

shell "dir /b > zlist.dat"
sleep 10
dim as string s2=loadfile("zlist.dat")

redim as string names()
string_split(s2,chr(10),names())
bsort(names())

for n as long=lbound(names) to ubound(names)
    if val(names(n)) then print names(n)
next


for n as long=lbound(names) to ubound(names)
    if val(names(n)) then
        'copy to your drive
   ' shell "copy " + curdir + "\" + names(n) + " " + c
    end if
next
print "done"


sleep

 
Post Reply