Remove zero if file name start with zero batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cumhur_28
Posts: 22
Joined: 22 Jul 2014 13:03

Remove zero if file name start with zero batch

#1 Post by cumhur_28 » 31 Oct 2014 04:44

Hello Everbody
Thanx for your previous helps. I have another question.
I have file names and like months 052014,102014 i need if the first character is zero, remove this character.
İnput:052014
Output52014
like that.
is taht any way to do it.

Thanx for your helps


Cumhur

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Remove zero if file name start with zero batch

#2 Post by Yury » 31 Oct 2014 05:04

Code: Select all

@for /f "delims=" %%i in ('dir /a-d/b') do @for /f "tokens=* delims=0" %%j in ("%%i") do @ren "%%i" "%%j"

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Remove zero if file name start with zero batch

#3 Post by Squashman » 31 Oct 2014 05:28

Yury, I believe they only want to remove the leading Zero. Not all zeros.

EDIT: Hmm, I guess that does work. Was thinking you needed to use TOKENS=1* but forgot about how it treats leading delimiters.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove zero if file name start with zero batch

#4 Post by foxidrive » 31 Oct 2014 05:35

Code: Select all

@echo off
type "file.txt" | repl "^0" "" >"newfile.txt"


This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Remove zero if file name start with zero batch

#5 Post by Yury » 31 Oct 2014 05:50

Squashman wrote:Yury, I believe they only want to remove the leading Zero. Not all zeros.


Squashman, this construction is designed to remove only the leading zero (zeros). Try it!

cumhur_28
Posts: 22
Joined: 22 Jul 2014 13:03

Re: Remove zero if file name start with zero batch

#6 Post by cumhur_28 » 31 Oct 2014 08:15

firstly thanx for your helps and sorry for my english i actually means, remove directories first character

thanx for your helps again


Image

to 52014
Last edited by cumhur_28 on 31 Oct 2014 08:19, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove zero if file name start with zero batch

#7 Post by foxidrive » 31 Oct 2014 08:19

Test this on a directory of test folders - if any of them start with two or more 0 then let me know.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /d %%a in (0*) do (
   set "folder=%%~nxa"
   ren "%%a" "!folder:~1!"
)

cumhur_28
Posts: 22
Joined: 22 Jul 2014 13:03

Re: Remove zero if file name start with zero batch

#8 Post by cumhur_28 » 31 Oct 2014 08:26

foxidrive wrote:Test this on a directory of test folders - if any of them start with two or more 0 then let me know.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /d %%a in (0*) do (
   set "folder=%%~nxa"
   ren "%%a" "!folder:~1!"
)



thank you so much it works. :)

Post Reply