Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

Batch move files from different subfolder up one level respectively

I have hundreds over folders with structure like this:

  • PARENTFolderASubfolder01files1.iso
  • PARENTFolderBSubfolder02files2.iso
  • PARENTFolderCSubfolder03files3.iso

I want to move all the files1.iso, files2.iso, files3.iso up one level respectively. Should look like this.

  • PARENTFolderAfiles1.iso
  • PARENTFolderBfiles2.iso
  • PARENTFolderCfiles3.iso

And what would be even better is something that work to delete the Subfolder01, Subfolder02, Subfolder03 which are not wanted.

And if possible, as well batch rename those files1.iso, files2.iso, files3.iso to the name of FolderA.iso, FolderB.iso, FolderC.iso respectively.

I really have no idea how to work this out. Anybody can help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
cd PARENT
for /D %%i in (*) do (
  for /D %%j in (%%i*) do (
    move "%%j*" "%%i\%%i.iso" 2>&1>nul && rmdir "%%j" 2>&1>nul
  )
)

An explanation:

cd PARENT

Just make sure you're in the root directory to work from so the rest works

for /D %%i in (*) do (

This is a for loop, for every directory in the working directory it sets %%i to the directory name (e.g. FolderA), then does the following:

  for /D %%j in (%%i*) do (

This is a nested for loop, for every directory in %%i (on first loop, FolderA) it sets %%j to the directory name (on first loop, FolderASubfolder01), then does the following:

    move "%%j*.iso" "%%i\%%i.iso" 2>&1>nul && rmdir "%%j" 2>&1>nul

Move everything whose name ends with .iso in %%j (FolderASubfolder01) to %%i (FolderA), and rename it to %%i.iso (FolderA.iso). If that works, remove the %%j directory. Redirect all output to nul (i.e. produce no output).

  )
)

Close off the loops.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...