rename files in a directory in a batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
franklins
Posts: 2
Joined: 04 Sep 2019 01:36

rename files in a directory in a batch

#1 Post by franklins » 04 Sep 2019 01:53

I have a long list of file names in a directory called %cd%/test2 a portion of which shown below
I need to make the file names conform to 8.3 standard length and still retain some information so I can validate them
I need to parse them in a dos batch so they are valid when I open up my program in foxprox they can be read
running in windows 7 through 10

here are some examples of file names
DCT-284489-0BANR-01Aug2019 (2).txt for this file ideally I would rename it DC284489.819 where the 819 in the extension stands for august 2019 which is in the name
DCT-284489-0BANR-01Jul2019.txt
DCT-284489-0BANR-01Jun2018.txt
PE284489 (1).348 and for this file PE284489.348 just remove the (1)
PF284489.798
PG284489.192
PH284489 (1).628
PI284489 (1).177
PJ284489.557
PK284489.058
PL284489.470

there will be at max 100 files in the folder

much appreciated

franklins
:D

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

Re: rename files in a directory in a batch

#2 Post by aGerman » 04 Sep 2019 16:41

You should exactly define the rules. Even your first example is already ambiguous. The name seems to be separated using dashes which divide it into 4 substrings.
Are there always 4 substrings?
Is the first substring at least 2 characters long and the second always 6?
Does always the 4th substring contain the date?
What if the month is Oct, Nov, or Dec which would result in a number of two digits?

Steffen

franklins
Posts: 2
Joined: 04 Sep 2019 01:36

Re: rename files in a directory in a batch

#3 Post by franklins » 04 Sep 2019 20:25

steffen you make a good point regarding the extension I must have been thinking 1-0 and A and B is 12 months ! LOL
The conversion of the date to a number makes the work a lot more complex
I like knowing what the file is just by looking at it - however I actually extract the date from the inside of the file not from the name
So its not worth the work to move the date into the extension so lets forget that

I am a foxpro for dos programmer and I am writing a program using vdos as an emulator
Normally I would dir /b >file.txt and then parse the text and rename the files within foxpro
Unfortunately I cannot as dir /b does not display properly in program mode in foxpro nor does it work directly in vdos
So I hope to run a dos preprocessor that fixes the files and then calls vdos and foxpro so it is transparent to the user
I am still going to validate the files before I run them once I can get their names loaded up so as long as the file name meets 8,3 and is unique it doesn't matter if they give me a wrong file

these are the 4 types of files
I need to be able to deal with these cases
Another variation is the addition of the bracket (1 ) (2) which happens when the file is downloaded more than once

RCX-284489-0BANR-S-6Aug2019.xml
PF284489.798
DCT-284489-0BANR-01Jul2019.txt
LIOU3843.019

If I was writing this in foxpro I would copy it to a txt file
append it to a data file
go through the file and rename as I went

run dir /b %cd%/test2/*.* > fred.txt
use holder ( a database file) with a field called holder (50) CHARACTERS
append from fred.txt sdf && standard data format essentially just txt this loads up the entire directory
go top
do while .not. eof()
do case
case eof()
loop
case length(alltrim(holder))=12 && the case that the filename is fine already
case holder= "RC" && the RCX case above
fred=%RANDOM% * 998 / 32768 + 100 && i would use the dos random function to generate a random number 100-998
shortname=substr(holder,1,2) + substr(holder,4,6)+"."+fred && this takes the RC and the 284489 and this gets around the date issue shortname will be RC284489.xxx
run rename (holder) (shortname)
case holder="DC"
fred=%RANDOM% * 100 / 32768 + 100 && another random number
shortname=substr(holder,1,2) + substr(holder,4,6)+"."+fred
run rename (holder) (shortname)
case length(allt(holder))<17 && most likely a correct file with a (1)
fred=%RANDOM% * 100 / 32768 + 100 && another random number - in case this file duplicates another file name already in by mistake they downloaded it twice
shortname=substr(holder,1,8)+"."+fred
run rename (holder) (shortname)
case length(allt(holder))>17 && likely junk
shortname=substr(holder,1,8)+".err"
run rename (Holder) (shortname)
endcase
skip
enddo
now hopefully all the files are 8 3 and I reprocess them inside my program for validity

Is that better ??

franklins

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: rename files in a directory in a batch

#4 Post by Aacini » 05 Sep 2019 05:14

Your problem description is confusing an incomplete. You should describe the problem using plain English instead of examples or code segments. If a real filename does not match the examples shown, the method may fail. The code segments may lead to wrong assumptions. For example, I don't know Foxpro, so I have assumed several points. I assumed that "DC" and "RC" cases should be treated in the same way. You indicated that "there are 4 types of files", but a same file may belong to two different "cases" (like "RC" and "(1)"). I don't understand why you use %random% and you don't explain it.

Anyway...

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Initialize variables used for date conversion
for %%a in ("Jan=1" "Feb=2" "Mar=3" "Apr=4" "May=5" "Jun=6"
            "Jul=7" "Aug=8" "Sep=9" "Oct=0" "Nov=A" "Dec=B") do set %%a
set "RCorDC=|RC|DC|"

REM dir /B test2/*.* > fred.txt
for /F "delims=" %%f in (fred.txt) do (
   set "file=%%f"

   rem Remove a " (#)" in *the last part* of filename
   if "!file:~-8,2!" equ " (" set "file=!file:~0,-8!!file:~-4!"

   if "!file:~12!" neq "" (

      for %%a in ("!file:~0,2!") do if "!RCorDC:|%%~a|=!" neq "%RCorDC%" (
         rem Case "RC" and "DC": date at end
         rem Convert the first part as characters 1-2 and 5-10
         rem Convert the Mon20YY.txt part into 3 characters: 1-0 and A and B is 12 months + YY
         for %%m in ("!file:~-11,3!") do (
            set "file=!file:~0,2!!file:~4,6!.!%%~m!!file:~-6,2!"
         )
       )

   )

   if "!file!" neq "%%f" (
      echo Rename "%%f" to "!file!"
   ) else (
      echo File "%%f" is correct
   )
)
Output example:

Code: Select all

Rename "DCT-284489-0BANR-01Aug2019 (2).txt" to "DC284489.819"
Rename "DCT-284489-0BANR-01Jul2019.txt" to "DC284489.719"
Rename "DCT-284489-0BANR-01Jun2018.txt" to "DC284489.618"
Rename "PE284489 (1).348" to "PE284489.348"
File "PF284489.798" is correct
File "PG284489.192" is correct
Rename "PH284489 (1).628" to "PH284489.628"
Rename "PI284489 (1).177" to "PI284489.177"
File "PJ284489.557" is correct
File "PK284489.058" is correct
File "PL284489.470" is correct
Rename "RCX-284489-0BANR-S-6Aug2019.xml" to "RC284489.819"
File "PF284489.798" is correct
Rename "DCT-284489-0BANR-01Jul2019.txt" to "DC284489.719"
File "LIOU3843.019" is correct
Antonio

Post Reply