Products > Programming

Create Seperate Directory Names From a List of Files and Then Move Into Them

(1/2) > >>

bostonman:
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.

DimitriP:
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: ---@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


--- End code ---

abeyer:
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 (which, despite the name, respects whatever your $EDITOR is.)

It makes big rename/move/delete tasks like this into a quick text edit.

bostonman:

--- 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
--- End quote ---

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.

DimitriP:
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



 

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod