Page 1 of 2 12>
Topic Options
#205908 - 2012-10-05 11:09 AM Newbee help for sorting files please
Buhli Offline
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:

 Code:
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
#205911 - 2012-10-05 01:09 PM Re: Newbee help for sorting files please [Re: Buhli]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Welcome to KORG!

I'm rushing out the door, but the first piece of advice I'd suggest is to print the content of your variables to the screen without actually moving or copying any file. This will tell you if your logic (which looks good at first review) is doing what you expect it to. Also, you have am extra quote at the end of your move statement.

Here's what I would print, and it's also how to modify your move. You should also terminate your If statement with an EndIf!
 Code:
"C:\Testing\t\*.*" "C:\Testing\t1\" + $Type + "\" + $FileYear + "\" + $FileMonth + "\" + $FileDay @CRLF
Note that there is no quote at the end of $FileDay.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205920 - 2012-10-06 12:51 PM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Buhli Offline
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 Offline
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 Administrator Offline
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:
 Code:
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! \:D

Top
#205933 - 2012-10-08 02:11 PM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
BTW - you should properly declare all your vars using
 Code:
Dim $var     ; description of var purpose
especially as your scripts become larger or more complex. The one var per Dim isn't required, but helps you self-document your code by identifying the reason for each var early in the program.

There are also several SetOption() parameters that are recommended to be used. Review the "getting started" threads for more info.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205935 - 2012-10-08 03:24 PM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Buhli Offline
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
#205943 - 2012-10-08 05:46 PM Re: Newbee help for sorting files please [Re: Buhli]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
OK - so it is being defined elsewhwere.. Be sure to re-define the destination to include it. \:\)

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205960 - 2012-10-09 09:16 AM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Buhli Offline
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
#205961 - 2012-10-09 01:16 PM Re: Newbee help for sorting files please [Re: Buhli]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
I'll review the code and let you know what I find.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205962 - 2012-10-09 01:23 PM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Replace the FileMonth line
 Code:
$FileMonth = $aMonate[Val(SubStr($FileDate, 6, 2))]
The original value of 4 should have been 6.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205963 - 2012-10-09 02:39 PM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Also - If the files are not being moved, make sure you uncommented the "Move $Path + $File $DstDir " line. (Remove leading ";"). I did not want to move during my test because I used TEMP as my source - several files would be open and non-movable.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205968 - 2012-10-10 08:42 AM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Buhli Offline
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:
 Code:
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
#205971 - 2012-10-10 01:14 PM Re: Newbee help for sorting files please [Re: Buhli]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Change all instances of $Path to $SPath. One in the first Dir command, one in the debug message, and one in the Move statement. I renamed Path to SPath when I added the DPath definition and neglected to change the other references.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#205980 - 2012-10-11 09:51 AM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Buhli Offline
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
#205982 - 2012-10-11 01:02 PM Re: Newbee help for sorting files please [Re: Buhli]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Keep it here.. I'm about to start a company-wide Disaster Recovery exercise - three 16-hour days with cleanup on Sunday so my time will be limited until Monday.

So, if you replace the MOVE with COPY, it works? Look at the manual page for Move and see if any of the optional arguments help.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#206025 - 2012-10-16 08:38 AM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Buhli Offline
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 ). \:\)
 Code:
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
#206026 - 2012-10-16 01:58 PM Re: Newbee help for sorting files please [Re: Buhli]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
goto...


you are testing while not @error but it is firing only for the dir()
you are not testing if copy is succesfull or not.

and get rid of the goto. it does nothing in your logic.

instead of while not error, do while $file and just before loop $file = dir()
_________________________
!

download KiXnet

Top
#206027 - 2012-10-16 03:31 PM Re: Newbee help for sorting files please [Re: Lonkero]
Glenn Barnas Administrator Offline
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.
 Code:
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! \:D

Top
#206029 - 2012-10-16 06:36 PM Re: Newbee help for sorting files please [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
it would break the logic if he had one.
he was trying to go around the logic with goto loop.
could make that stuff work too, but it's way easier to make stuff not work with it, like he ended up doing.
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 837 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.129 seconds in which 0.086 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org