Create random folders and move files in them

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pelomero
Posts: 7
Joined: 01 May 2020 03:08

Create random folders and move files in them

#1 Post by pelomero » 23 May 2020 23:32

Hello!
The context:
In a specific folder I have multiple zip files. I have a code which takes all the zip files in that folder and unpack them in separate folders

The code:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /r "%cd%" %%a in ("*.zip") do powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('%%~fa', '00. %%~na'); }

What I'am trying to add to the mix.

- Before unpacking the zip file, create a new folder, where I move the zip file and after that unpack the zip file in that new folder

So, for:

Code: Select all

D:\Folder1
>D:\Folder1\file1.zip
>D:\Folder1\file2.zip
For file1.zip, create Folder "01 - a random number", move the file1.zip in this new created folder, unpack the zip file with the code above.
In the final I would have something like this:

Code: Select all

D:\Folder1
>D:\Folder1\01 - 342
>D:\Folder1\01 - 342\file1.zip
>D:\Folder1\01 - 342\00 - File1 (where I unpacked the zip file)
>D:\Folder1\02 - 5235
>D:\Folder1\02 - 5235\file2.zip
>D:\Folder1\02 - 5235\00 - File2 (where I unpacked the zip file)
P.S. I don't know how to make this whole command line code to work in general. The bits of information I find on the net, I can't seem to put it together in a working code... It drives me nuts. Every simple line I'm trying to insert in the previous code, breaks all the existing code and I am talking about simple lines of codes.
When I am trying to add a piece of code (which is working just fine as itself) in my previous code, all the hell breaks. Even trying to add 2 commands in a for loop it's a pain in the butt.

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create random folders and move files in them

#2 Post by aGerman » 24 May 2020 04:39

That's not tested at all.

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set /a "n=100"
for /r "%cd%" %%a in ("*.zip") do (
  set /a "n+=1"
  set "file=%%~fa"
  set "filepath=%%~dpa"
  set "filename=%%~na"
  setlocal EnableDelayedExpansion
  set "rnd=!random!"
  md "!filepath!!n:~-2! - !rnd!"
  move "!file!" "!filepath!!n:~-2! - !rnd!\"
  powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('!filepath!!n:~-2! - !rnd!\!filename!.zip', '00. !filename!'); }"
  endlocal
)
Steffen

pelomero
Posts: 7
Joined: 01 May 2020 03:08

Re: Create random folders and move files in them

#3 Post by pelomero » 24 May 2020 06:34

Thank you for the response. I've just tested it.

1- It just goes in a endless loop in creating folders and move it the zip file for each iteration; it treats the same file as a new file. So, for the file "file1.zip", after a couple of seconds of running the program, I get this :mrgreen: :
"d:\Folder1\01 - 24678\02 - 22636\03 - 8314\04 - 986\05 - 847\06 - 13220\07 - 21562\08 - 25769\file1.zip"

So probably the loop should look only in the root folder, without looking in the sub-folders, to avoid this behavior. I've tried to change the for syntax by removing the "/r" argument or by replacing with others, but I can't get the damn thing to work... :mrgreen:

2- At the same time, the unpacking of the zip file is done in the root path, not put in the new created folder.

pelomero
Posts: 7
Joined: 01 May 2020 03:08

Re: Create random folders and move files in them

#4 Post by pelomero » 26 May 2020 10:19

I managed to modify the code above to move the extraction of the zip to the new created folder, by doing some debugging using echo. :mrgreen:

Code: Select all

 powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('!filepath!!n:~-2! - !rnd!\!filename!.zip', ('!n:~-2! - !rnd!\00. !filename!')); }"
 
 

But, I still didn't figure out why can I make the for loop to stop searching in sub-directories.
I've managed to find a solution by using forfiles, but I don't know to put it the existing code.

What do I need to change in this line:

Code: Select all

for /r "%cd%" %%a in ("*.zip")
Any help please? :)


Later edit: I just had to delete /r "%cd%" to stop looking in the subfolders. Thanks.

Post Reply