batch file help

Archived from groups: microsoft.public.windowsxp.general (More info?)

I am trying to write a batch file to strip the 4 leading characters from the
name of all files in a directory. I tried using rename but was not having any
luck getting the pattern matching to strip the characters off. Please let me
know if anyone knows a way to do this in a batch file.
6 answers Last reply
More about batch file help
  1. Archived from groups: microsoft.public.windowsxp.general (More info?)

    EricP wrote:

    > I am trying to write a batch file to strip the 4 leading
    > characters from the name of all files in a directory. I tried
    > using rename but was not having any luck getting the pattern
    > matching to strip the characters off. Please let me know if
    > anyone knows a way to do this in a batch file.
    Hi

    You can use a VBScript (.vbs file) to do this:


    '--------------------8<----------------------

    ' Folder where files are located
    sFolderPath = "c:\test"

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    ' Get all file names that are 5 characters or
    ' more (excluding file extension) into an Array

    arFiles = Array()
    Set oFolder = oFSO.GetFolder(sFolderPath)
    For Each oFile In oFolder.Files
    If Len(oFSO.GetBaseName(oFile.Name)) >= 5 Then
    iCount = UBound(arFiles) + 1
    ReDim Preserve arFiles(iCount)
    Set arFiles(iCount) = oFile
    End If
    Next

    For i = 0 To UBound(arFiles)
    sFileName = arFiles(i).Name
    sBaseName = oFSO.GetBaseName(sFileName)
    sFileExtension = ""
    If InStr(sFileName, ".") > 0 Then
    sFileExtension = "." & oFSO.GetExtensionName(sFileName)
    End If
    sNewBaseName = LTrim(Mid(sBaseName, 5))
    If sNewBaseName <> "" Then
    sNewName = sNewBaseName & sFileExtension
    On Error Resume Next
    arFiles(i).Name = sNewName
    If Err.Number <> 0 Then
    WScript.Echo "Error when renaming " & sFileName & " to " & sNewName _
    & vbCrLf & "Reason: " & Err.Description
    End If
    Else
    WScript.Echo "Not possible to rename: " & sFileName
    End If

    Next

    MsgBox "Done!", vbSystemModal+vbInformation, "Rename files"

    '--------------------8<----------------------


    --
    torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
    Administration scripting examples and an ONLINE version of
    the 1328 page Scripting Guide:
    http://www.microsoft.com/technet/scriptcenter/default.mspx
  2. Archived from groups: microsoft.public.windowsxp.general (More info?)

    "EricP" <EricP@discussions.microsoft.com> wrote in message
    news:9E516A25-879D-4251-960B-BF8D58DD7AA5@microsoft.com...
    >I am trying to write a batch file to strip the 4 leading characters from
    >the
    > name of all files in a directory. I tried using rename but was not having
    > any
    > luck getting the pattern matching to strip the characters off. Please let
    > me
    > know if anyone knows a way to do this in a batch file.

    you could try asking in 'microsoft.public.win2000.cmdprompt.admin' - they're
    experts at this sort of thing.
    bob
  3. Archived from groups: microsoft.public.windowsxp.general (More info?)

    >"EricP" <EricP@discussions.microsoft.com> wrote in message

    >>I am trying to write a batch file to strip the 4 leading characters from
    >>the name of all files in a directory. I tried using rename but was not
    >>having any luck getting the pattern matching to strip the characters.
    >>Please let me know if anyone knows a way to do this in a batch file.

    I'd use CKRename (a free tool that's great at that sort of thing)
    interactively, but that's not batch file automation.

    On batch files, I'd create a disposable set of files and try something
    like Ren ????*.* *.* to see if that would work... nope.

    OK, next is to look up the For statement, especially the NT
    enhancements to it - it can do pretty kewl things now. Let's see...

    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx

    You can use For to parse all files in a dir, or an entire subtree.
    That gets you the file( name)s, but I still can't see how to parse the
    strings. So I'd Google for a suitable command line utility...

    http://www.google.co.za/search?q=command+line+file+rename+utility+download

    Lots of tools, but not sure which can be used from batch files.


    >------------ ----- ---- --- -- - - - -
    The most accurate diagnostic instrument
    in medicine is the Retrospectoscope
    >------------ ----- ---- --- -- - - - -
  4. Archived from groups: microsoft.public.windowsxp.general (More info?)

    cquirke (MVP Windows shell/user) wrote:

    >>"EricP" <EricP@discussions.microsoft.com> wrote in message
    >
    >
    >>>I am trying to write a batch file to strip the 4 leading characters from
    >>>the name of all files in a directory. I tried using rename but was not
    >>>having any luck getting the pattern matching to strip the characters.
    >>>Please let me know if anyone knows a way to do this in a batch file.
    >
    >
    > I'd use CKRename (a free tool that's great at that sort of thing)
    > interactively, but that's not batch file automation.
    >
    > On batch files, I'd create a disposable set of files and try something
    > like Ren ????*.* *.* to see if that would work... nope.
    >
    > OK, next is to look up the For statement, especially the NT
    > enhancements to it - it can do pretty kewl things now. Let's see...
    >
    > http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx
    >
    > You can use For to parse all files in a dir, or an entire subtree.
    > That gets you the file( name)s, but I still can't see how to parse the
    > strings.

    Here's one way:

    @echo off
    set tst=12345678
    :: Display the 5th and 6t character
    echo %tst:~4,2%
    :: Display all characters from the 5th character
    echo %tst:~4%


    > So I'd Google for a suitable command line utility...
    >
    > http://www.google.co.za/search?q=command+line+file+rename+utility+download
    >
    > Lots of tools, but not sure which can be used from batch files.
    >
    >
    >
    >
    >
    >
    >>------------ ----- ---- --- -- - - - -
    >
    > The most accurate diagnostic instrument
    > in medicine is the Retrospectoscope
    >
    >>------------ ----- ---- --- -- - - - -


    --
    torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
    Administration scripting examples and an ONLINE version of
    the 1328 page Scripting Guide:
    http://www.microsoft.com/technet/scriptcenter/default.mspx
  5. Archived from groups: microsoft.public.windowsxp.general (More info?)

    On Sun, 14 Aug 2005 19:46:51 +0200, "Torgeir Bakken \(MVP\)"
    >cquirke (MVP Windows shell/user) wrote:
    >>>"EricP" <EricP@discussions.microsoft.com> wrote in message

    >@echo off
    >set tst=12345678
    >:: Display the 5th and 6t character
    >echo %tst:~4,2%
    >:: Display all characters from the 5th character
    >echo %tst:~4%

    Arsome, dude! Where did you find that? I didn't see it in...

    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx

    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx

    ....etc.? Got any more?


    >------------------------ ---- --- -- - - - -
    Forget http://cquirke.blogspot.com and check out a
    better one at http://topicdrift.blogspot.com instead!
    >------------------------ ---- --- -- - - - -
  6. Archived from groups: microsoft.public.windowsxp.general (More info?)

    cquirke (MVP Windows shell/user) wrote:

    > "Torgeir Bakken \(MVP\)" wrote:
    >
    >>@echo off
    >>set tst=12345678
    >>:: Display the 5th and 6t character
    >>echo %tst:~4,2%
    >>:: Display all characters from the 5th character
    >>echo %tst:~4%
    >
    >
    > Arsome, dude! Where did you find that? I didn't see it in...
    >
    > http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx
    >
    > http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx
    >
    > ...etc.? Got any more?
    >
    Hi,

    I guess I have picked it up from posts in the newsgroup
    microsoft.public.win2000.cmdprompt.admin

    Running set /? in a command prompt will give you more on this.


    --
    torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
    Administration scripting examples and an ONLINE version of
    the 1328 page Scripting Guide:
    http://www.microsoft.com/technet/scriptcenter/default.mspx
Ask a new question

Read More

Microsoft Windows XP