#205908 - 2012-10-05 11:09 AM
Newbee help for sorting files please
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Hallo to all,
I'm glad to find this great forum here!
My career is system admin in a small company. We are two, one for the hardware and one for the software. My partner is the programmer and i'm the network and hardware guy. He is in a hospital right now with a broken leg and two broken arms after a car accident.
Three days ago my boss came into my office and made a request about a new method of sorting files. I was using batch-files for years, but now he want to have some changes.
What he want to have is: If a file is in "\direction\folder", move it to "\direction2\year\month\day" That all should be in an endles loop.
My first try for testing was:
Break on
$Monate = "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC" ; Monate
$File = "*.*"
$FileDate = GetFileTime ($File)
$FileYear = SubStr($FileDate,1,4)
$FileMonth = SubStr($Monate,Val(SubStr($FileDate,6,2))*4-3,3)
$FileDay = Right("0"+SubStr($FileDate,9,2),2)
$TypeFound = 0
MD (C:\Testing\t1+"\"+$Type) ; create \Type\JAHR\MONAT\Tag
MD (C:\Testing\t1+"\"+$Type+"\"+$FileYear)
MD (C:\Testing\t1+"\"+$Type+"\"+$FileYear+"\"+$FileMonth)
MD (C:\Testing\t1+"\"+$Type+"\"+$FileYear+"\"+$FileMonth+"\"+$FileDay)
IF Exist ("C:\Testing\t\*.*")
move "C:\Testing\t\*.*" "C:\Testing\t1\"+$Type+"\"+$FileYear+"\"+$FileMonth+"\"+$FileDay"
It moves the files from the current day into the correct direction, but when i tried it with an older file, it moved it into the current day-direction.
I read about FileTime and FileTimeStamp, but i really have no idea, how to use it correct. Have you guys the time to help me please? I am quite new in this business, so please dont laugh about me, any beginning is hard.
All replys are welcome,
Thank you very much,
Buhli
Edited by Mart (2012-10-07 12:07 PM) Edit Reason: Please use code tags when posting code.
|
|
Top
|
|
|
|
#205920 - 2012-10-06 12:51 PM
Re: Newbee help for sorting files please
[Re: Glenn Barnas]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Ahh, copy that. It make sence to me, thank you very much for the advise! I am still reading the manual, but for me it is quite hard to programming.
Cheers Glenn,
Buhli
|
|
Top
|
|
|
|
#205928 - 2012-10-08 08:57 AM
Re: Newbee help for sorting files please
[Re: Buhli]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
It works fine, but i still have the problem with the create-time of the files. Sometime it happends, that we have to edit a file from, for example 02.09.2012. It lays the edited file into the current day-direction, not into the create time-direction.
Can someone give me an advise for using the FileTimeStamp in this case please?
Best regards, Buhli
Edited by Buhli (2012-10-08 11:22 AM)
|
|
Top
|
|
|
|
#205932 - 2012-10-08 02:06 PM
Re: Newbee help for sorting files please
[Re: Buhli]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
Looking at this, you can't use $File = "*.*" and expect it to work properly.
Try something like this:Break On
; define array of month names
$aMonate = Split('x,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC', ',')
$SPath = 'c:\temp\' ; where the files are...
$DPath = 'C:\Temp\dest\' ; where the files go...
$File = Dir($Path + '*.*')
; get first file name
While Not @ERROR
'Processing ' $Path $File ? ; for debugging
$FileDate = GetFileTime($File)
$FileYear = SubStr($FileDate,1,4)
$FileMonth = $aMonate[Val(SubStr($FileDate, 4, 2))]
$FileDay = Right('0'+SubStr($FileDate,9,2),2)
$TypeFound = 0
$DstDir = $DPath + $FileYear + '\' + $FileMonth + '\' + $FileDay
; create the destination directory, if needed
If Not Exist($DstDir)
'MD ' $DstDir ? ; for debugging
MD $DstDir ; create the destination dir
EndIf
; move the file to the destination folder
'Move ' $Path + $File + ' ' + $DstDir ; for debugging
; Move $Path + $File $DstDir ; uncomment for production
$File = Dir() ; get next file name
Loop I'm not sure where you define "$Type" or why you define "$TypeFound", but the code example here works. Using variables for source and destination eliminates hard-coding and lets you test in one location and make a simple change to put the code into production. Using hard-coded paths means you have to change lots of code to put it in production, making it more difficult to troubleshoot later when you miss just one setting!
I replaced your substring / "almost Mod" code with an array of month names to slightly simplify the process. Note the "x" value in the array as a placeholder for "Month 0". 
Also, you can create the entire destination directory with just one statement - you don't need to build it folder by folder.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#205935 - 2012-10-08 03:24 PM
Re: Newbee help for sorting files please
[Re: Glenn Barnas]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Hi Glenn,
thank you very much for the quick help!!! I will test it tomorrow morning and will give you a feedback. You explain it very well in my old eyes, so i have a chance as a not programming guy. About the $Type, i just want to make sure, it moves anything from the source destination to the correct target.
It could be an air ticket, a letter etc. If it is an air ticket, move it to des1, if it is a letter, move it to des2.
Cheers and thousend thanks again, Buhli
|
|
Top
|
|
|
|
#205960 - 2012-10-09 09:16 AM
Re: Newbee help for sorting files please
[Re: Glenn Barnas]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Hi Glenn,
i run your script as it was, only uncomand the things for testing. I put some files with different create-dates into C:\Temp and run your script.
It creates the des-dir very well and in it, it creates three folders: "2011", "2012" and "x". Why has the "2011" being created? The files i putinto it were from Aug and Oct 2012? Also the files were not moved.
Did i something wrong?
Cheers, Buhli
Edited by Buhli (2012-10-09 09:30 AM)
|
|
Top
|
|
|
|
#205968 - 2012-10-10 08:42 AM
Re: Newbee help for sorting files please
[Re: Glenn Barnas]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Hi Glenn,
Thank you very much, it comes closer. Here, what i have changed:
Break On
; define array of month names
$aMonate = Split('x,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC', ',')
$SPath = 'c:\Testing\TestDateien\' ; where the files are...
$DPath = 'C:\Testing\t1\' ; where the files go...
$File = Dir($Path + '*.*')
; get first file name
While Not @ERROR
; 'Processing ' $Path $File ? ; for debugging
$FileDate = GetFileTime($File)
$FileYear = SubStr($FileDate,1,4)
$FileMonth = $aMonate[Val(SubStr($FileDate, 6, 2))]
$FileDay = Right('0'+SubStr($FileDate,9,2),2)
$TypeFound = 0
$DstDir = $DPath + $FileYear + '\' + $FileMonth + '\' + $FileDay
; create the destination directory, if needed
If Not Exist($DstDir)
; 'MD ' $DstDir ? ; for debugging
MD $DstDir ; create the destination dir
EndIf
; move the file to the destination folder
; 'Move ' $Path + $File + ' ' + $DstDir ; for debugging
Move $Path + $File $DstDir ; uncomment for production
$File = Dir() ; get next file name
Loop
It creates the correct folder for 2012/OCT/09 and 10. It also put the files into the correct dir. Just one strange thing left: it put all files from my Testing-dir into the target-des, not only the files from "Testing\TestDateien". Wondering, the path should be correct.
Thanks anyway for your quick help Glenn,
Buhli
|
|
Top
|
|
|
|
#205980 - 2012-10-11 09:51 AM
Re: Newbee help for sorting files please
[Re: Glenn Barnas]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Hi Glenn,
sorry for late reply and thank you very much once more! I did what you told me. The strange thing is, it works nearly perfect when i use the "Copy"-command, but not with "Move". Maybe i should use this and then a "Del"-command, not sure - it don't need to be nice, it just must work. Another thing is, if i put a new file into the "Testing"-dir, it will not be copied or moved for some reason. Can i send you the whole Testing-environment, so maybe you want to have a look, or shall it stay in the forum?
Best regards, Buhli
|
|
Top
|
|
|
|
#206025 - 2012-10-16 08:38 AM
Re: Newbee help for sorting files please
[Re: Glenn Barnas]
|
Buhli
Fresh Scripter
Registered: 2012-09-24
Posts: 28
Loc: Berlin, Germany
|
Hi Glenn,
i had no time the last days - business trip and other stuff to do, sorry about the late reply. The moving command wasn't working correct, whatever i tried. It copies very well without deleting, but when i arm the delete-command, it copies not in the same matter. Maybe it should do it one by one, copy the file and then remove it from the source, not all together. I guess there cold be an option with a Fore Each loop? Here is what i have atm( i mean, what you did ).
Break On
:Search
"Searching " ?
DIM $SPath,$SFile ; reset the $Vars
$aMonate = Split(' ,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC', ',') ; Month in an Array (just set the "X" to " " for testing)
$SPath = 'c:\Testing\' ; Source
$DPath = 'C:\t2\' ; Target
$File = Dir($SPath + '*.*')
; get first file name
While Not @ERROR
$FileDate = GetFileTime($File)
$FileYear = SubStr($FileDate,1,4)
$FileMonth = $aMonate[Val(SubStr($FileDate, 6, 2))]
$FileDay = Right('0'+SubStr($FileDate,9,2),2)
; $TypeFound = 0
$DstDir = $DPath + $FileYear + '\' + $FileMonth + '\' + $FileDay
If Not Exist($DstDir)
; 'MD ' $DstDir ? ; for debugging
MD $DstDir ; creating target des
EndIf
SLEEP 1 ; slowing down for checkinhg
If Exist($File) "Copying " $SPath + $File ?
; copying or moving the files to the target des
; Move ' $Path + $File + ' ' + $DstDir ; for debugging
Copy $SPath + $File $DstDir ; copy the files into the target des
; Move $SPath + $File $DstDir ; move the files into the target des
Else
GOTO "Search"
EndIf
SLEEP 1
; DEL $SPath + $File ; delete the files from the source des
; "deleting " $SPath + $File ?
$File = Dir() ; get the next file
Loop
Thank you once more Glenn for your patience with a noob like me.
Buhli
Edited by Buhli (2012-10-16 12:24 PM)
|
|
Top
|
|
|
|
#206027 - 2012-10-16 03:31 PM
Re: Newbee help for sorting files please
[Re: Lonkero]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
The goto will break the logic, actually - it will send you out of the for/each loop and cause the loop to restart!
There should be no goto, no If Exist() - they are unnecessary. The code in my first example with the changes we discussed works. You did not add the $SPath in your code to all references to $File. I just tested the following and it moved all of the files as expected.Break On
Dim $FileDate ; File timestamp
Dim $FileYear ; Year of file timestamp
Dim $FileMonth ; Month of file timestamp
Dim $FileDay ; Day of file timestamp
Dim $aMonate ; Array of month names
Dim $SPAth, $DPath ; Source and Destination paths
Dim $File ; Name of file being processed
Dim $DstDir ; Full destination path as created
; define array of month names
$aMonate = Split('x,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC', ',')
$SPath = 'c:\temp\Src\' ; where the files are...
$DPath = 'C:\Temp\dest\' ; where the files go...
$File = Dir($SPath + '*.*')
; get first file name
While Not @ERROR
If $File <> '.' And $File <> '..'
'Processing ' $SPath $File ? ; for debugging
$FileDate = GetFileTime($SPath + $File)
$FileYear = SubStr($FileDate,1,4)
$FileMonth = $aMonate[Val(SubStr($FileDate, 6, 2))]
$FileDay = Right('0'+SubStr($FileDate,9,2),2)
$TypeFound = 0
$DstDir = $DPath + $FileYear + '\' + $FileMonth + '\' + $FileDay
; create the destination directory, if needed
If Not Exist($DstDir)
'MD ' $DstDir ? ; for debugging
MD $DstDir ; create the destination dir
EndIf
; move the file to the destination folder
'Move ' $SPath + $File + ' ' + $DstDir ; for debugging
Move $SPath + $File $DstDir ; uncomment for production
EndIf
$File = Dir() ; get next file name
Loop Glenn
Edited by Glenn Barnas (2012-10-16 03:37 PM) Edit Reason: Added DIM statements for clarity
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 837 anonymous users online.
|
|
|