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