Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: dos command to strip leading zeros  (Read 10203 times)

0 Members and 1 Guest are viewing this topic.

smeezekitty

  • Guest
dos command to strip leading zeros
« on: September 06, 2009, 05:33:35 PM »
i have files and i want them renames as such:
p000001.gif -> p1.gif
p000002.gif -> p2.gif
p000003.gif -> p3.gif
p000004.gif -> p4.gif
p000005.gif -> p5.gif
p000006.gif -> p6.gif
p000007.gif -> p7.gif
p000008.gif -> p8.gif
p000009.gif -> p9.gif
p000010.gif -> p10.gif
does anybody have a batch file or dos command (better) to do it?

gh0std0g74



    Apprentice

    Thanked: 37
    Re: dos command to strip leading zeros
    « Reply #1 on: September 06, 2009, 07:16:13 PM »
    Code: [Select]
    Set objFS=CreateObject("Scripting.FileSystemObject")
    strFolder = "c:\test"
    Set objFolder = objFS.GetFolder(strFolder)
    For Each strFile In objFolder.Files
    If objFS.GetExtensionName(strFile) = "gif" Then
    strFileName  = strFile.Name
    strNum = Mid(strFileName,2,6)
    strNum = strip(strNum)
    strNewFileName = Mid(strFileName,1,1) & strNum & Mid(strFileName,8)
    strFile.Name strNewFileName
    End If
    Next

    Function strip(str)
        s=""
    For i=1 To Len(str)
    If Mid(str,i,1) <> "0" Then
    s=s&Mid(str,i)
    Exit For
    End If
    Next
    strip=s
    End Function

    Code: [Select]
    c:\test> cscript /nologo myscript.vbs

    smeezekitty

    • Guest
    Re: dos command to strip leading zeros
    « Reply #2 on: September 06, 2009, 07:21:45 PM »
    didnt work ::
    it didnt change anything

    smeezekitty

    • Guest
    Re: dos command to strip leading zeros
    « Reply #3 on: September 06, 2009, 07:26:40 PM »
    oh wait i didnt notice it the first time:
    there  was an error message
    Microsoft VBScript runtime error object does no
    support this property or method: 'strFile.Name'

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: dos command to strip leading zeros
      « Reply #4 on: September 06, 2009, 07:31:28 PM »
      the problem area is
      Code: [Select]
      strFile.Name strNewFileName i will leave it to you to find out. Hint: an operator is missing

      smeezekitty

      • Guest
      Re: dos command to strip leading zeros
      « Reply #5 on: September 06, 2009, 07:35:38 PM »
      i tried = but it didnt work
      i dont like vb or vb script so i dont know
      never mind i got it -- there was a conflicting file in there

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: dos command to strip leading zeros
      « Reply #6 on: September 06, 2009, 07:43:31 PM »
      Here is a site that has lots of string manipulation for DOS.
      http://www.dostips.com/DtTipsStringManipulation.php

      From that site an example to replace a word in a line of text.
      Code: [Select]
      set str=teh cat in teh hat
      echo.%str%
      set str=%str:teh=the%
      echo.%str%
         
      DOS Script Ouput
      Quote
      teh cat in teh hat
      the cat in the hat

      If you can understand that, then you can get rid of leading zeros by recursion. Or iteration.








      smeezekitty

      • Guest
      Re: dos command to strip leading zeros
      « Reply #7 on: September 06, 2009, 08:02:38 PM »
      Here is a site that has lots of string manipulation for DOS.
      http://www.dostips.com/DtTipsStringManipulation.php

      From that site an example to replace a word in a line of text.
      Code: [Select]
      set str=teh cat in teh hat
      echo.%str%
      set str=%str:teh=the%
      echo.%str%
         
      DOS Script Ouput
      If you can understand that, then you can get rid of leading zeros by recursion. Or iteration.

      thats neat







      billrich

      • Guest
      Re: dos command to strip leading zeros
      « Reply #8 on: September 06, 2009, 08:05:49 PM »
      http://www.robvanderwoude.com/battech_leadingzero.php

      Code: [Select]
      :Loop
      IF "%Var:~0,1%"=="0" (
        SET Var=%Var:~1%
        GOTO Loop
      )