Output Newest Folder to Text File

Dell / 1650
January 21, 2009 at 15:48:55
Specs: Windows Server 2003, PIII/512
Hi all,

I've been trying to get a batch file to find the name of the newest folder in a directory and output it into a text file. The following is what I'm using...

------------------
@ECHO OFF

dir "\\backup\shared\111\VolBackup\" /od/ad/b > "C:\script\dirout.txt"

for /f "tokens=* delims= " %%a in (c:\script\dirout.txt) do set latest=%%a

echo %latest% > "C:\script\newest.txt"

exit
------------

The problem is that the file newest.txt comes out with a blank character at the end of the folder name, which messes up the next thing I'm trying to do. Does anyone know what's going wrong?


See More: Output Newest Folder to Text File

Report •


#1
January 21, 2009 at 17:02:02
You're echoing the space after the environment variable so instead of:
echo %latest% > "C:\script\newest.txt" use echo %latest%>"C:\script\newest.txt"

No spaces.


Report •

#2
January 21, 2009 at 18:09:39
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/ad/od') do (
> mytext echo %%a
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
January 22, 2009 at 02:03:23
No need to carry on looping once you've got the latest as the first one (dir /o-d):

@echo off
for /f "tokens=*" %%a in ('dir/b/ad/o-d \\backup\shared\111\VolBackup\') do (
   echo %%a>c:\script\newest.txt
   goto :newestFound
)
:newestFound


Report •

Related Solutions

#4
January 22, 2009 at 02:05:54
Hi klint,

yep


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
January 22, 2009 at 09:39:05
Thanks for the replies everyone! I'm using the code klint posted and it's working great.

Report •


Ask Question