strip characters, move beginning to end, fix extension

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thisazlife
Posts: 1
Joined: 16 Oct 2017 17:53

strip characters, move beginning to end, fix extension

#1 Post by thisazlife » 16 Oct 2017 18:00

Currently I use a set of batch files to clean and rename filenames. They come to me with added extension information I don’t need, the date on the front of the file and I want it at the end of the file so I can sort by file name, and remove all the extra brackets and underscoring.

I know there must be a better way to run this and possibly cleaner and more efficient. Any thoughts? TIA - Steve

Filename examples BEFORE I clean them:
[20171013]_[FILENAME_A]_[120.00]_[1234456].txt.99980
[20171013]_[FILENAME_b]_[101.00]_[1273456].txt.99913
[20171014]_[FILENAME_c]_[100.25]_[1239456].txt.121980

Filename exampes AFTER I clean them:
FILENAME-A-120.00-1234456-20171013.txt
FILENAME-B-101.00-1273456-20171013.txt
FILENAME-C-100.25-1239456-20171014.txt



I start with FixFileName.bat and then call each so if any error it stops. I’m sure there are 100 better ways…which is why I’m asking for help.

FIXFILENAME.bat
-----------------------------------------

Code: Select all

START /WAIT  REMOVECHAR.bat
START /WAIT  FIXEXT.bat
START /WAIT  MOVEDATE.bat
START /WAIT  ADDTXT.bat


REMOVECHAR.bat – Strips characters from file
----------------------------------------------------------------------------

Code: Select all

for /f "delims= " %%a in ('dir /a:-d /o:n /b') do call :next "%%a"
EXIT
GOTO:EOF
:next
set "newname=%~nx1"
set "newname=%newname:]=%"
set "newname=%newname:[=%"
set "newname=%newname:__=-%"
set "newname=%newname:_=-%"
set "newname=%newname:EANW=%"

ren %1 "%newname%


FIXEXT.bat – makes a copy of the files and removes the garbage from the end of the file
---------------------------------------------------------------------------------------------------------------------

Code: Select all

COPY K:\EDI\GWEDI\REMITS\TEMP\*--* K:\EDI\GWEDI\REMITS\TEMP\XTEMP\*-
pause
DEL /F /Q K:\EDI\GWEDI\REMITS\TEMP\*
pause
exit
MOVEDATE.bat – then I strip off the date at the beginning of the file and move it to the end of the file.
SETLOCAL EnableDELAYedeXpansion
for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set F=%%a
ren "%%a" "!F:~9!!F:~,8!"
)
EXIT


ADDTXT.bat – then I add the “.txt” back to the end of the file name and I’m done!
-----------------------------------------------------------------------------------------------

Code: Select all

CD K:\EDI\GWEDI\REMITS\TEMP\XTEMP
ren *--???????? *-????????.txt
exit
Last edited by aGerman on 17 Oct 2017 05:00, edited 1 time in total.
Reason: Code Tags added

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

Re: strip characters, move beginning to end, fix extension

#2 Post by aGerman » 17 Oct 2017 04:58

Some real file names would have been nice.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

for /f "delims=" %%g in ('dir /a-d /b *[*].txt.*^|findstr /ie "\.txt\.[0-9][0-9]*"') do (
  for /f "tokens=1,3,5,7 delims=[]" %%h in ("%%g") do (
    set "file=%%g"
    set "name=%%i"
    setlocal EnableDelayedExpansion
    set "name=!name:__=-!"
    set "name=!name:_=-!"
    set "name=!name:--=-!"
    ECHO ren "!file!" "!name:EANW=!-%%j-%%k-%%h.txt"
    endlocal
  )
)
PAUSE

Remove ECHO and PAUSE if the script lists the right commands.

Steffen

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

Re: strip characters, move beginning to end, fix extension

#3 Post by Aacini » 17 Oct 2017 07:20

This works with your example file names:

Code: Select all

@echo off

for /F "tokens=1-8 delims=[]_." %%a in ('dir /a-d  /b') do (
   ECHO ren "[%%a]_[%%b_%%c]_[%%d.%%e]_[%%f].%%g.%%h" "%%b-%%c-%%d.%%e-%%f-%%a.%%g"
)

Output example:

Code: Select all

ren "[20171013]_[FILENAME_A]_[120.00]_[1234456].txt.99980" "FILENAME-A-120.00-1234456-20171013.txt"
ren "[20171013]_[FILENAME_b]_[101.00]_[1273456].txt.99913" "FILENAME-b-101.00-1273456-20171013.txt"
ren "[20171014]_[FILENAME_c]_[100.25]_[1239456].txt.121980" "FILENAME-c-100.25-1239456-20171014.txt"

Antonio

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

Re: strip characters, move beginning to end, fix extension

#4 Post by Compo » 17 Oct 2017 08:01

In FIXFILENAME.bat I would replace all instances of START /WAIT with CALL

Post Reply