EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: bostonman on October 29, 2024, 03:23:05 am

Title: Create Seperate Directory Names From a List of Files and Then Move Into Them
Post by: bostonman on October 29, 2024, 03:23:05 am
Not sure if my subject line makes sense.

I collect manuals in PDF format for vintage C64 video games. Normally I get them in ZIP format, and, when I extract them (either single or a bunch of ZIP files), I let WinZip unzip them into separate directories which they are created based on the ZIP file name.

i.e. file.zip gets unzipped into a newly created directory named file with the ZIP contents in it.

I take this approach because I began getting more files related to that particular game, so I realized having separate directories made more sense for future needs.

Earlier I got a single ZIP file with almost 1000 PDFs in it and would like to keep the same structure.

Does a way exist that will create a directory based on the file name (leaving out the extension) and then move that file into the newly created directory; I'd need this done in a batch since I have almost 1000 PDFs?

The next issue I'll have should a way exist to do this will be eliminating the duplicates and/or how to handle having that directory already, however, this is another story.
Title: Re: Create Seperate Directory Names From a List of Files and Then Move Into Them
Post by: DimitriP on October 29, 2024, 05:12:07 am
Creates a folder for each filename without the extension and then moves the file onto it.
For those that are into that sort of thing :)


Code: [Select]
@echo off
setlocal enabledelayedexpansion

rem Check if a parameter is provided
if "%~1"=="" (
       echo No filename pattern specified ie: *.pdf
    exit /b
)

rem Loop through each file matching the wildcard passed as a parameter
for %%f in (%1) do (
    rem Get the file name without the extension and the file extension
    set "filename=%%~nf"
    set "extension=%%~xf"
   
    rem Create a folder with the file name if it doesn't exist
    if not exist "!filename!" (
        mkdir "!filename!"
    )

    rem Set the destination path
    set "destpath=!filename!\%%~nxf"
   
    rem Check if the file already exists in the folder
    if exist "!destpath!" (
        set "suffix=a"
       
        rem Loop to find the next available suffix
        :find_suffix
        set "newdest=!filename!\%%~nf-!suffix!%%~xf"
        if exist "!newdest!" (
            set /a "suffix=!suffix! + 1"
            for %%i in (!suffix!) do (
                set "suffix=!suffix:a=z!"
            )
            goto :find_suffix
        )
       
        rem Move the file with the suffix
        move "%%f" "!newdest!"
    ) else (
        rem Move the file without suffix if it doesn't exist
        move "%%f" "!destpath!"
    )
)

endlocal

Title: Re: Create Seperate Directory Names From a List of Files and Then Move Into Them
Post by: abeyer on October 29, 2024, 09:30:27 pm
Probably not what you're looking for given the windows references... but for anyone else with a similar problem for whom it is a fit, don't forget about vidir (https://linux.die.net/man/1/vidir) (which, despite the name, respects whatever your $EDITOR is.)

It makes big rename/move/delete tasks like this into a quick text edit.
Title: Re: Create Seperate Directory Names From a List of Files and Then Move Into Them
Post by: bostonman on October 30, 2024, 12:45:33 am
Quote
Creates a folder for each filename without the extension and then moves the file onto it.
For those that are into that sort of thing

Did you create that or did you already have it?

Also, which language is that in? I don't know much about programming, so uncertain where to go with the code.
Title: Re: Create Seperate Directory Names From a List of Files and Then Move Into Them
Post by: DimitriP on October 30, 2024, 01:02:31 am
Paste it it into notepad and save it as movem.bat  in the same folder as your 1000s of pdfs
Open a command prompt window CD to the same folder and while in the folder run:  movem  *.pdf



 
Title: Re: Create Seperate Directory Names From a List of Files and Then Move Into Them
Post by: bostonman on October 30, 2024, 02:34:58 am
That worked perfectly, thanks!

I know a bit about .bat files, however, not to the extent you created.