Using Formatted Filenames to Generate Folders
- A complete install of
Python 3.x
. - Under
Downloads
, the properly formatted files.
Formatted Files:
report-working with ai.txt
sales-2007.txt
2008-requirements.txt
You'll notice that the files names start with a word or number, followed by a dash, and end with a word or number.
The script isn't exclusive to just .txt
. We can work with any file type, as long as the filename is properly formatted.
- Under your
USERPROFILE
, extractusing-formatted-filenames-to-generate-folders-main.zip
.
Example:
C:\Users\nso89\using-formatted-filenames-to-generate-folders-main
- Open
cmd.exe
and change the directory to theusing-formatted-filenames-to-generate-folders-main
folder.
Example:
C:\Users\nso89>cd using-formatted-filenames-to-generate-folders-main
- Start the
main.py
script.
Example:
C:\Users\nso89\using-formatted-filenames-to-generate-folders-main>python main.py
- It takes the word before the hyphen, and generates a folder. It then renames and moves the file to that folder.
Example:
Renaming ..\Downloads\2008-requirements.txt --> ..\Downloads\2008\requirements.txt
Renaming ..\Downloads\report-working with ai.txt --> ..\Downloads\Report\working-with-ai.txt
Renaming ..\Downloads\sales-2007.txt --> ..\Downloads\Sales\2007.txt
If you need to change the SOURCE
or the DELIMITER
:
- Open the
main.py
script in any text editor. - Locate the
SOURCE
andDELIMITER
variable.
Example:
SOURCE = Path.home().joinpath("Downloads")
DELIMITER = "-"
- When you finish changing the variables, save and close the editor.