View unanswered posts | View active topics It is currently 28 Nov 2014 03:31



Post new topic Reply to topic  [ 8 posts ] 
removing dots from filenames 
Author Message

Joined: 20 Feb 2012 10:45
Posts: 15
Post removing dots from filenames
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


27 Feb 2012 07:10
Profile

Joined: 10 Feb 2012 02:20
Posts: 4424
Post Re: removing dots from filenames
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:
@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"


27 Feb 2012 07:27
Profile

Joined: 20 Feb 2012 10:45
Posts: 15
Post Re: removing dots from filenames
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


29 Feb 2012 04:39
Profile

Joined: 10 Feb 2012 02:20
Posts: 4424
Post Re: removing dots from filenames
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??


29 Feb 2012 04:51
Profile

Joined: 20 Feb 2012 10:45
Posts: 15
Post Re: removing dots from filenames
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?


29 Feb 2012 07:48
Profile

Joined: 23 Dec 2011 13:59
Posts: 2022
Post Re: removing dots from filenames
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.


29 Feb 2012 10:34
Profile

Joined: 10 Feb 2012 02:20
Posts: 4424
Post Re: removing dots from filenames
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:
:: 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


29 Feb 2012 18:16
Profile

Joined: 20 Feb 2012 10:45
Posts: 15
Post Re: removing dots from filenames
Great stuff foxidrive!

thanks again. :D

Mark


01 Mar 2012 03:20
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: Baidu [Spider], bars143, Bing [Bot], Google [Bot], Yahoo [Bot] and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.