Batch Script to log first + last file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Batch Script to log first + last file

#1 Post by Hohokami » 29 Jun 2016 06:46

Hello,

Inexperienced in the world of coding, so apologies from now. I'm looking for command lines that can help me simplify one of my daily tasks. What I need is a batch file that can do the following:

I have 1 folder (DIRECTORY: c:\users\anonymous\WAV). In that folder is 18 subfolders. In each subfolder are 5 WAV files.

I need a batch file that when I double-click will create a .txt file listing:

A) the names of the 18 subfolders in the \WAV folder
B) the first file in each subfolder (Alphabetically organised)
C) the last file in each subfolder (Alphabetically organised)
D) the size of each subfolder

If possible I would like it formatted as such:

subfolder1name firstfilename lastfilename *size*
subfolder2name firstfilename lastfilename *size*
subfolder3name firstfilename lastfilename *size*
subfolder4name firstfilename lastfilename *size*
....etc


Many thanks for any help you can offer.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Batch Script to log first + last file

#2 Post by Compo » 29 Jun 2016 12:14

This may work for you, (file folder names are unknown and could therefore be problematic).

Code: Select all

@Echo Off&SetLocal EnableExtensions EnableDelayedExpansion
If /I "%CD%" NEq "C:\Users\anonymous\WAV" (Pushd C:\Users\anonymous\WAV||Exit/B)
(For /D %%A In (*) Do (Set "_s=0"& For %%B In ("%%A\*.*") Do (Set/A "_s+=%%~zB"
   If Not Defined _f%%~nxA (Set _f%%~nxA=%%~nxA %%~nxB) Else (
      Set _%%~nxA=!_f%%~nxA! %%~nxB !_s!))
   Echo=!_%%~nxA!))>Output.txt

Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Re: Batch Script to log first + last file

#3 Post by Hohokami » 30 Jun 2016 14:19

Thank you so much. Works perfectly!

Small request: is it possible to change the size output from bytes to Gigabytes?

Thanks again!

Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Re: Batch Script to log first + last file

#4 Post by Hohokami » 30 Jun 2016 14:37

Also (Last request I swear)... if we were to add one more layer to the directory so that the directory is now:

c:\users\anonymous\ROOTFOLDER\WAV

and everyday I create a new ROOTFOLDER (ROOTFOLDER 002, ROOTFOLDER 003, etc) within which the WAV folders with the 18 subfolders go - is there a way of making the batch file always target the WAV folder in the last ROOTFOLDER (Alphabetically organised)? In other words, I don't have to edit source path in the batch file everyday?

Many Thanks.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Batch Script to log first + last file

#5 Post by Compo » 03 Jul 2016 12:39

You could try this:

Code: Select all

@Echo Off&SetLocal EnableExtensions EnableDelayedExpansion
For /D %%A In ("C:\Users\anonymous\ROOTFOLDER*") Do (
   If Exist "%~fA\WAV\" Set "_nF=%%~fA\WAV")
If /I "%CD%" NEq "%_nF%" (Pushd %_nF%||Exit/B)
(For /D %%A In (*) Do (Set "_s=0"&For %%B In ("%%A\*.*") Do (Set/A "_s+=%%~zB"
   If Not Defined _f%%~nxA (Set _f%%~nxA=%%~nxA %%~nxB) Else (
      Call :Conv&Set _%%~nxA=!_f%%~nxA! %%~nxB !_s!))
   Echo=!_%%~nxA!))>Output.txt
Exit/B
:Conv
Set _h=!_s:~,-6!
Set _l=!_s:~-6!
Set/A KB=_h/1024*1000000, _c=_h%%1024
If %_c% Gtr 0 (Set _l=%_c%%_l%) Else (
   For /L %%A In (1,1,5) Do If "!_l:~0,1!" Equ "0" Set _l=!_l:~1!)
Set/A KB+=_l/1024, GB=KB/1048576, HH=KB%%1048576*100/1048576
If %HH% Lss 10 Set HH=0%HH%
Set _s=%GB%.%HH%

Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Re: Batch Script to log first + last file

#6 Post by Hohokami » 06 Jul 2016 04:20

Thanks for the efforts Compo.

Unfortunately though the new script won't even export the output file any longer.

An error message pops up but is present less then a second so can't read it. Any way to keep the prompt open so I can report back?

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Script to log first + last file

#7 Post by foxidrive » 06 Jul 2016 06:11

Put the command pause on a line above exit/B

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Batch Script to log first + last file

#8 Post by Compo » 06 Jul 2016 07:38

Hohokami wrote:Thanks for the efforts Compo.

Unfortunately though the new script won't even export the output file any longer.

An error message pops up but is present less then a second so can't read it. Any way to keep the prompt open so I can report back?

Thanks.
Sorry, I think it may have been the following typo on line 3:

Code: Select all

   If Exist "%~fA\WAV\" Set "_nF=%%~fA\WAV")

Replace it with

Code: Select all

   If Exist "%%~fA\WAV\" Set "_nF=%%~fA\WAV")

Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Re: Batch Script to log first + last file

#9 Post by Hohokami » 09 Jul 2016 03:52

Great! That fix did it. Many thanks compo.

I've incorporated it into my workflow now (rather then on test folders) and just noticed 1-2 small issues which I didn't take into consideration when giving my original request. Sorry about that.

Rather then looking into the latest ROOTFOLDER - is there a way of changing that so it always looks into the second last ROOTFOLDER?

Also, the WAV folder always contains a metadata file (.csv) which always comes up as the last file. Is there a way of ingoring this file so only WAV files are recognised? (Otherwise the code for above - where the first file and second last file are recognised only - might work)

Much appreciated.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Batch Script to log first + last file

#10 Post by Compo » 09 Jul 2016 04:14

Hohokami wrote:Great! That fix did it. Many thanks compo.

I've incorporated it into my workflow now (rather then on test folders) and just noticed 1-2 small issues which I didn't take into consideration when giving my original request. Sorry about that.

Rather then looking into the latest ROOTFOLDER - is there a way of changing that so it always looks into the second last ROOTFOLDER?

Also, the WAV folder always contains a metadata file (.csv) which always comes up as the last file. Is there a way of ingoring this file so only WAV files are recognised? (Otherwise the code for above - where the first file and second last file are recognised only - might work)

Much appreciated.

Just selecting the wav files should be a simple change:

Code: Select all

@Echo Off&SetLocal EnableExtensions EnableDelayedExpansion
For /D %%A In ("C:\Users\anonymous\ROOTFOLDER*") Do (
   If Exist "%%~fA\WAV\" Set "_nF=%%~fA\WAV")
If /I "%CD%" NEq "%_nF%" (Pushd %_nF%||Exit/B)
(For /D %%A In (*) Do (Set "_s=0"&For %%B In ("%%A\*.wav") Do (Set/A "_s+=%%~zB"
   If Not Defined _f%%~nxA (Set _f%%~nxA=%%~nxA %%~nxB) Else (
      Call :Conv&Set _%%~nxA=!_f%%~nxA! %%~nxB !_s!))
   Echo=!_%%~nxA!))>Output.txt
Exit/B
:Conv
Set _h=!_s:~,-6!
Set _l=!_s:~-6!
Set/A KB=_h/1024*1000000, _c=_h%%1024
If %_c% Gtr 0 (Set _l=%_c%%_l%) Else (
   For /L %%A In (1,1,5) Do If "!_l:~0,1!" Equ "0" Set _l=!_l:~1!)
Set/A KB+=_l/1024, GB=KB/1048576, HH=KB%%1048576*100/1048576
If %HH% Lss 10 Set HH=0%HH%
Set _s=%GB%.%HH%


The first part could mean a radical change in the code, we would need to be sure we had the genuine 'rootfolder' names and may possibly have to use the directory creation dates. (I have no idea if your directories are even created in date order or updated/touched/modified by other processes).

Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Re: Batch Script to log first + last file

#11 Post by Hohokami » 09 Jul 2016 07:01

Thanks Comp.

The official names of my ROOTFOLDERS are date in reverse order (160707,160708,160709) and are created automatically on the start of each day. (so at midnight folder 160709 was created)

Creation date is therefore always consistent + never modified by other processes (other then adding in files to the folder)

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Batch Script to log first + last file

#12 Post by Compo » 09 Jul 2016 08:52

My quick suggestion would be to utilise powershell to determine yesterday's directory:

Code: Select all

@Echo Off&SetLocal EnableExtensions EnableDelayedExpansion
For /F UseBackQ %%A In (`PowerShell "(Get-Date).AddDays(-1).ToString('yyMMdd')"`
   ) Do If /I "%CD%" NEq "C:\Users\anonymous\%%A\WAV" (
   Pushd C:\Users\anonymous\%%A\WAV||Exit/B)
(For /D %%A In (*) Do (Set "_s=0"&For %%B In ("%%A\*.wav") Do (Set/A "_s+=%%~zB"
   If Not Defined _f%%~nxA (Set _f%%~nxA=%%~nxA %%~nxB) Else (
      Call :Conv&Set _%%~nxA=!_f%%~nxA! %%~nxB !_s!))
   Echo=!_%%~nxA!))>Output.txt
Exit/B
:Conv
Set _h=!_s:~,-6!
Set _l=!_s:~-6!
Set/A KB=_h/1024*1000000, _c=_h%%1024
If %_c% Gtr 0 (Set _l=%_c%%_l%) Else (
   For /L %%A In (1,1,5) Do If "!_l:~0,1!" Equ "0" Set _l=!_l:~1!)
Set/A KB+=_l/1024, GB=KB/1048576, HH=KB%%1048576*100/1048576
If %HH% Lss 10 Set HH=0%HH%
Set _s=%GB%.%HH%

Hohokami
Posts: 7
Joined: 29 Jun 2016 06:14

Re: Batch Script to log first + last file

#13 Post by Hohokami » 09 Jul 2016 12:41

That's done it.

Many thanks for all your help!

Post Reply