Page 1 of 1

Renaming Data

Posted: 16 Oct 2018 06:53
by Niffer0116
Hello Guys,

i have a bunch of folders with a bunch of data in it.
I need to rename the data inside into FOLDERNAME_YEAR

The year of each data is in the name of the data, but its completly random where:

Like:
Foldername:LOLA

Datas:
hjsdfsdjhf2011.txt
2012asdasda.txt
dsfs2014sdfdf.txt
aso.

need to be:
LOLA_2011
LOLA_2012
LOLA_2014
aso.


Can somebody help me? Please not that it is ok to copy the bat. into the folders. No need for one bat. which does is for all. I hope somebody can help me

Re: Renaming Data

Posted: 16 Oct 2018 13:08
by Squashman
Take a look at JREN.bat

Re: Renaming Data

Posted: 16 Oct 2018 14:13
by Aacini
You should not describe the data by examples only. You should always use clear English to describe the data; otherwise, it could lead to confusions...

Code: Select all

@echo off
setlocal

set "letters=abcdefghijklmnopqrstuvwxz"

rem i have a bunch of folders
for /D %%f in (*) do (

   rem with a bunch of data in it
   cd "%%f"
   for /F "delims=" %%d in ('dir /B *.txt') do (

      rem The year of each data is in the name of the data,  *delimited by letters*
      for /F "delims=%letters%" %%y in ("%%~Nd") do (

         rem I need to rename the data inside into FOLDERNAME_YEAR
         ECHO ren "%%d" "%%f_%%y"

      )
   )
   cd ..

)
Put this Batch file in the same folder that contains the "bunch of folders". Try it and, if the displayed REN commands looks correct, remove the ECHO part so the files be renamed...

Re: Renaming Data

Posted: 17 Oct 2018 14:22
by Niffer0116
At first I want to thank you for your response.

I try to specify my problem. I have Folders which have the URL of a Company

baesystems.com
boeing.com
...

In these Folders are Anual Reports which have quite different names(all in pdf) :
annual-report-2017
body_BILANCIO_CONSOLIDATO_2008_ENG_rev_finale_3_
...

These have to be in this form: (if .com is at the end of the company name, that shouldn't be a problem
baesystems_2017
...

I tried ur code but it didn't work.

Re: Renaming Data

Posted: 17 Oct 2018 14:42
by Squashman
Niffer0116 wrote:
17 Oct 2018 14:22
I tried ur code but it didn't work.
And why do you think that is?

Re: Renaming Data

Posted: 17 Oct 2018 15:21
by Niffer0116
Because I have absolutely no clue what I am doing?

Re: Renaming Data

Posted: 17 Oct 2018 15:25
by Squashman
Niffer0116 wrote:
17 Oct 2018 15:21
Because I have absolutely no clue what I am doing?
No, because the technical specifications within your initial question did not accurately describe the task. Now we delve into scope creep.

Re: Renaming Data

Posted: 17 Oct 2018 15:39
by Niffer0116
So did you now got what my problem is?

Re: Renaming Data

Posted: 17 Oct 2018 15:50
by Squashman
Niffer0116 wrote:
17 Oct 2018 15:39
So did you now got what my problem is?
Yes. The batch file will have to be completely rewritten because your technical specifications in your initial question did not match what you actually need. Lets hope someone takes the time to do it.

Re: Renaming Data

Posted: 18 Oct 2018 01:16
by ShadowThief
Squashman wrote:
17 Oct 2018 15:50
Lets hope someone takes the time to do it.
Ordinarily I would, but I still don't have enough sample data.

Re: Renaming Data

Posted: 18 Oct 2018 05:11
by ShadowThief
Actually, I might have enough sample data. Had to guess on a few things, though, which means that there are more assumptions than I would like.

Code: Select all

::------------------------------------------------------------------------------
:: ASSUMPTIONS
::     - This script is located inside of the main folder that contains all of
::       the company folders
::     - All of the folders that are named after company URLs have at least one
::       period in the folder name
::     - File names do not contain !, %, or ^
::     - The first number in the file name that is at least four digits long is
::       the year that needs to be extracted
::       -- Note that this means that "file_name_21094_invoice_2016.pdf" will
::          extract 2109 as the year
::       -- You are expected to manually fix any file names that may have this
::          issue before running the script.
::     - All files that need to be renamed have a year somewhere in their name
::     - Each file has a unique year
::     - There are only PDF files in the folder
::------------------------------------------------------------------------------
:: SUCCESSFUL TEST FILE NAMES
::     body_BILANCIO_CONSOLIDATO_2008_ENG_rev_finale_3_.pdf
::     nospacesatall2009.pdf
::     2010_weird_layout#hashtag.pdf
::     it's_a_weird_2011_name.pdf
::     some file (2012).pdf
::     how.cr@zy.was.2012.pdf
::     both-oh-&-no-2013.pdf
::     who put spaces in this name 2016.txt
::     annual-report-2017.pdf
::
:: UNSUCCESSFUL TEST NAMES
::     ^5_for_2005.pdf
::     100%_operational_as_of_2014.pdf
::     hooray!_for_2006.pdf
::     uh-oh-21090-bad-mask-2019.pdf
::------------------------------------------------------------------------------
@echo off
setlocal enabledelayedexpansion
cls

for /f "tokens=1,* delims=." %%A in ('dir /b /a:d') do (
	pushd "%%A.%%B"
	echo == %%A ==
	for /f "delims=" %%C in ('dir /b') do (
		call :extract_year "%%C" "%%A"
	)
	popd
)

exit /b

::------------------------------------------------------------------------------
:: Renames a specified file with a more meaningful filename
::
:: Arguments: %1 - The current filename
::            %2 - The prefix of the new filename
:: Returns:   None
::------------------------------------------------------------------------------
:extract_year
set "in_string=%~1"
set "string_prefix=%~2"

call :getLength in_string string_length 2>nul

set "year_string="
for /L %%A in (0,1,!string_length!) do (
	echo "!in_string:~%%A,4!" | findstr /R "[0-9][0-9][0-9][0-9]" >nul
	if "!errorlevel!"=="0" (
		set "year_string=!in_string:~%%A,4!"
		goto :exit_loop
	)
)

:exit_loop
ren "!in_string!" "!string_prefix!_!year_string!.pdf"
echo Old file name: !in_string!
echo New file name: !string_prefix!_!year_string!.pdf
echo(

exit /b

::------------------------------------------------------------------------------
:: Gets the length of a given string, minus 3
::
:: Adapted from :strlen7 - https://ss64.org/viewtopic.php?pid=6478#p6478
:: Arguments: %1 - The string to get the length of
:: Returns:   The length of the string
::------------------------------------------------------------------------------
:getLength
setlocal EnableDelayedExpansion
set "s=#!%~1!"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
	if "!s:~%%N,1!" neq "" (
		set /a "len+=%%N"
		set "s=!s:~%%N!"
	)
)
set /a len-=3
endlocal&set %~2=%len%
exit /b