DosTips.com

A Forum all about DOS Batch
It is currently 30 Aug 2016 07:07

All times are UTC-06:00




Post new topic  Reply to topic  [ 8 posts ] 
Author Message
PostPosted: 27 Feb 2012 07:10 
Offline

Joined: 20 Feb 2012 10:45
Posts: 15
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


Top
   
PostPosted: 27 Feb 2012 07:27 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5914
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"


Top
   
PostPosted: 29 Feb 2012 04:39 
Offline

Joined: 20 Feb 2012 10:45
Posts: 15
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


Top
   
PostPosted: 29 Feb 2012 04:51 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5914
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??


Top
   
PostPosted: 29 Feb 2012 07:48 
Offline

Joined: 20 Feb 2012 10:45
Posts: 15
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?


Top
   
PostPosted: 29 Feb 2012 10:34 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3169
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.


Top
   
PostPosted: 29 Feb 2012 18:16 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5914
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


Top
   
PostPosted: 01 Mar 2012 03:20 
Offline

Joined: 20 Feb 2012 10:45
Posts: 15
Great stuff foxidrive!

thanks again. :D

Mark


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 8 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Matt Williamson and 5 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® Forum Software © phpBB Limited