Results 1 to 11 of 11
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Manipulate file name and save as (VBS/DOS(?))

    Are you sure that you posted the appropriate code? The version that you posted loops through the files in the output folder and its subfolders, but it doesn't do anything with the files. The input folder doesn't come into it at all.

  2. #2
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Manipulate file name and save as (VBS/DOS(?))

    I realised too late that I had sent the wrong code , that was a garbled attempt that obviously didn't work <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

    I think it best if I delete the code in the above and replace it with the current work...sorry
    Jerry

  3. #3
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Manipulate file name and save as (VBS/DOS(?))

    <P ID="edit" class=small>(Edited by Jezza on 01-Oct-08 16:47. To put the correct code in place)</P>I have been approached by a colleague who hs an unusual request that I am a tad uncomfy with the programming...well I don't know what the best one to go for, so maybe you can advise.

    He is sent a file with the following name format DDMMYYn.dat You may have guessed the format id D=Date, M=Month, Y= Year and n= an arbitrary number. He wishes to have a program that goes to a specified folder (say C:jezzas) and it to be copied to another folder (say C:jezzasbackup) in the following format RMCMBY20YYMMDD.1201

    As you can see the RMCMBY20 is appended as a prefix on all files outputted and the data format is manipulated as a string with .dat removed and replaced with .1201

    I wrote a piece of code but I have now found it copies ALL dat files on my hard disc to the output folder <img src=/S/flee.gif border=0 alt=flee width=25 height=25>

    Any suggestions on how to rejig my code or another method, I have been informed by him that it cannot be a freeware utility or other utility as he has a set of batch files running and they will interact.

    <pre>Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
    Dim Act : Set Act = CreateObject("Wscript.shell")
    Dim Sd : Sd = Act.ExpandEnvironmentStrings("%systemDrive%")
    Dim OldName, NewName, Path, TheFile , INTA
    On Error Resume Next
    INTA = 1-1
    strComputer = "."
    If Not Fso.FolderExists(Sd & "jezzasbackup") Then
    Fso.CreateFolder(Sd & "jezzasbackup")
    End If
    Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")
    Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile Where Extension = 'dat'")
    If colFiles.Count = 0 Then
    Act.popup "No file with dat extention found",4,"Invalid File Type", 0 + 32
    'Wscript.Quit
    End If
    For Each objFile in colFiles
    INTA = INTA + 1
    OldName = Ucase(objFile.FileName & "." & objFile.Extension)

    NewName= Ucase("RMCMBY20" & Mid(objFile.FileName,5,2) & Mid(objFile.FileName,5,2) & Left(objFile.FileName,2) & ".1201")

    'Path = UCase(objFile.Drive & objFile.Path)

    Path = Sd & "jezzas"
    TheFile = KbPath & Kbname
    Fso.CopyFile(Path & OldName), (Sd & "jezzas" & NewName)
    Act.Popup "Completed The Copy Of This" & vbCrLf & Path & Name & vbCrLf & "The File Count = " & INTA, 2, "Gsm Copy File", 0 + 32
    Next
    Act.Popup "Completed Search And Copy" & vbCrLf & "Total Amount Of Files Moved = " & INTA, 5, "Gsm Search And Copy"
    </pre>

    Jerry

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Manipulate file name and save as (VBS/DOS(?))

    The line

    Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile Where Extension = 'dat'")

    creates a collection of ALL files with extension .dat regardless of where they are because the WHERE clause only looks at the extension, not at the path.

    Why not use the good old Dir function to loop through the files in the specified folder? Using objWMIService seems overkill here.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Manipulate file name and save as (VBS/DOS(?))

    > Using objWMIService seems overkill here

    Agreed just I am clutching at straws
    Jerry

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Manipulate file name and save as (VBS/DOS(?))

    How about this?

    Sub Test()
    Const strInput = "C:Jezzas"
    Const strOutput = "C:JezzasBackup"
    Dim strFile As String
    Dim strNew As String
    strFile = Dir(strInput & "*.dat")
    Do While Not strFile = ""
    strNew = "RMCMBY20" & Mid(strFile, 5, 2) & _
    Mid(strFile, 5, 2) & Left(strFile, 2) & ".1201"
    Name strInput & strFile As strOutput & strNew
    strFile = Dir
    Loop
    End Sub

  7. #7
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Manipulate file name and save as (VBS/DOS(?))

    That works great, I lobbed it into Excel just to run it through but he runs all this code in batch files so how would I call this? Sorry I thought I had explained that bit above
    Jerry

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Manipulate file name and save as (VBS/DOS(?))

    If you change the declarations

    Dim ... As String

    to

    Dim ...

    the code should work in a VBS file, I think (I haven't tested it).

  9. #9
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Manipulate file name and save as (VBS/DOS(?))

    Hi Hans

    Back from work now, I can't work out why there is a problem on this line as the syntax appears correct:

    Name strInput & strFile As strOutput & strNew
    Jerry

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Manipulate file name and save as (VBS/DOS(?))

    My absolute lack of experience with VBScript shows here. The old BASIC file manipulation instructions such as Dir and Name are not available in VBScript, so we'll have to use Scripting.FileSystemObject after all. The following works for me in vb script:

    Const strInput = "C:Jezzas"
    Const strOutput = "C:JezzasBackup"
    Dim fso
    Dim fld
    Dim fil
    Dim strFile
    Dim strNew
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(strInput)
    For Each fil In fld.Files
    strFile = fil.Name
    If Right(strFile, 4) = ".dat" Then
    strNew = "RMCMBY20" & Mid(strFile , 5, 2) & _
    Mid(strFile , 5, 2) & Left(strFile , 2) & ".1201"
    fil.Move strOutput & strNew
    End If
    Next

  11. #11
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Manipulate file name and save as (VBS/DOS(?))

    Thank you, thank you, that will work a treat
    Jerry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •