hi
It's been sometime since I've used BATCH files on the 'MSDOS' command line and I've NEVER used Powershell. So I am struggling with this
POWERSHELL 2012 or CMD Version 6.2.9200 are AVAILABLE to me
I've got lots of files (1000's?) (*.txt and *.xls) that I need to rename using part of the individual files path.
F:\k\20150224_Daily_055\IN5720R\Users\len\Documents\Text Files\FRED.TXT (RENAME TO: 20150224d_IN5720R_FRED.TXT)
F:\k\20150224_Daily_055\IN5720R\Users\len\Documents\Text Files\GEORGE.TXT (RENAME TO: 20150224d_IN5720R_GEORGE.TXT)
F:\k\20150224_Daily_055\IN5720R\Users\len\Documents\DAVE.TXT (RENAME TO: 20150224d_IN5720R_DAVE.TXT)
F:\k\20150224_Daily_055\PRECT9100\Users\len\Documents\Text Files\GEORGE.TXT (RENAME TO: 20150224d_PRECT9100_GEORGE.TXT)
F:\k\20150224_Daily_055\DIM1600\DATA\MAIN\CONTACTS\PETER.XLS (RENAME TO: 20150224d_DIM1600_PETER.XLS)
F:\k\20150224_Daily_055\DIM1600\DATA\MAIN\HOUSE.XLSM (RENAME TO: 20150224d_DIM1600_HOUSE.XLSM)
then again in the next 'root folder'
F:\k\20150225_Daily_056\IN5720R\Users\len\Documents\Text Files\FRED.TXT (RENAME TO: 20150225d_IN5720R_FRED.TXT)
F:\k\20150225_Daily_056\IN5720R\Users\len\Documents\Text Files\GEORGE.TXT (RENAME TO: 20150225d_IN5720R_GEORGE.TXT)
F:\k\20150225_Daily_056\IN5720R\Users\len\Documents\DAVE.TXT (RENAME TO: 20150225d_IN5720R_DAVE.TXT)
F:\k\20150225_Daily_056\PRECT9100\Users\len\Documents\Text Files\GEORGE.TXT (RENAME TO: 20150225d_PRECT9100_GEORGE.TXT)
F:\k\20150225_Daily_056\DIM1600\DATA\MAIN\CONTACTS\PETER.XLS (RENAME TO: 20150225d_DIM1600_PETER.XLS)
F:\k\20150225_Daily_056\DIM1600\DATA\MAIN\HOUSE.XLSM (RENAME TO: 20150225d_DIM1600_HOUSE.XLSM)
1234567890123456789012345
also
F:\k\20150522_Weekly_21\IN5720R\Users\len\Documents\Text Files\FRED.TXT (RENAME TO: 20150522w_IN5720R_FRED.TXT)
F:\k\20150522_Weekly_21\IN5720R\Users\len\Documents\Text Files\GEORGE.TXT (RENAME TO: 20150522w_IN5720R_GEORGE.TXT)
F:\k\20150522_Weekly_21\IN5720R\Users\len\Documents\DAVE.TXT (RENAME TO: 20150522w_IN5720R_DAVE.TXT)
F:\k\20150522_Weekly_21\PRECT9100\Users\len\Documents\Text Files\GEORGE.TXT (RENAME TO: 20150522w_PRECT9100_GEORGE.TXT)
F:\k\20150522_Weekly_21\DIM1600\DATA\MAIN\CONTACTS\PETER.XLS (RENAME TO: 20150522w_DIM1600_PETER.XLS)
F:\k\20150522_Weekly_21\DIM1600\DATA\MAIN\HOUSE.XLSM (RENAME TO: 20150522w_DIM1600_HOUSE.XLSM)
1234567890123456789012345
etc.....
(note: all the files to rename appear somewhere in folders 'beneath' the PC name (eg IN5720R, PRECT9100, DIM1600)
so I presume I use something like
FOR /R "F:\k\" %%G in (.) DO (
:: to recurse down through the filepath (here below is my provisional algorithm)
:: IF a folder contains a *.txt file OR a *.xls* file
:: THEN
LET _OLDFILENAME = name of the file
:: LET _PATHNAME = path of the file
:: LET _DATEPREFIX = slice of _PATHNAME chars 6 to 13
:: LET _TYPEPREFIX = slice of _PATHNAME char 15
:: LET _PCTYPE = slice of _PATHNAME char 25
:: LET _PCTYPEPREFIX = "error"
:: IF _PCTYPE = "I"
:: THEN LET _PCTYPEPREFIX = "IN5720R"
:: IF _PCTYPE = "P"
:: THEN LET _PCTYPEPREFIX = "PRECT9100"
:: IF _PCTYPE = "D"
:: THEN LET _PCTYPEPREFIX = "DIM1600"
:: LET _FILEPREFIX = _DATEPREFIX + _TYPEPREFIX + "_" + _PCTYPEPREFIX + "_"
:: rename ALL *.TXT to _FILEPREFIX + originalfilename
:: rename ALL *.xls* to _FILEPREFIX + originalfilename
:: NEXT Folder
:: until recursed back to root
)
I am sure their are 'Better Algorithms' which suit batch scripting
thanks
mass recursive rename of files using parts of the file path
Moderator: DosItHelp
-
- Posts: 2
- Joined: 13 Aug 2016 07:37
Re: mass recursive rename of files using parts of the file path
Something like that should work
Regards
aGerman
Code: Select all
@echo off &setlocal
set "base=F:\k"
pushd "%base%"
for /f "delims=" %%i in ('dir /ad /b^|findstr /b "20[0-9][0-9][01][0-9][0-3][0-9]_"') do ( REM subfolder name with date in %%i
for /f "tokens=1,2 delims=_" %%j in ("%%i") do ( REM date in %%j, Daily or Weekly in %%k
for /f "delims=" %%l in ('dir /ad /b "%%i\"') do ( REM PC name in %%l
for /f "delims=" %%m in ('dir /a-d /b /s "%%i\%%l\*.txt" "%%i\%%l\*.xls?"^|findstr /ve "\\20[0-9][0-9][01][0-9][0-3][0-9][dw]_[^\\]*"') do ( REM file full name in %%m, excluding already renamed files
if /i "%%k"=="Daily" (
ren "%%m" "%%jd_%%l_%%~nxm"
) else if /i "%%k"=="Weekly" (
ren "%%m" "%%jw_%%l_%%~nxm"
)
)
)
)
)
popd
Regards
aGerman
-
- Posts: 2
- Joined: 13 Aug 2016 07:37
Re: mass recursive rename of files using parts of the file path
Appears to work flawlessly, first time.....
Awesome
Thanks Simon
PS. wish I understood the script you have written
Awesome
Thanks Simon
PS. wish I understood the script you have written
Re: mass recursive rename of files using parts of the file path
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "letter[Daily]=d"
set "letter[Weekly]=w"
set "pcType[I]=IN5720R"
set "pcType[P]=PRECT9100"
set "pcType[D]=DIM1600"
cd /D F:\k
for /F "delims=" %%f in ('dir /S /B *.txt *.xls*') do (
for /F "tokens=3,4,6 delims=\_" %%a in ("%%f") do (
set "pc=%%c"
for /F %%p in ("!pc:~0,1!") do set "pc=!pcType[%%p]!"
if not defined pc set "pc=error"
ren "%%f" "%%a!letter[%%b]!_!pc!_%%~NXf"
)
)
Antonio
Re: mass recursive rename of files using parts of the file path
SimonBiggs wrote:Appears to work flawlessly, first time.....
Awesome
Thanks Simon
Simon, I'm commenting here to help you avoid a massive problem in the future if my assessment of how you proceeded is correct. Your post suggested to me that you ran aGerman's script on the data you are using, and that it worked fine.
If you have your live data all backed up, or ran the script against a copy of your live data, then I'm preaching to the choir and you already follow the need for protecting your data.
My point here is that any script can go wrong and mangle your data in ways that is very difficult to recover from.
So a script should tested on a copy of your files, or you simply back up your files just before you run the script.