Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

destination folder depending on month

Status
Not open for further replies.

pawelem

Technical User
Oct 8, 2015
8
0
0
DE
Hello everybody!

I am new here because I am looking for help with my vbs script.
(sorry for my english if I will make any mistakes)

So my script need to copy every files from source folder into
other destination folder which is on mapped drive --- and this part is ready and working.

I manually made there (destination) sub folders according to months (jan)(feb)....(dec)

Is possible to make this script copy those files depending on month we have?? ( it should check time somehow )
I mean if we have january it will copy files to january subfolder.. if december to.. deceber..

this is way to difficult for me....

please help me : (
 
What code do you have so far, and where are you stuck?
 
Hey guitarzan...

My code is so simple that I ashamed ... ( I am really beginner )

Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\", OverwriteExisting


If this could somehow be able to check current month and then paste it into proper folder it would be great!
 
One way is to go through each file, get the modified date of the file (shown below), then compare that to the current date etc (not shown)

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\log1")

For Each objFile in objFolder.Files
   date1 = objFile.DateLastModified
Next

Below are other properties of the FSO File object:
 
hey I did it ! :) but here i have another problem and I don't understand it.

my script is working like I wanted .. so I made new task in task scheduler but now he is giving me 800A004C error... ( path not found) ??
when i click on it with mouse or run it in cmd it is working ....

any ideas??


========

ok just checked one thing... because this script suppose to copy files from my local drive to mapped drive
i changed path to local drive to other path in local drive and now it is working!.. that means
if i want to run this script in task scheduler i need to add some informations about this mapped drive i think...

any ideas??


Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")

If Month(Now()) = 1 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\jan", OverwriteExisting
elseif Month(Now()) = 2 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\feb", OverwriteExisting
elseif Month(Now()) = 3 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\mar", OverwriteExisting
elseif Month(Now()) = 4 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\apr", OverwriteExisting
elseif Month(Now()) = 5 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\may", OverwriteExisting
elseif Month(Now()) = 6 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\jun", OverwriteExisting
elseif Month(Now()) = 7 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\jul", OverwriteExisting
elseif Month(Now()) = 8 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\aug", OverwriteExisting
elseif Month(Now()) = 9 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\sep", OverwriteExisting
elseif Month(Now()) = 10 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\oct", OverwriteExisting
elseif Month(Now()) = 11 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\nov", OverwriteExisting
elseif Month(Now()) = 12 then
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\dec", OverwriteExisting
end if

C:\log1\*.* - local folder on local drive ( source )
Z:\iRT\IT\test\ - mapped drive (destination) ...
this z mapped drive ask for login and password with every new windows start. so probably there need to be also login and password saved in this script?

 
Yes, mapped drives will only exist when you are logged in... For your script to work in task scheduler, your options are to use a UNC path, or have your script create the mapped drive before the copies:

 
Instead of the 12 ifs you are using now consider a single line something like;

Code:
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\" & Format$(Now(), "mmm"), OverwriteExisting
 
A good point, though vbscript does not have a Format$ function. This would do the trick, though:
Code:
... "Z:\iRT\IT\test\" & Left(MonthName(Month(Now())), 3)
 
>vbscript does not have a Format$ function
It doesn't - but: thread329-1570966
 
I know there's a Choose command with VB for Office; if it's available, the code could look something like this:
Code:
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\"&Choose(Month(Now()), "jan", "feb", "mar", "apr", "jun", "jul", "aug", "sep", "oct", "nov", "dec") , OverwriteExisting
 
THANKS FOR SUPPORT !!!!! Everything is working now.. but I have big problem with Task Scheduler..

My Script is working when I will click on it two times ( I mean on icon ) but I need it to work with task scheduler
so I made new task..... and when task scheduler runs this script I get info 'PATH NOT FOUND'

but the path is correct! and like already said... clicking on it manually everything works fine..

This is definitely about mapped drive access... I need to write into this script some extra parameters which will
map this drive ????? or log me in ??????

( this drive is mapped on my pc from server in other room... I need to log into this drive with password every time after turning system on )

any body help pleasee :(( ????

I was trying already with ......"\\192.168.10.5\iRT\iRT\IT\test\may\"... instead of 'Z:'

and also :

dim objNetwork
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "Z:", " \\192.168.10.5\iRT", False, "user", "password"

but in this case task scheduler is giving 'the local device name has a remembered connection to another network resource'


and with:

Dim objNetwork, strRemoteShare
Set objNetwork = WScript.CreateObject("WScript.Network")
strRemoteShare = "\\192.168.10.5\iRT"
objNetwork.MapNetworkDrive "z:", strRemoteShare, False

'local device name is already in use'


ANYBODY? :(






 
Maybe try a different drive letter besides Z (one that is not being used), and when the script is done, use RemoveNetworkDrive:

I need to log into this drive with password every time after turning system on
Then use the username and password parameters, like you did in the first MapNetworkDrive you showed.
 
Guitarzan THANKSS!!!
your suggestion was very helpfull.. "IT'S AAALLIIIIVEE" [2thumbsup]... finally!
so it is :

MAPPING ( with other letter than Z)
SCRIPT
and REMOVE line at the end...

its going every 5 minutes now .. :)


....

Guitarzan if you have some more patience.. any idea how this script could mail me when copy process fails?



 
There are many ways to send emails, this page shows a few different methods that you can try out. (There are other ways besides this, including using Outlook)

As for finding out when one of the copy's fail, you would have to check the error object after each call, something like this:
Code:
[COLOR=blue]On Error Resume Next[/color] [COLOR=#4E9A06]'continue if we get a runtime error[/color]
objFSO.CopyFile "C:\log1\*.*" , "Z:\iRT\IT\test\jan", OverwriteExisting
[COLOR=blue]If Err.Number <> 0 Then
   wscript.echo "Got Err#" & Err.Number & ": " & Err.Description
   [/color][COLOR=#4E9A06]'append to a log, and send that email later[/color][COLOR=blue]
End if
On Error Goto 0[/color] [COLOR=#4E9A06]'stop execution if we get a runtime error.[/color]
 
Hey! it's me one more time... I learned a lot from your advices (thanks one more time)

so I deleted 12 ifs and added 3rd party powershell script to mail me if somethings goes wrong but I have
one problem... if source folder is empty i will also recieve email because script thinks this is error..
what command can I use to 'convince' my script that when folder is empty just to ignore this ???

Code:
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fs = CreateObject("Scripting.FileSystemObject")

Dim objShell
On Error Resume Next
objFSO.CopyFile "SOURCE PATH\*.*" , "DEST. PATH\" & Left(MonthName(Month(Now())), 3), OverwriteExisting
If Err.Number <> 0 Then
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "powershell -file ""PATH\mail.ps1"""   
End if
On Error Goto 0


There is also another thing that I cannot solve...

Im using OpenTextFile and GetFolder to make file list of copied content, but content of this created file is totally chaotic
for example file names in source folder can be a,b,c,d,1,2,3,4... and what he's writing inside is like 1,b,a,4,c,1....

is there any sort .. or alfabetic filter I can use ?



 
>what command can I use to 'convince' my script that when folder is empty just to ignore this
Look into the FileSystem Object's FolderExists Method. Also FileExists

Regarding sorting when iterating through the files you get from GetFolder, there is no sort filter built in. You would have to do the sorting yourself, for example saving the filenames in an array and sorting, or putting them in a database that can be sorted etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top