Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File not created or copied

45 views
Skip to first unread message

Michael Astrosky

unread,
Oct 15, 2012, 9:04:28 AM10/15/12
to
I ave a long filename with many extranneous characters in it that needs to be lessened and have yesterdays date appended in it. I figured that part out - thanks to Goolge. I need to then move the file from it present location to naother network location - this is the part I fail on repeatedly. I believe it is due to the file not actually being created but just a variable being defined. I have looked at this for so long I am not seeing the error. Can anyone point point my (probably) simply overlook?

I beleive all the parts you need are posted below -

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMessage = CreateObject("CDO.Message")
Set objShell = WScript.CreateObject("WScript.Shell")
Set objshell = CreateObject("Wscript.shell")

'On Error Resume Next

'Get current date
'-----------------------
strMonth = Month(Date)
If len(strMonth) = 1 Then
strMonth = "0" & strMonth
End if

strDay = Day(Date)
If Len(strDay) = 1 Then
strDay = "0" & strDay
End if

strYear = Year(Date)

'strHour = Hour(Time)

'strMinute = Minute(Time)

'strSecond = Second(Time)

strDate = strYear & strMonth & strDay

strPastDay = strDate -1
'-----------------------

'----------------------------------------------------------------------------
'Specify variables for Emails
'----------------------------------------------------------------------------
strScriptServer = "TECHOPSPC01"
strScriptPath = "\\TECHOPSPC01\C$\Scripts\CSI\Capture\..."
strScriptName = "DFX_Loans_Past_Due file Move to FSCHAUNI01"
'strToEmail = ""
'strCCEmail =
strProcessID = "[LPDR-01]"
strCustomerImpact = "LOW"
strCorporateImpact = "LOW??"
'----------------------------------------------------------------------------

'Specify variables for File Paths
'strFromPath1 = "\\techopspc01\C$\CSIeSafeDFX\receive"
'strToPath1 = "\\fschauni01\group_share\special assets retail\special assets\past due report\"
strFromPath1 = "C:\CSIeSafeDFX\receive"
strToPath1 = "c:\treasury\Public funds\"
'Specify variable for File
strFile1 = "DFX_Loans Past Due by PDR*.xls"
strNewFile1 = "DFX_LoansPastDueByPDR"
'#######################'


If Right(strFromPath1, 1) <> "\" Then
strFromPath1 = strFromPath1 & "\"
End If


objShell.Run "cmd /c move ""C:\CSIeSafeDFX\receive\DFX_Loans Past Due by PDR*.xls"" C:\CSIeSafeDFX\receive\DFX_LoansPastDueByPDR"
Wscript.Sleep 4000
strNewFile2 = "DFX_Loans Past Due By PDR" & strPastDay & ".xls"
strNewFile2 = strNewFile1 & strPastDay & ".xls"
objFSO.CopyFile strFromPath1 & strNewFile2, strToPath1, True
' objFSO.DeleteFile strFromPath1 & NewFile1
blnEmailNotification = true


Thank you all.

Mayayana

unread,
Oct 15, 2012, 11:01:59 AM10/15/12
to
The code you have is rather convoluted. In the docs
for MOVE I don't see anything about using wildcards.
Nor does it say anything about specifying a folder in
the Destination parameter.
But why are you trying to use DOS (console code)
anyway? You're calling WSHShell to run a DOS command
and then calling FSO to do the same thing! DOS is for
console windows.

When I try your code I get that the source and
destination are:

C:\CSIeSafeDFX\receive\DFX_LoansPastDueByPDR.xls
c:\treasury\Public funds\

That looks fine. If it's failing you should be getting
an error, like "file not found". Your post implies that
you're not seeing an error code. If I were you I'd clean
it up, get the CMD code out of there, and use
FSO.FileExists where necessary so that you can check
where the operation fails.

--
--
"Michael Astrosky" <mast...@gmail.com> wrote in message
news:e12ceae3-bcb4-4589...@googlegroups.com...

Michael Astrosky

unread,
Oct 15, 2012, 12:50:25 PM10/15/12
to
Thanks for the reply.

The DOS code is there because the orignial file name is DFX_Loans past Due by PDR011833(and then a bunch of miscellaneous characters that always change).xls

what I need to do is change the file name to DFXLoansPastDueByPRD_10152012.xls and then copy that file to another location.

this file comes in everyday and needs yesterday's date appended to it and then moved. I could find no way with VB to rename a file with wildcards.

The code runs, renames the original file to DFX_LoansPastDueBy and then eventually exits. When I debug it I get fil not found when trying to copy to the new location. The variable strNewFile2 does get set to DFX_LoansPastDueBy_10142012.xls but I do not think it is actually created for the move to work.

that is where I am stuck.

Mayayana

unread,
Oct 15, 2012, 3:13:46 PM10/15/12
to

> I could find no way with VB to rename a file with wildcards.

Below is "air code" but it should be about right. CopyFile
can actually deal with wildcards but since you just have one
file to deal with I thought it might be easier to just avoid
that complication.

Set oFol = FSO.GetFolder("C:\CSIeSafeDFX\receive")
Set oFils = oFol.Files
For each oFil in oFils
sName = oFil.Name
If Instr(oFil.Name, "DFX_Loans Past Due by PDR") > 0 then
oFil.Name = "DFX_LoansPastDueBy_10142012" & sDate & ".xls"
oFil.Copy strToPath1 & oFil.Name, True
oFil.Delete
Exit For
End if
Next
Set oFils = Nothing
Set oFol = Nothing

That code uses the File object. Similar operations can be done
doing the copy, delete, etc. via FSO. It doesn't much matter.
But I think either way is more clear than bringing cmd.exe into
it. This way you can use FSO for any debugging needed, you
don't have to see phantom DOS windows, and you can skip
WSHShell.

You might want to download the WSH help file. It details the
various FSO operations available:

http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9


Todd Vargo

unread,
Oct 16, 2012, 2:10:45 AM10/16/12
to
Here is a simplification of the relevant parts of your code. You can
merge with whatever other code you deem necessary. This will rename the
file and move it per your code spec. I left out extension verification
because I felt it unnecessary.

Please note, the correct method of working with current date and time to
to first store them in a variable, and then work with the variable
because it will not change while the script is running. This code has
not been tested with UNC paths. You will have to do that on your end.



Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objshell = CreateObject("Wscript.shell")

'On Error Resume Next
'Do not use OERN unless error handling code is included

'Get yesterday's date
'-----------------------
d = Date - 1
strMonth = Right("0" & Month(d),2)
strDay = Right("0" & Day(d),2)
strYear = Year(d)

'strHour = Hour(Time)
'strMinute = Minute(Time)
'strSecond = Second(Time)

strPastDay = strYear & strMonth & strDay
'-----------------------

'----------------------------------------------------------------------
'Specify variables for File Paths
strFromPath1 = "C:\CSIeSafeDFX\receive"
strToPath1 = "c:\treasury\Public funds\"

'Specify variable for original Filename prefix and new name
strFile1 = "DFX_Loans Past Due by PDR" ' *.xls
strNewFile1 = "DFX_LoansPastDueByPDR" & strPastDay & ".xls"
'----------------------------------------------------------------------

Set f = objFSO.GetFolder(strFromPath1)
Set fc = f.Files
moved = false
For Each f1 in fc
s = f1.name
If Left(Ucase(s),Len(strFile1)) = Ucase(strFile1) Then
f1.Name = strNewFile1
objFSO.MoveFile f1.Path, strToPath1
moved = True
Exit For
End If
Next

If moved Then
MsgBox "File [" & strFile1 & "*.*] renamed and moved to [" & _
strToPath1 & "\" & strNewFile1 & "].", vbInformation
Else
MsgBox "File [" & strFile1 & "*.*] not found.", vbExclamation
End If

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Dr J R Stockton

unread,
Oct 16, 2012, 1:27:40 PM10/16/12
to
In microsoft.public.scripting.vbscript message <e12ceae3-bcb4-4589-a6b4-
8125f8...@googlegroups.com>, Mon, 15 Oct 2012 06:04:28, Michael
Astrosky <mast...@gmail.com> posted:

>strMonth = Month(Date)
>If len(strMonth) = 1 Then
> strMonth = "0" & strMonth
>End if
>
>strDay = Day(Date)
>If Len(strDay) = 1 Then
> strDay = "0" & strDay
>End if
>
>strYear = Year(Date)
>
>'strHour = Hour(Time)
>
>'strMinute = Minute(Time)
>
>'strSecond = Second(Time)
>
>strDate = strYear & strMonth & strDay


That approach should fail if the time in minutes changes during the
execution of that code. The potency of the failure increases with the
magnitude of the biggest field to change. You should put ItIs = Now
at the beginning, and use ItIs as the argument for Year..Second. You
probably will not notice the effect in test, but it will cause
occasional havoc in production runs.

--
(c) John Stockton, nr London UK Reply address via Home Page.
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
0 new messages