removing dots from filenames

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

removing dots from filenames

#1 Post by mcranmer » 27 Feb 2012 07:10

Anyone have batch script to remove all dots (except the one preceding the file extension) from filenames, for all .txt files in a directory?

thanks all.

Mark

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

Re: removing dots from filenames

#2 Post by foxidrive » 27 Feb 2012 07:27

Untested: should print the ren command to the console which replaces dots with spaces. Remove the echo to make it do the actual rename if it looks right.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /b *.txt') do call :dot "%%a"
pause
goto :EOF
:dot
set "var=%~n1"
set "var=%var:.= %"
echo ren %1 "%var%%~x1"

mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

Re: removing dots from filenames

#3 Post by mcranmer » 29 Feb 2012 04:39

Hi foxidrive,

Your code worked fine, thanks.

I am attempting to work out a new problem for myself, but it is taking a while.

I want to replace any date-like string in either ddmmyy, dd.mm.yy, ddmmyyyy or dd.mm.yyyy in all filenames to be replaced with the current date in ddmmyy format.

I'll continue to play around with this, but feel free to throw some hints my way.

thanks for your support.

(do you know of any essential/recommended texts on this?)

Mark

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

Re: removing dots from filenames

#4 Post by foxidrive » 29 Feb 2012 04:51

mcranmer wrote:I want to replace any date-like string in either ddmmyy, dd.mm.yy, ddmmyyyy or dd.mm.yyyy in all filenames to be replaced with the current date in ddmmyy format.


Mark, you can't always tell which format is being used by numbers alone. EG: is 01.02.1999 dd.mm.yyyyy or mm.dd.yyyy ?

Even worse is 02.03.01 :)

You either have to know the source data to have an idea of the range of formats or there could be inaccuracy.


Can the filedate on the files be used??

mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

Re: removing dots from filenames

#5 Post by mcranmer » 29 Feb 2012 07:48

These are dates which have been manually added by various colleagues, past and present, and were not automatically generated dates, which explains their haphazard format. I know that the order is always day, month, year.

My assumption, however, is that the crucial thing is to strip out a string, which has a relatively small number of permutations, from the filenames. Then appended the current date. The naming convention of the files if fairly simple. Examples are:

12_PPRb_B7X_220212.txt
12_RP1_B7S_270211.txt
12_RP2_B7S_270211.txt
12_RP3_B7S_270211.txt
12_RP3_B7V_270211.txt
12_RP4_B7S_270211.txt
2P_PPN_20.07.11.txt
773_PPN_06.10.2011.txt
9O_PPN_08.08.11.txt
A9D_PPN_17.02.12.txt
A9E_PPN_17.02.12.txt
A9F_PPN_17.02.12.txt
A9G_PPN_17.02.12.txt
A9H_PPN_17.02.12.txt
B68_PPN_17.02.12.txt
C3G_PPN_24112011.txt


I am thinking the problem in terms of stripping out the dots (which won't have any adverse effects elsewhere in the filename) and replacing "\d{6,8}" (in regex terms) with todays date.

Does this make sense?

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

Re: removing dots from filenames

#6 Post by Squashman » 29 Feb 2012 10:34

Well once the dots are stripped out I think you could just test to see if the -7th or -9th position of the file name is an underscore and rename accordingly by trimming the string appropriately.

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

Re: removing dots from filenames

#7 Post by foxidrive » 29 Feb 2012 18:16

mcranmer wrote:I know that the order is always day, month, year.
The naming convention of the files if fairly simple. Examples are:

12_PPRb_B7X_220212.txt
12_RP1_B7S_270211.txt
12_RP2_B7S_270211.txt
12_RP3_B7S_270211.txt
12_RP3_B7V_270211.txt
12_RP4_B7S_270211.txt
2P_PPN_20.07.11.txt
773_PPN_06.10.2011.txt
9O_PPN_08.08.11.txt
A9D_PPN_17.02.12.txt
A9E_PPN_17.02.12.txt
A9F_PPN_17.02.12.txt
A9G_PPN_17.02.12.txt
A9H_PPN_17.02.12.txt
B68_PPN_17.02.12.txt
C3G_PPN_24112011.txt


I am thinking the problem in terms of [...] replacing "\d{6,8}" (in regex terms) with todays date.

Does this make sense?


Can you confirm that you want to strip out every date and replace it with today's date?

If so then the code below will help.

EDIT: It now counts the filename length so should be quicker.

Code: Select all

:: renames and replaces from the last "_" in a filename with _%d8%

@echo off
set d8=20120301
for /f "delims=" %%b in ('dir "*.txt" /b') do call :next "%%b"
pause
GOTO:EOF
:next
echo processing %1
pushd "%~dp1"
set "name=%~nx1"
set "num=0"
set "found="

:countlength
set /a num=num+1
call set "name2=%%name:~%num%,1%%"
if not "%name2%"=="" goto :countlength

:loop
set /a num=num-1
if %num% lss 0 goto :EOF
call set "name2=%%name:~%num%,1%%"
if "%name2%"=="_" set /a found=num-1
if not defined found goto :loop
call ren %1 "%%name:~0,%num%%%_%d8%%~x1"
popd
goto :EOF

mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

Re: removing dots from filenames

#8 Post by mcranmer » 01 Mar 2012 03:20

Great stuff foxidrive!

thanks again. :D

Mark

Post Reply