How to bulk rename files within subfolders - using CMD command prompt

十年热恋 提交于 2019-12-25 03:57:06

问题


I am quite new to Command Prompt (Windows), however I have used it to do some file extensions changing and it is extremely helpful. I know nothing on coding, and so i am simply going off what I have read about but I am looking for a command line that I cant seem to find anywhere. I have folder, and inside that folder are 70 sub folders each labelled by a number from 1-70. Inside these subfolders are roughly 20 png picture files, that are currently numbered by a number from 1-20 in png format. I am looking for a command line to rename each file from its original name to "folder name (page number).png"

For example, I have folder called '68' and inside that folder is 1.png, 2.png, 3.png. I want the command line to change that 1.png and 2.png etc... to 68 (1).png and 68 (2). png, noticing the space between the bracket and the folder name. Sorry if i have made it confusing but I would really appreciate it and I have got some very helpful and quick answers from StackOverflow before

Thankyou if you are able to help me, as i am completely hopeless on this matter.


回答1:


Only run this once - launch it from the folder containing the 70 folders.

@echo off
for /f "delims=" %%a in ('dir /ad /b') do (
pushd "%%a"
for /f "delims=" %%b in ('dir /a-d /b') do ren "%%b" "%%a (%%~nb)%%~xb"
popd
)



回答2:


I am not a very experienced bash scriptor, but I guess this should do the task for you. I am assuming that you are using Linux operating system. So open a text editor and copy the following:

#!/bin/bash

NumberOfFolders=70

for((a=1; a <= NumberOfFolders ; a++))
do
        cd ./$a
        b=1
        while [ -f $b.png ]
        do
                mv "$b.png" "$a($b).png"
                let b=b+1
        done
        cd ..
done

save this script where you have you folders 1-70 (and call it whatever.ssh). Then open a terminal and write down chmod a+x whatever.ssh and after that ./whatever.ssh. This should do the job as you presented in your question.




回答3:


A slight modification of the approach @foxidrive suggested, so the script can be run from anywhere and won't have to parse dir output:

@echo off
for /r "C:\root\folder" %%d in (.) do (
  pushd "%%~fd"
  for %%f in (*) do ren "%%~ff" "%%~nd (%%~nf)%%~xf"
  popd
)

Note that this will not work in Windows XP (not sure about Windows Vista), due to a bug with the expansion of * in the inner for loop that would cause the files to be renamed multiple times.



来源:https://stackoverflow.com/questions/16248730/how-to-bulk-rename-files-within-subfolders-using-cmd-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!