Can anyone write a batch file to do this, please?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Can anyone write a batch file to do this, please?

#1 Post by Kerr Avon » 09 Feb 2018 17:13

Would it be possible for someone to write a batch file that searches for files in any subfolders of a given folder, and moves any files that are more than one subfolder down, into the first subfolder of it's patch (relative to the batch file)? What I mean is, say you have the following four archived files:

c:\1\a.7z
c:\1\b.7z
c:\1\c.7z
c:\1\d.7z

And you want to un-archive them so that all of the files from each archive is inside a folder named after the archive file, i.e.

c:\1\a\allornoth.tap
c:\1\a\antattack.tap
c:\1\a\aufmonty.z80
c:\1\b\batman3d.tap
c:\1\b\batty.z80
c:\1\b\backtosk.tap
c:\1\b\botp.z80

etc, then of course this is easy to do (I use the Windows version of 7zip, with the "extract all files to */" function), but there is one irritating thing, and that's that sometimes you'll end up with a file being put into a sub folder, or a sub-sub-folder, etc. This is mainly because some archive files always produce a folder for the files, and others don't, plus some files might be two or more folders down in the archive.

So what I'm looking for is a batch file that I can, after I've unarchived the files into one folder, copy into that folder, run and it will scan every folder and subfolder (starting from the folder in which the batch file is placed) and then if it will:

1. Ignore files in the same folder as the batch file,

2. Ignore any files in the immediate subfolders (i.e. the files that are one subfolder down, will be ignored by the batch file)

3. Any file that is two or more subfolders down will be moved into the first subfolder of it's location.

4. Any sub-subfolder (which will now be empty) will now be deleted.


What I mean is, say I have the files:

c:\1\a.7z
c:\1\b.7z
c:\1\c.7z
c:\1\d.7z



and when I unzip them to the same folder, I end up with:

c:\1\a\ALLORNOTH.TAP
c:\1\a\a\ANTATTACK.TAP
c:\1\a\a\AUFMONTY.Z80
c:\1\b\BATMAN3D.TAP
c:\1\b\b\123\BATTY.Z80
c:\1\b\b\48k\BACKTOSK.Z80
c:\1\b\BOTP.Z80
c:\1\b\BRKDWN128.TAP
c:\1\b\128K\HB\BL128.TZX
c:\1\b\BDASH3.TAP
c:\1\b\WITHEDITR\BDASH.TAP
c:\1\c\cassette-games\c\CG3.TAP
c:\1\c\CC128.TAP
c:\1\c\CC.Z80
c:\1\c\CENT_128.TAP
c:\1\c\cassette-games\c\Chess3D.TAP
c:\1\d\cassette-games\d\DEFIANT.Z80
c:\1\d\DEFIANT2.TAP
c:\1\d\DRILLER.Z80
c:\1\d\cassette-games\DCFT.TAP

then if I copy this batch file (that I'm hoping is possible to write) to the c:\1 folder, and run it, then it will move all of the files in the subfolders into their immediate first subfolder, and then delete any sub-subfolders and deeper, so that the end is:



c:\1\a\ALLORNOTH.TAP
c:\1\a\ANTATTACK.TAP
c:\1\a\AUFMONTY.Z80
c:\1\b\BATMAN3D.TAP
c:\1\b\BATTY.Z80
c:\1\b\BACKTOSK.Z80
c:\1\b\BOTP.Z80
c:\1\b\BRKDWN128.TAP
c:\1\b\BL128.TZX
c:\1\b\BDASH3.TAP
c:\1\b\WITHEDITR\BDASH.TAP
c:\1\c\CG3.TAP
c:\1\c\CC128.TAP
c:\1\c\CC.Z80
c:\1\c\CENT_128.TAP
c:\1\c\Chess3D.TAP
c:\1\d\DEFIANT.Z80
c:\1\d\DEFIANT2.TAP
c:\1\d\DRILLER.Z80
c:\1\d\DCFT.TAP

I'm using Windows 7, if that's important. Thanks for any answers.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Can anyone write a batch file to do this, please?

#2 Post by penpen » 10 Feb 2018 14:09

Could it be that the 7-Zip command line switch "e" is what you need?

Code: Select all

7z.exe e *.7z
penpen

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: Can anyone write a batch file to do this, please?

#3 Post by Kerr Avon » 10 Feb 2018 18:00

penpen wrote:
10 Feb 2018 14:09
Could it be that the 7-Zip command line switch "e" is what you need?

Code: Select all

7z.exe e *.7z
penpen
No, because that puts the immediate files (the ones from the archive file that are not in a subdirectory inside the archive file) all in the same folder, whereas I want each one in a folder that's named after the initial archive file that it came from, plus those files that were in a subdirectory in the archive are now in their subdirectories (which can be three or four deep, if a file was archived as say \arcade\platform\1986\DYN-DAN2.Z80).

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Can anyone write a batch file to do this, please?

#4 Post by penpen » 11 Feb 2018 05:13

Kerr Avon wrote:
10 Feb 2018 18:00
plus those files that were in a subdirectory in the archive are now in their subdirectories (which can be three or four deep, if a file was archived as say \arcade\platform\1986\DYN-DAN2.Z80).
I suspect you are confusing option "e" with option "x".
Kerr Avon wrote:
10 Feb 2018 18:00
No, because that puts the immediate files (the ones from the archive file that are not in a subdirectory inside the archive file) all in the same folder, whereas I want each one in a folder that's named after the initial archive file that it came from
But i missed that point.

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for %%a in (*.7z) do (
	>nul "C:\Program Files\7-Zip\7z.exe" e -o".\%%~na" "%%~a"
	for /F "tokens=* delims=" %%b in ('dir /a:D /b /s ".\%%~na"') do rd "%%~b"
)
goto :eof
penpen

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: Can anyone write a batch file to do this, please?

#5 Post by Kerr Avon » 11 Feb 2018 13:44

That batch file is great, I've just tried it on some .7z files and it unarchives the files and moves them into the correct subfolders that I wanted, thanks!

But (sorry about this!) would it be possible to have a batch file that does the same thing on already unarchived folders, please? I mean, folders that are already on the hard-drive, such as:

c:\1\a\allornoth.tap
c:\1\a\123\atmosphere.tap
c:\1\a\123\addons\atmos_ud.tap
c:\1\a\antattack.tap
c:\1\a\aufmonty.z80
c:\1\b\batman3d.tap
c:\1\b\batty.z80
c:\1b\batz\
c:\1\b\backtosk.tap
c:\1\b\botp.z80

so that the batch file, when copied and ran from

c:\1\

will sort the files into the immediate subfolder (a or b, in the above case) and delete the empty sub-subfolders? This is because I already have folders like that on my hard-drive, and this would really help to tidy them up, plus in the unlikely even that I download files in an archive format that 7zip can't unarchive (some archives of 8 bit emulators can be in LZH or other formats, though I doubt I'll encounter them), thanks.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Can anyone write a batch file to do this, please?

#6 Post by penpen » 13 Feb 2018 05:45

The following might help you (untested, so you should make a backup befor you test it):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /F "tokens=* delims=" %%a in ('dir /a:D /b') do (
	pushd "%%~a"
	for /F "tokens=* delims=" %%b in ('dir /a:-D /b /s "."') do move "%%~b" "."
	for /F "tokens=* delims=" %%b in ('dir /a:D /b "."') do rd /s /q "%%~b"
	popd

)
goto :eof
penpen

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: Can anyone write a batch file to do this, please?

#7 Post by Kerr Avon » 14 Feb 2018 08:59

Mate, that is brilliant! I've just tried it and it does what I want.

Thanks for that, and also for the earlier unarching one.

People like you and forums like this are what the internet was invented for!

Thanks again, mate.

Post Reply