Search code examples
filebatch-filebulkfile-manipulation

manipulate bulk of 1000+ images


Hi all I will be glad to get some help with windows batch script file to manipulate images, I am migrating from old software, it used to save the files in folder and each file had its own user id (e.g. 10050.jpg) I have like 1000 of these images, I would like to distribute the images to new folder with the image name and create SQL file to update the new software for example:

10050.jpg, 10051.jpg, 10052a.jpg, 10052b.jpg

Will go to:

/root/10050/10050.jpg
/root/10051/10051.jpg
/root/10052/10052a.jpg
/root/10052/10052b.jpg

And SQL file created:

update users set user_img = 10050/10050.jpg where user_id = 10050;
update users set user_img = 10051/10051.jpg where user_id = 10051;
update users set user_img_a = 10052/10052a.jpg where user_id = 10052;
update users set user_img_b = 10052/10052b.jpg where user_id = 10052;

Can anyone help me write a batch file to extract this information? I am a rank beginner at this. Thank you!


Solution

  • Given that the IDs in file names always consist of 5 digits, the algorithm could be like this:

    1. Take a file at the old location.

    2. Extract the leading 5 characters from the file name as the respective user's id.

    3. If there's no corresponding subfolder by the new root path, create it.

    4. Copy the file to the new location.

    5. Add the corresponding SQL script line to the SQL script file.

    6. Repeat steps 1..5 for all the relevant files.

    And here's my attempt at an implementation:

    SETLOCAL
    
    SET "oldroot=X:\originalpath"
    SET "newroot=Y:\newrootfolder"
    SET "sqlscript=Z:\path\to\script.sql"
    
    FOR %%F IN (%oldroot%\*.jpg) DO CALL :process "%%F"
    
    ENDLOCAL
    GOTO :EOF
    
    :process
    SET filename=%~nx1
    SET userid=%filename:~0,5%
    
    IF NOT EXIST "%newroot%\%userid%\" MKDIR "%newroot%\%userid%"
    COPY %1 "%newroot%\%userid%"
    
    >>%sqlscript% ECHO update users set user_img = '%userid%\%filename%' where user_id = %userid%