开发者

manipulate bulk of 1000+ images

开发者 https://www.devze.com 2023-02-22 05:27 出处:网络
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

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.j开发者_如何学Pythonpg, 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!


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%


If you have a choice, here's a Ruby for Windows script

require 'fileutils'
root="C:\\root"
o = File.open("sql.txt","w")
Dir["*.jpg"].each do| jpg|
    dirname = jpg.scan(/^(\d+)/)[0].first # get the nbumber
    Dir.mkdir(dirname) if not Dir.exists?(dirname) #make directory
    FileUtils.copy( jpg , root + "\\"+dirname) #copy / move
    o.write( "update users set user_img = #{dirname}/#{jpg} where user_id = #{dirname}\n" ); #create sql
end
o.close

I don't know why you are using *nix directory syntax when you are in windows, but change the root variable accordingly as you deem fit.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号