Batch Script to log first + last file
Moderator: DosItHelp
Batch Script to log first + last file
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.
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.
Re: Batch Script to log first + last file
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
Re: Batch Script to log first + last file
Thank you so much. Works perfectly!
Small request: is it possible to change the size output from bytes to Gigabytes?
Thanks again!
Small request: is it possible to change the size output from bytes to Gigabytes?
Thanks again!
Re: Batch Script to log first + last file
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.
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.
Re: Batch Script to log first + last file
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%
Re: Batch Script to log first + last file
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.
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.
Re: Batch Script to log first + last file
Put the command pause on a line above exit/B
Re: Batch Script to log first + last file
Sorry, I think it may have been the following typo on line 3: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.
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")
Re: Batch Script to log first + last file
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.
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.
Re: Batch Script to log first + last file
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).
Re: Batch Script to log first + last file
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)
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)
Re: Batch Script to log first + last file
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%
Re: Batch Script to log first + last file
That's done it.
Many thanks for all your help!
Many thanks for all your help!