如何遞回地將不同檔案夾中的檔案(但所有檔案夾都在一個檔案夾下)(不考慮它們的名稱)重命名為十六進制基數?

因此,當它轉到下一個檔案夾時,它會記住它之前命名的內容并從那里繼續。
例如(不同于截圖):
Folder1: 1.jpg, 1_A.jpg, ... 10th_blah.jpg
Folder2: 1.jpg , ..., 2000th_text1.jpg
將變成 ->
FolderAllTogether: 1.jpg, 2.jpg, ..., A.jpg, B.jpg , ..., 7D0.jpg
解決方案可以是 javascript、bash、python 或任何其他我可以在 macOS 上輕松運行的腳本語言。
我的代碼:
const fs = require('fs')
const path = require('path');
const dir = path.resolve(__dirname, 'ImagesSuperTemp');
let i = 0
function renameFilesRecursive(dir, from, to) {
fs.readdirSync(dir).forEach(it => {
const itsPath = path.resolve(dir, it);
const itsStat = fs.statSync(itsPath);
if (itsPath.search(from) > -1) {
console.log(`${i} : rename ${from} to ${to}`);
fs.renameSync(itsPath, itsPath.replace(from, to))
i = i 1
}
if (itsStat.isDirectory()) {
renameFilesRecursive(itsPath.replace(from, to), from, to)
}
})
}
renameFilesRecursive(dir, /\*.jpg$/, `${i.toString(16)}.jpg`);
但它什么都不做!
uj5u.com熱心網友回復:
下面是一個 bash 腳本。更改的值dest_dir和source_dir
不要忘記使其可執行 chmod x flattener.sh
#!/bin/env bash
dest_dir="/var/tmp/flat-files"
source_dir="parent-dir"
if [ ! -d "${dest_dir}" ] ; then
mkdir -p "${dest_dir}"
fi
if [ ! -f "${dest_dir}"/index.dat ] ; then
printf "1" > "${dest_dir}"/index.dat || exit 1
fi
find "${source_dir}" -type f -print0 |\
xargs -0 bash -c ' \
for filename ; do \
index=$(cat '"${dest_dir}"'/index.dat) ;\
printf "%d" $((index 1)) > '"${dest_dir}"'/index.dat ;\
if [[ "${filename}" =~ [.] ]] ; then \
extension=."${filename##*.}" ;\
else \
extension="" ;\
fi ;\
mv -vu "${filename}" '"${dest_dir}"'/$(printf "%X" $index)"${extension}" ;\
done' flattener
uj5u.com熱心網友回復:
Python解決方案
(如果您想重命名已經存在的檔案,則會拋出一個錯誤,例如您正在處理影像 5.jpg,而您的計數器為 3,但 3.jpg 已經存在;可以使用try except子句避免這種情況,但您沒有圖片總是以 1 遞增 - 不確定您需要什么):
import os
counter = 1
for r, d, f in os.walk(r"Images"): # can also put a full path here
for name in f:
# first argument here is full path to the image
# second argument is the path the hex number of the counter the extension of the file
os.rename(r os.sep name, r os.sep hex(counter).lstrip("0x") os.path.splitext(name)[-1])
counter = 1
編輯
我進一步記錄了代碼,添加了狀態訊息并注意到您需要大寫的十六進制檔案名:
import os
counter = 1
for r, d, f in os.walk(r"Images"): # can also put a full path here
for name in f:
# full path to original image
original_fn = r os.sep name
# path hex number of counter extension of file
new_fn = r os.sep hex(counter).lstrip("0x").upper() os.path.splitext(name)[-1]
# rename, status print, incrementing counter
os.rename(original_fn, new_fn)
print(f"Renamed {original_fn} > {new_fn}")
counter = 1
編輯 2
這是一個解決方案,首先將所有檔案重命名為附加隨機字串的臨時檔案,然后根據計數器將所有檔案重命名為十六進制數字。希望能解決您的問題:
import os
import random
import string
for r, d, f in os.walk(r"Images"):
for name in f:
rnd_string = ''.join(random.SystemRandom().choice(string.ascii_letters string.digits) for _ in range(12))
original_fn = r os.sep name
new_fn = r os.sep rnd_string name
os.rename(original_fn, new_fn)
print(f"Temp. rename {original_fn} > {new_fn}")
counter = 1
for r, d, f in os.walk(r"Images"): # can also put a full path here
for name in f:
original_fn = r os.sep name
new_fn = r os.sep hex(counter).lstrip("0x").upper() os.path.splitext(name)[-1]
os.rename(original_fn, new_fn)
print(f"Final rename {original_fn} > {new_fn}")
counter = 1
uj5u.com熱心網友回復:
使用bash>= 4.0 和cp(無mv/ rename):
shopt -s globstar
unset int
for i in **/*.jpg; do
((int ))
printf -v hex '%x' "$int"
echo cp -v "$i" "FolderAllTogether/${hex^^}.jpg"
done
如果輸出看起來不錯,請洗掉echo.
uj5u.com熱心網友回復:
我只是為了完整性而添加這個來回答您要求重命名檔案的實際問題,盡管它表明您實際上想要復制檔案。
你可以rename像這樣用 Perl 做到這一點:
find images -iname "*.jpg" -print0 | rename --stdin -n -0 -N 01 '$_=sprintf("%X.jpg", $N);'
樣本輸出
'images/11/5.jpg' would be renamed to '1.jpg'
'images/11/3.jpg' would be renamed to '2.jpg'
'images/6/2.jpg' would be renamed to '3.jpg'
'images/6/3.jpg' would be renamed to '4.jpg'
'images/1/17.jpg' would be renamed to '5.jpg'
'images/1/21.jpg' would be renamed to '6.jpg'
'images/1/22.jpg' would be renamed to '7.jpg'
'images/1/23.jpg' would be renamed to '8.jpg'
'images/1/1_A.jpg' would be renamed to '9.jpg'
'images/1/25.jpg' would be renamed to 'A.jpg'
'images/1/1.jpg' would be renamed to 'B.jpg'
跨目錄保留計數器的關鍵是使用-N 01,然后您有一個可以使用的自動遞增計數器。
如果您有 bash 4 或更新版本或zsh(我認為),您可能可以使用這個更簡單的版本:
rename -n -N 01 '$_=sprintf("%X.jpg", $N)' images/**/*.jpg
請注意,Perlrename最簡單地安裝在帶有自制軟體的macOS 上,使用:
brew install rename
uj5u.com熱心網友回復:
使用bash和find:
#!/bin/bash
srcdir='/path/to/source/directory'
dstdir='/path/to/destination/directory'
n=1
while IFS= read -r -d '' file; do
while [[ -e $dstdir/$(printf '%X' $n).jpg ]]; do (( n)); done
cp -- "$file" "$dstdir/$(printf '%X' $n).jpg"
done < <(find "$srcdir" -type f -name '*.jpg' -print0)
如果源和目標目錄是在同一個檔案系統,你可能會考慮更換cp用ln。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/392354.html
標籤:javascript Python 猛击 苹果系统
