renaming extention of a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joe
Posts: 35
Joined: 06 Sep 2017 07:56

renaming extention of a file

#1 Post by joe » 16 Sep 2017 00:33

I have 1000's of files without extensions. but these files are named as below

xxxxxxxxxxxxxxxx12345-xxxxxxxxxxxxxxxxxxxxx
or
xxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxx12345

my request is

search for files containing 12345 in their names and rename them with unique
names of xxxxxxxxxx.txt (10 Char name) with .txt extension.
source and destination folder's are the same E:\test

is this possible

if so please help
thanx in advance

joe

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: renaming extention of a file

#2 Post by aGerman » 16 Sep 2017 04:36

joe wrote:files without extensions ... search for files containing 12345 in their names

Code: Select all

for /f "delims=" %%i in ('dir /a-d /b "E:\test\*12345*"') do if "%%~xi"=="" echo %%i
So far not a big deal. But ...
joe wrote:...rename them with unique
names of xxxxxxxxxx.txt (10 Char name) with .txt extension
... isn't clear to me. Not even a numeric name would be easy to calculate because the the biggest number in batch is 2147483647 which is already 10 digits.

Steffen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: renaming extention of a file

#3 Post by Compo » 16 Sep 2017 04:49

I'll start you off, this will find files with your required patterns:

Code: Select all

@Echo Off
If /I Not "%CD%"=="E:\test" (PushD "E:\test" 2>Nul||Exit/B)
For /F "Delims=" %%A In ('Where .:"????????????????12345-?????????????????????"^
 "????????????????12345-?????????????????????"') Do If "%%~xA"=="" Echo [%%A]
Timeout -1
All you need to do now is determine how you intend to create unique ten character file names and change

Code: Select all

Echo [%%A]
to

Code: Select all

Ren "%%A" "UniqueName.txt"
Depending upon how the files are generated you could possibly use a combination of date and time to generate the unique names.

joe
Posts: 35
Joined: 06 Sep 2017 07:56

Re: renaming extention of a file

#4 Post by joe » 16 Sep 2017 08:45

ok steffen any name as u suggested.. but should not repeat over writing files

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: renaming extention of a file

#5 Post by aGerman » 16 Sep 2017 09:09

I'm afraid I wasn't clear enough. A 10 digits number is already limited in batch. So if you would say 9 digits are sufficient it would be much easier. Also consider Compo's suggestion. The file time could be unique enough (and could be composed to a 10 digits file name) if you could make sure there don't exist 2 or more files with "modified" time stamp within the same minute. This way you also don't need to iterate over all files to find the latest number.

Steffen

Post Reply